Skip to content

Commit 9ea31eb

Browse files
committed
Add demo for use of Label widget
Code for text manipulation in Label widget. Code for image display in Label widget.
1 parent f84baa8 commit 9ea31eb

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/program3.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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()

src/simple_gif.gif

58.8 KB
Loading

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy