-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.py
77 lines (67 loc) · 2.19 KB
/
notes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# GUI using pygame
import pygame, time, sys
from notes_gui import MyGUI
import json
class MainWindow:
def __init__(self):
pygame.font.init()
font_file = pygame.font.match_font('timesnewroman')
self.font = pygame.font.Font(font_file, 22) # adjust to match your screen
pygame.display.init()
w, h = 800, 600 # adjust to fit your screen
#self.screen = pygame.display.set_mode((w,h)) # use specified screen size
self.screen = pygame.display.set_mode() # full screen mode
self.w, self.h = self.screen.get_rect().size
self.bg_color = (255, 255, 255) # white background
pygame.display.set_caption('Notes')
self.gui = [] # list of GUI elements
self.mygui = MyGUI(self)
self.mygui.add() # add GUI elements
self.mygui.open_note(False)
self.mygui.previous_page()
self.update_display()
def add(self, g):
self.gui += [g]
def run_window(self):
# main loop
while True:
self.run_cycle()
def run_cycle(self):
time.sleep(0.001)
# process keyboard and mouse events:
for event in pygame.event.get():
self.process_event(event)
def process_event(self, event):
if event.type == pygame.QUIT:
self.Exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
self.Exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
self.mygui.buttondown(event.button, event.pos)
elif event.type == pygame.MOUSEBUTTONUP:
self.mygui.buttonup(event.button, event.pos)
elif event.type == pygame.MOUSEMOTION:
self.mygui.mousemove(event.buttons, event.pos, event.rel)
elif event.type == pygame.KEYDOWN:
self.mygui.keydown(event)
elif event.type == pygame.KEYUP:
self.mygui.keyup(event)
def update_display(self):
self.screen.fill(self.bg_color) # erase everything
self.show()
pygame.display.flip() # make redraw visible
def show(self):
self.mygui.show()
for g in self.gui:
g.show()
def Exit(self):
# do something before exit if need
self.mygui.save_as = False
self.mygui.save_note()
with open(self.mygui.lastnotefile, 'w') as f:
json.dump(self.mygui.file.txt, f)
sys.exit()
if __name__ == '__main__':
# make a window instance and run it
mw = MainWindow()
mw.run_window()