-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwindow.py
More file actions
158 lines (127 loc) · 4.97 KB
/
window.py
File metadata and controls
158 lines (127 loc) · 4.97 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from tkinter import Tk,Menu,Frame,Label,TclError
from tkinter import messagebox
from board import board
from tileSource import tileSource
from about import about
from clock import clock
from configShisen import configShisen
from shisenException import shisenException
from log import log
import math
class window:
"""
create a graphic window
"""
def __init__(self):
self.tk = None
self.root = None
self.widgetList = []
self.initial_time = 0
self.menubar = None
def start(self):
"""
create widgets in window
"""
self.root = Tk()
try:
# define title
title = configShisen.load('app-title')
version = configShisen.load('version')
title1 = title.replace("%version%",version)
self.root.wm_title(title1)
self.clock = clock()
# initial cols x rows of board
dimension1 = configShisen.load('initial-dim')
dimension = dimension1[0],dimension1[1]
filename = configShisen.load('tiles-file')
tile_source = tileSource(filename=filename)
tilesize = tile_source.tilesize
menu_bar_height=20
window_title_height = 40
relation = math.ceil(
( ( dimension[0] + 2 ) * tilesize[0] )/
( ( dimension[1] + 2 ) * tilesize[1] )
)
log.save('screen dim:' + str(self.root.winfo_screenwidth()) + ',' + str(self.root.winfo_screenheight()))
window_height = math.ceil(self.root.winfo_screenheight()*0.7)
canvas_height = window_height - (menu_bar_height + window_title_height )
canvas_width = math.ceil(relation * canvas_height)
window_width = canvas_width + 2
log.save("relation:%s,window size:%sx%s" % (relation, window_width,window_height))
# load board
Board = board(self,tile_source,dimension,(canvas_width,canvas_height));
Board.load()
except shisenException as ex:
self.showAlert(ex.msg)
exit(1)
# readjust window width
self.root.geometry('{}x{}'.format(Board.size[0] + 2 , window_height))
self.root.update()
self.createMenu(Board)
menu_inf=Frame(self.root,height=50,bd=1)
menu_inf.grid()
l2 = Label(menu_inf,text='time:')
l2.grid(row=0,column=0)
self.label_time = Label(menu_inf,text='')
self.label_time.grid(row=0,column=1)
self.loop()
def showAlert(self,msg):
messagebox.showwarning('Error',msg)
self.root.destroy()
def createMenu(self,Board):
# create the menu
menubar = Menu(self.root)
# create a pulldown menu, and add it to the menu bar
optionsmenu = Menu(menubar, tearoff=0)
tilelist = configShisen.load('tiles')
for board in tilelist:
dim = board['dim']
dim1 = ( dim[0], dim[1] )
optionsmenu.add_command(label= str(dim[0]) + " x " + str(dim[1]),
command=lambda dim_l=dim1: Board.changeSize(dim_l))
menubar.add_cascade(label="Size", menu=optionsmenu)
menubar.add_command(label="New", command=Board.load)
menubar.add_command(label="Pause", command=Board.pause)
menubar.add_command(label="Clue", command=Board.showClue)
menubar.add_command(label="About",command=self.about)
menubar.add_command(label="Quit", command=self.root.destroy)
self.menubar = menubar
self.root.config(menu=menubar)
def about(self):
# pause clock
self.pauseClock(True)
window = about(self,"About")
window.open()
def pauseClock(self,status):
self.clock.pause_clock = status
def options(self):
pass
def loop(self):
self.clock.reset()
self.timerTasks()
self.root.mainloop()
log.save('exit')
def timerTasks(self):
time=self.clock.update()
self.label_time.configure(text = time)
self.root.after(1000, self.timerTasks)
def onStartGame(self):
self.clock.reset()
self.pauseClock(False)
if not self.menubar is None:
self.menubar.entryconfig('Clue',state="normal")
self.menubar.entryconfig('Pause',state="normal")
def onEndGame(self):
self.pauseClock(True)
self.menubar.entryconfig('Clue',state="disabled")
self.menubar.entryconfig('Pause',state="disabled")
try:
self.menubar.entryconfig('Paused',state="disabled")
except TclError:
pass
def onPauseGame(self):
self.pauseClock(True)
self.menubar.entryconfig('Pause',label="Paused")
def onEndPause(self):
self.pauseClock(False)
self.menubar.entryconfig('Paused',label="Pause")