1
+ '''
2
+ Created on Aug 13, 2017
3
+
4
+ @author: Aditya
5
+
6
+ this program demosntrates the use of Label widgets.
7
+
8
+ '''
9
+
10
+ import tkinter as tk
11
+ from tkinter import ttk
12
+
13
+ class GreetingApp :
14
+
15
+ def __init__ (self , master ): # constructor method
16
+ # define list of label texts
17
+ self .greet_list = ('Hello, World! This is python GUI.' , \
18
+ 'Hello, This is Python GUI. Sadly, I was made to say Hello only. I will love to say so much more.' )
19
+
20
+ # creat label as a child of root window with some text.
21
+ self .label = ttk .Label (master , text = self .greet_list [0 ])
22
+
23
+ self .btn = ttk .Button (master , text = 'Greet Again' , command = self .handle_text ) # create a button
24
+
25
+ # store image in the label obj to keep it in the scope as
26
+ # long as label is displayed and to avoid getting the image getting garbage collected
27
+ imgpath = 'simple_gif.gif'
28
+ self .label .img = tk .PhotoImage (file = imgpath )
29
+ self .label .config (compound = 'left' ) # to display image in left of text
30
+
31
+
32
+ self .label .pack () # pack label to the window with pack() geometry method of Label
33
+ self .btn .pack ()
34
+ self .label .config (wraplength = 200 )
35
+ self .label .config (justify = tk .CENTER ) # justify can be CENTER, RIGHT, LEFT
36
+ self .label .config (foreground = 'blue' , background = 'yellow' ) # insert font color (foreground) and background color
37
+ self .label .config (font = ('Times' , 10 , 'bold' )) # font = (font_name, font_size, font_type)
38
+
39
+ # store image in the label obj to keep it in the scope as
40
+ # long as label is displayed and to avoid getting the image getting garbage collected
41
+ imgpath = 'simple_gif.gif'
42
+ self .label .img = tk .PhotoImage (file = imgpath )
43
+ self .label .config (image = self .label .img )
44
+ self .label .config (compound = 'left' ) # to display image in left of text
45
+
46
+ def handle_text (self ):
47
+ if self .label ['text' ] == self .greet_list [0 ]:
48
+ self .label .config (text = self .greet_list [1 ])
49
+ else :
50
+ self .label .config (text = self .greet_list [0 ])
51
+
52
+ def test ():
53
+ root = tk .Tk ()
54
+ GreetingApp (root )
55
+ root .mainloop ()
56
+
57
+ if __name__ == '__main__' : test ()
0 commit comments