-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
85 lines (62 loc) · 2.54 KB
/
Copy pathmain.py
File metadata and controls
85 lines (62 loc) · 2.54 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
"""
Example showing for tkinter and ttk how to:
-- 1. Make a menubar with menu's
-- 2. Put menu items on the menu's.
-- 3. Establish callback functions for the menu items, that is,
functions that are called when a menu item is selected.,
"""
import tkinter
from tkinter import ttk
class Data(object):
def __init__(self):
self.number = 0
self.number_label = None
def main():
data = Data()
root = tkinter.Tk()
main_frame = ttk.Frame(root, padding=50)
main_frame.grid()
label_text = 'The number is ' + str(data.number)
label = ttk.Label(main_frame, text=label_text)
label.grid()
data.number_label = label
# The default is for menus to be "tear-off" -- they can be dragged
# off the menubar. Use whichever style best suits your GUI.
root.option_add('*tearOff', False)
# Step 1: Make the menu bar
menubar = tkinter.Menu(root)
root['menu'] = menubar
# Step 2: Make the pull-down menu's on the menu bar.
change_menu = tkinter.Menu(menubar)
menubar.add_cascade(menu=change_menu, label='ChangeIt')
show_menu = tkinter.Menu(menubar)
menubar.add_cascade(menu=show_menu, label='ShowIt')
# Step 3: Make menu items for each menu on the menu bar.
# Bind callbacks using lambda, as we have seen elsewhere,
# but this time with a command=... optional argument supplied.
change_menu.add_command(label='Increase the number',
command=lambda: increase_number(data, 1))
change_menu.add_command(label='Decrease the number',
command=lambda: increase_number(data, -1))
show_menu.add_command(label='Show it in red',
command=lambda: show(data, 'red'))
show_menu.add_command(label='Show it in yellow',
command=lambda: show(data, 'yellow'))
root.mainloop()
def increase_number(data, amount):
"""
Increases the number in the given Data object by the given amount
and updates the Label in the given Data object that displays
the number.
"""
data.number = data.number + amount
new_text = 'The number is {}'.format(data.number)
data.number_label['text'] = new_text
def show(data, color):
new_text = 'The number is {}'.format(data.number)
data.number_label['text'] = new_text
data.number_label['background'] = color
# ----------------------------------------------------------------------
# Calls main to start the ball rolling.
# ----------------------------------------------------------------------
main()