-
Notifications
You must be signed in to change notification settings - Fork 2
/
guimole_starter.py
57 lines (45 loc) · 1.85 KB
/
guimole_starter.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
"""GUI demo: whack-a-mole starter"""
# MCS 260 Fall 2021
# Emily Dumas
import random
import time
import tkinter
import tkinter.ttk
class MoleGame(tkinter.Tk):
"Whack-a-mole game"
def __init__(self):
"Create main window and buttons"
# Call the tkinter.Tk constructor
super().__init__()
# Now add the GUI elements we want
self.title("Hit the mole")
# Build buttons that initially all say "Mole" on them
self.b0 = tkinter.ttk.Button(self,text="Mole",command=self.click0)
self.b0.grid(row=0,column=0,padx=5,pady=5,ipadx=10,ipady=10,sticky="nsew")
self.b1 = tkinter.ttk.Button(self,text="Mole",command=self.click1)
self.b1.grid(row=0,column=1,padx=5,pady=5,ipadx=10,ipady=10,sticky="nsew")
self.b2 = tkinter.ttk.Button(self,text="Mole",command=self.click2)
self.b2.grid(row=0,column=2,padx=5,pady=5,ipadx=10,ipady=10,sticky="nsew")
self.b3 = tkinter.ttk.Button(self,text="Mole",command=self.click3)
self.b3.grid(row=0,column=3,padx=5,pady=5,ipadx=10,ipady=10,sticky="nsew")
self.buttons = [self.b0, self.b1, self.b2, self.b3]
#----------------------------------------------------------
# Let's just show how you might change every button's text
# (you might adapt this part and use it elsewhere, but
# you should definitely delete it from the constructor)
for i,b in enumerate(self.buttons):
b.config(text="Btn"+str(i))
def click0(self):
print("Button 0 was pressed")
self.quit() # Tk.quit() exits the application
def click1(self):
print("Button 1 was pressed")
self.quit()
def click2(self):
print("Button 2 was pressed")
self.quit()
def click3(self):
print("Button 3 was pressed")
self.quit()
g = MoleGame()
g.mainloop()