Python tkinter Label

This code we will add one label.
Basics of Python Tkinter
import tkinter  as tk 
my_w = tk.Tk()
my_w.geometry("500x500")  

my_str = tk.StringVar()
l1 = tk.Label(my_w,  textvariable=my_str, width=10 )
l1.grid(row=1,column=2) 
my_str.set("Hi Welcome")

my_w.mainloop()
To manage layout read more on grid()

Positioning Labels using GRID

Tkinter Label positioning
We will change the font size, font colour, font family and place them at different locations using GRID row and column layouts.
import tkinter  as tk 
my_w = tk.Tk()
my_w.geometry("300x200")  

my_font1=('times', 18, 'bold')
my_str1 = tk.StringVar()
l1 = tk.Label(my_w,  textvariable=my_str1, fg='red',font=my_font1 )
l1.grid(row=1,column=2,columnspan=3,sticky='E') 
my_str1.set("plus2net.com")

my_font2=('Verdana', 10, 'normal')
my_str2 = tk.StringVar()
l2 = tk.Label(my_w,  textvariable=my_str2, fg='black',font=my_font2 )
l2.grid(row=2,column=1) 
my_str2.set("python tutorials")

my_font3=('MS Sans Serif', 10, 'italic')
my_str3 = tk.StringVar()
l3 = tk.Label(my_w,  textvariable=my_str3, fg='green',font=my_font3 )
l3.grid(row=3,column=3) 
my_str3.set("Tkinter")

my_str4 = tk.StringVar()
l4 = tk.Label(my_w,  textvariable=my_str4, font=my_font2 )
l4.config(fg='blue')
l4.grid(row=4,column=4)
my_str4.set("Pandas")


my_str5 = tk.StringVar()
l5 = tk.Label(my_w,  textvariable=my_str5, fg='yellow',font=my_font2 )
l5.grid(row=5,column=5) 
my_str5.set("Numpy")

my_w.mainloop()
There are many optional options we can add to Label, the list is available below.

Now let us add the click event of a button button, to change the text of the label.
# Change the text of a label 
import tkinter  as tk 
my_w = tk.Tk()
my_w.geometry("500x500")  

def my_upd():
     my_str.set("plus2net.com")

my_str = tk.StringVar()
l1 = tk.Label(my_w,  textvariable=my_str, width=10 )
l1.grid(row=1,column=2) 
my_str.set("Hi Welcome")

b1 = tk.Button(my_w, text='Change text', width=15,bg='yellow',command=lambda: my_upd())
b1.grid(row=2,column=2) 

my_w.mainloop()
Read text from Label and copy the same to another Label
import tkinter  as tk 
my_w = tk.Tk()
my_w.geometry("500x500")  

def my_upd():
    my_str.set("plus2net.com") # set string for button 1
    my_copy_text=l1.cget('text')
    my_str2.set(my_copy_text) # read text from button 1 and set text to button 2

#my_str = tk.StringVar()
l1 = tk.Label(my_w,  text='Hi Welcome', width=10 )
l1.grid(row=1,column=2) 
#my_str.set("Hi Welcome")

my_str2 = tk.StringVar()
l2 = tk.Label(my_w,  textvariable=my_str2, width=10 )
l2.grid(row=1,column=3) 
my_str2.set("I will change")

b1 = tk.Button(my_w, text='Change text', width=15,bg='yellow',command=lambda: my_upd())
b1.grid(row=2,column=2) 

my_w.mainloop()

Changing the font colour dynamically

Based on condition or event we can change the color of the font.
l1.config(fg='green') # foreground color
Like this many other values we can change.
l1.config(bg='yellow') # background color

Adding Image

Window with Image
import tkinter  as tk 
my_w = tk.Tk()
my_w.geometry("400x200")  

l1 = tk.Label(my_w,  width=15 )
l1.grid(row=1,column=1) 

my_img = tk.PhotoImage(file = "D:\\top2.png") 
l2 = tk.Label(my_w,  image=my_img )
l2.grid(row=1,column=2) 


my_w.mainloop()
  1. Exercise on Label
  2. By using four buttons change the Text and Background colour of a Label. On clicking the first button the label should change to Google and background colour should change to Red. Similarly the text and background colour should change on clicking for all other three buttons.
Solution


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer