-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathexample-3.py
More file actions
90 lines (61 loc) · 2.09 KB
/
Copy pathexample-3.py
File metadata and controls
90 lines (61 loc) · 2.09 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
import tkinter as tk
'''
One Spinbox automatically changes other Spinbox
'''
def sb1_changed(): # first spinbox
# get selection in first spinbox
selected = sb1_var.get()
# find its index on first list of all values
idx = vals1.index(selected)
# use index to get value for other list of all values
val = vals2[idx]
# set selection in second spinbox
sb2_var.set(val)
print('selected/index/val:', selected, idx, val)
def sb2_changed(): # second spinbox
# get selection in first spinbox
selected = sb2_var.get()
# find its index on first list of all values
idx = vals2.index(selected)
# use index to get value for other list of all values
val = vals1[idx]
# set selection in second spinbox
sb1_var.set(val)
print('selected/index/val:', selected, idx, val)
# ---
'''
sb1['textvariable'] give ID, not object but sb1.get() works
sb1['values'] gives list with values
'''
def changed(sb1, sb2):
# get selection in first spinbox
selected = sb1.get()
# find its index on first list of all values
idx = sb1['values'].index(selected) # <- but it works ['values']
# use index to get value for other list of all values
val = sb2['values'][idx] # <- but it works ['values']
print('selected/index/val:', selected, idx, val)
# set selection in second spinbox
sb2.insert('0', val) # <- problem with ['textvariable']
print('selected/index/val:', selected, idx, val)
# --- main ---
vals1 = ['A', 'B', 'C']
vals2 = ['1', '2', '3']
root = tk.Tk()
sb1_var = tk.StringVar()
sb2_var = tk.StringVar()
sb1 = tk.Spinbox(root, textvariable=sb1_var, values=vals1, command=sb1_changed)
sb1.pack()
sb2 = tk.Spinbox(root, textvariable=sb2_var, values=vals2, command=sb2_changed)
sb2.pack()
# ---
sbA_var = tk.StringVar()
sbB_var = tk.StringVar()
sbA = tk.Spinbox(root, textvariable=sbA_var, values=vals1)
sbA.pack()
sbB = tk.Spinbox(root, textvariable=sbB_var, values=vals2)
sbB.pack()
sbA['command'] = lambda:changed(sbA, sbB)
sbB['command'] = lambda:changed(sbB, sbA)
# ---
root.mainloop()