-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dialog.py
214 lines (155 loc) · 5.84 KB
/
Dialog.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
class Dialog(Toplevel):
def __init__(self, parent, title = None, data = None):
Toplevel.__init__(self, parent)
self.transient(parent)
if title:
self.title(title)
self.parent = parent
self.result = None
body = Frame(self)
if data:
self.initial_focus = self.body(body, data)
else:
self.initial_focus = self.body(body)
body.pack(padx=5, pady=5)
if data:
self.buttonbox(data)
else:
self.buttonbox()
self.grab_set()
if not self.initial_focus:
self.initial_focus = self
self.protocol("WM_DELETE_WINDOW", self.cancel)
self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
parent.winfo_rooty()+50))
self.initial_focus.focus_set()
self.wait_window(self)
#
# construction hooks
def body(self, master, data = None):
# create dialog body. return widget that should have
# initial focus. this method should be overridden
pass
def buttonbox(self, data = None):
# add standard button box. override if you don't want the
# standard buttons
box = Frame(self)
w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE)
w.pack(side=LEFT, padx=5, pady=5)
w = Button(box, text="Cancel", width=10, command=self.cancel)
w.pack(side=LEFT, padx=5, pady=5)
self.bind("<Return>", self.ok)
self.bind("<Escape>", self.cancel)
box.pack()
#
# standard button semantics
def ok(self, event=None):
if not self.validate():
self.initial_focus.focus_set() # put focus back
return
self.withdraw()
self.update_idletasks()
self.apply()
self.cancel()
print("ok method called")
def cancel(self, event=None):
# put focus back to the parent window
self.parent.focus_set()
self.destroy()
#
# command hooks
def validate(self):
return 1 # override
def apply(self):
pass # override
class SetPlayersDialog(Dialog):
def buttonbox(self, data = None):
box = Frame(self)
def set(num):
self.result = num
self.ok()
w = Button(box, text="1", width=10, command=lambda : set(1))
w.pack(side=LEFT, padx=5, pady=5)
w = Button(box, text="2", width=10, command=lambda : set(2))
w.pack(side=LEFT, padx=5, pady=5)
w = Button(box, text="3", width=10, command=lambda : set(3))
w.pack(side=LEFT, padx=5, pady=5)
w = Button(box, text="4", width=10, command=lambda : set(4))
w.pack(side=LEFT, padx=5, pady=5)
w = Button(box, text="Cancel", width=10, command=self.cancel)
w.pack(side=BOTTOM, padx=5, pady=5)
self.bind("<Escape>", self.cancel)
box.pack()
def apply(self):
pass
# def setPlayerCount(self):
class SelectTokensDialog(Dialog):
def body(self, master, data = None):
Label(master, text="Set your name:").grid(row=0)
self.e1 = Entry(master)
self.e1.grid(row=0, column=1)
return self.e1 # initial focus
def buttonbox(self, data = None):
box = Frame(self)
# images = []
# for imagePath in data["images"]:
# image = PhotoImage(file=imagePath)
# images.append(image)
def set(num, token):
self.result = num, token
self.ok()
for index, token in enumerate(data['tokens']):
if token not in data['selectedTokens']:
image = PhotoImage(file=data["images"][index])
b = ttk.Button(box, image=image, command=lambda index=index, token=token: set(index, token))
b.image = image
b.pack(side=LEFT)
box.pack()
def validate(self):
first= self.e1.get()
if first.strip():
return 1
else:
messagebox.showwarning(
"Bad input",
"Please enter a name"
)
return 0
def apply(self):
name = self.e1.get()
token = self.result[0]
tokenData = self.result[1]
returnData = name, token, tokenData
self.result = returnData
# print first, second # or something
class EndGameScreen(Dialog):
def body(self, master, data = None):
Label(master, text="Congratulations " + data["player"].name + "!").grid(row=0, columnspan=5)
# include any code here to draw the end game stats
Label(master, text="Game Stats").grid(row=1)
Label(master, text="Player:").grid(row=2)
Label(master, text="Turns:").grid(row=3)
Label(master, text="Portals:").grid(row=4)
Label(master, text="Tiles:").grid(row=5)
Label(master, text="Remaining:").grid(row=6)
for index, player in enumerate(data["players"]):
Label(master, text=player.name).grid(row=2, column=1+index)
Label(master, text=player.turncount).grid(row=3, column=1 + index)
Label(master, text=player.portalsactivated).grid(row=4, column=1 + index)
Label(master, text=player.tilesmoved).grid(row=5, column=1 + index)
Label(master, text=39 - player.location).grid(row=6, column=1 + index)
def buttonbox(self, data = None):
def exit(method):
self.cancel();
method();
box = Frame(self)
for index, option in enumerate(data['menuOptions']["options"]):
image = PhotoImage(file=option["image"])
b = ttk.Button(box, image=image, command=lambda option=option:exit(option["method"]))
b.image = image
b.pack(side=LEFT)
box.pack()