-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_3_buttons.py
98 lines (83 loc) · 2.14 KB
/
1_3_buttons.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
import tkinter as tk
import ttkbootstrap as ttk
# setup
window = ttk.Window()
window.title('buttons')
window.geometry('600x400')
# button
def button_func():
print('a basic button')
print(radio_var.get())
button_string = tk.StringVar(value = 'A button with string var')
button = ttk.Button(window, text = 'A simple button', command = lambda: print('a simple button'), textvariable = button_string)
# button = ttk.Button(window, text = 'A simple button', command = button_func) # 本行和上面一行相同,上面使用了lambda
button.pack()
# checkbutton
check_var = tk.IntVar()
check1 = ttk.Checkbutton(
window,
text = 'check box1',
command = lambda: print(check_var.get()),
variable = check_var,
onvalue = 10,
offvalue = 5)
check1.pack()
check2 = ttk.Checkbutton(
window,
text = 'Checkbox 2',
command = lambda: check_var.set(5))
check2.pack()
# radio buttons
radio_var = tk.StringVar()
radio1 = ttk.Radiobutton(
window,
text = 'Radiobutton 1',
value = 'radio 1',
variable = radio_var,
command = lambda: print(radio_var.get()))
radio1.pack()
radio2 = ttk.Radiobutton(
window,
text = 'Radiobutton 2',
value = 2,
variable = radio_var)
radio2.pack()
# create another checkbutton and 2 radiobuttons
# radio button:
# values for the buttons are A and B
# ticking either prints the value of the checkbutton
# ticking the radio button unchecks the checkbutton
# check button:
# ticking the checkbutton prints the value of the radio button value
# use tkinter var for Booleans!
# exercise radios
def radio_func():
print(check_bool.get())
check_bool.set(False)
#data
radio_string = tk.StringVar()
check_bool = tk.BooleanVar()
#widgets
exercise_radio1 = ttk.Radiobutton(
window,
text = 'Radio A',
value = 'A',
command = radio_func,
variable = radio_string)
exercise_radio2 = ttk.Radiobutton(
window,
text = 'Radio B',
value = 'B',
command = radio_func,
variable = radio_string)
exercise_check = ttk.Checkbutton(
window,
text = 'exercise check',
variable = check_bool,
command = lambda: print(radio_string.get()))
# layout
exercise_radio1.pack()
exercise_radio2.pack()
exercise_check.pack()
# run
window.mainloop()