-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathttkSub-module.py
More file actions
119 lines (93 loc) · 3.22 KB
/
ttkSub-module.py
File metadata and controls
119 lines (93 loc) · 3.22 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
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
import tkinter as tk
import tkinter.ttk as ttk
import itertools
# Introduction to the ttk sub module
def samplettk():
win = tk.Tk()
button_tk = tk.Button(win, text='tk')
button_ttk = ttk.Button(win, text='ttk')
button_tk.pack(padx=10, pady=10)
button_ttk.pack(padx=10, pady=10)
win.mainloop()
# Styling a tk widget
def samplettk02():
"""
A list of aspects and values for styling can be found online on
http://effbot.org/tkinterbook/tkinter-widget-styling.htm
"""
style_1 = {
'fg': 'red',
'bg': 'black',
'activebackground': 'gold',
'activeforeground': 'dim gray'
}
style_2 = {
'fg': 'yellow',
'bg': 'grey',
'activebackground': 'chocolate',
'activeforeground': 'blue4'
}
style_cycle = itertools.cycle([style_1, style_2])
def switch_style():
style = next(style_cycle)
button.configure(**style) # used to define the keywords as a dictionary is being passed
win = tk.Tk()
button = tk.Button(win, text='style switch', command=switch_style)
button.pack(padx=50, pady=50)
win.mainloop()
def samplettk03():
"""
styling a ttk widget
"""
win = tk.Tk()
style = ttk.Style()
style_1 = {
'foreground': 'red',
'background': 'grey'
}
style_2 = {
'foreground': 'yellow',
'background': 'grey'
}
mapping_1 = {
'background': [('pressed', 'gold'), ('active', 'magenta')]
}
mapping_2 = {
'background': [('pressed', 'chocolate'), ('active', 'blue4')]
}
style_cycle = itertools.cycle([style_1, style_2])
mapping_cycle = itertools.cycle([mapping_1, mapping_2])
def switch_style():
style_choice = next(style_cycle)
mapping_choice = next(mapping_cycle)
style.configure('TButton', **style_choice)
style.map('TButton', **mapping_choice)
button = ttk.Button(win, text='style switch', command=switch_style, style='TButton')
button.pack(padx=50, pady=50)
win.mainloop()
def ttksample04():
"""
ttk Style Inheritance
A list of all of these can be found online at:
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-style-layer.html
"""
win = tk.Tk()
regular_button = ttk.Button(win, text='regular button')
small_button = ttk.Button(win, text='small button', style='small.TButton')
big_button = ttk.Button(win, text='big button', style='big.TButton')
big_dangerous_button = ttk.Button(win, text='big dangerous button', style='danger.big.TButton')
small_dangerous_button = ttk.Button(win, text='small dangerous button', style='danger.small.TButton')
style = ttk.Style()
style.configure('TButton', foreground="blue4")
style.configure('small.TButton', font=(None, 7))
style.configure('big.TButton', font=(None, 20))
style.configure('danger.small.TButton', foreground="red")
style.configure('danger.big.TButton', foreground="dark red")
regular_button.pack(padx=50, pady=50)
small_button.pack(padx=50, pady=50)
big_button.pack(padx=50, pady=50)
big_dangerous_button.pack(padx=50, pady=50)
small_dangerous_button.pack(padx=50, pady=50)
win.mainloop()
if __name__ == '__main__':
ttksample04()