-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathm1e_label.py
More file actions
64 lines (48 loc) · 1.76 KB
/
m1e_label.py
File metadata and controls
64 lines (48 loc) · 1.76 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
"""
Example showing for tkinter and ttk(Object Oriented Approach) :
-- ttk.Label
-- ttk.Button
-- ttk.Frame
-- Associating a Button with a CALLBACK function
Authors: David Mutchler and his colleagues
at Rose-Hulman Institute of Technology.
"""
from tkinter import *
import random
from tkinter import ttk
class Example(Tk):
def __init__(self, width, height):
super().__init__()
# Initial Variable Declaration and Assignment
self.width = width
self.height = height
self.wm_geometry(f'{self.width}x{self.height}')
self.title('Hello!')
self.new_title = ''
# Widgets Creation and Placement
# Main Frame
self.frame_1 = ttk.Frame(self)
self.frame_1.grid()
# Label inside Frame
self.label_1 = ttk.Label(self.frame_1, text='This is a Label above a Button')
self.label_1.grid()
# Buttons_Title_Change
self.change_title_btn = ttk.Button(self.frame_1, text='Change the Title (above)',
command=self.change_title)
self.change_title_btn.grid()
# Button_Quit
self.quit_button = ttk.Button(self.frame_1, text='Quit', command=lambda: self.quit())
self.quit_button.grid()
# Another Label, with its text set another way
self.label2 = ttk.Label(self.frame_1)
self.label2['text'] = 'Later, we will put Labels BESIDE Buttons'
self.label2.grid()
# Functions
def change_title(self):
self.new_title = ''
for K in range(8):
self.new_title = self.new_title + chr(ord('A') + random.randrange(26))
self.title(f'{self.new_title}')
if __name__ == '__main__':
mainwin = Example(250, 120)
mainwin.mainloop()