Tkinter Button exercise solutions

Button Tutorial & Exercise
By preseing the + button we can increase the button width and by pressing the - we can decrease the button width.
#increase and decrease button size
import tkinter as tk
my_w=tk.Tk()
my_w.geometry('500x500')

def my_fun(todo):
    w=b2.cget('width')
    if(todo=='increase'):
       w=w+5
    else:
       w=w-5
    b2.config(width=w)
    
b1=tk.Button(my_w,text='+',width=20,bg='yellow',command=lambda: my_fun('increase'))
b1.grid(row=1,column=1)
b2=tk.Button(my_w,text='Close',width=10,bg='Green',command=my_w.destroy)
b2.grid(row=1,column=3)
b3=tk.Button(my_w,text='-',width=20,bg='yellow',command=lambda: my_fun('decrease'))
b3.grid(row=2,column=1)
my_w.mainloop()
Change the colour of Close button by clicking different buttons.
#change the Colour of a button 
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("600x200")  

def my_upd(c_type):
    b_c.config(bg=c_type)
    
b_c=tk.Button(my_w,text='Close',width=10,command=my_w.destroy)
b_c.grid(row=1,column=2)    
    
b1 = tk.Button(my_w, text='Yellow', width=10,bg='yellow',command=lambda: my_upd('yellow'))
b1.grid(row=1,column=1) 

b2 = tk.Button(my_w, text='Blue', width=10,bg='blue',command=lambda: my_upd('blue'))
b2.grid(row=2,column=1) 

b3 = tk.Button(my_w, text='Green', width=10,bg='green',command=lambda: my_upd('green'))
b3.grid(row=3,column=1) 

b4 = tk.Button(my_w, text='Red', width=10,bg='red',command=lambda: my_upd('red'))
b4.grid(row=4,column=1) 

my_w.mainloop()
Changing mouseover Cursor Style over a button.
#change the mouseover cursor style
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("600x200")  

def my_upd(c_type):
    b6.config(cursor=c_type)
    
b1 = tk.Button(my_w, text='Circle', width=10,bg='yellow',command=lambda: my_upd('circle'))
b1.grid(row=1,column=1) 

b2 = tk.Button(my_w, text='Arrow', width=10,bg='blue',command=lambda: my_upd('arrow'))
b2.grid(row=2,column=1) 

b3 = tk.Button(my_w, text='sailboat', width=10,bg='green',command=lambda: my_upd('sailboat'))
b3.grid(row=3,column=1) 

b4 = tk.Button(my_w, text='hand1', width=10,bg='yellow',command=lambda: my_upd('hand1'))
b4.grid(row=4,column=1) 

b5 = tk.Button(my_w, text='hand2', width=10,bg='blue',command=lambda: my_upd('hand2'))
b5.grid(row=5,column=1) 

b6 = tk.Button(my_w, text='Close', width=10,bg='red',command=my_w.destroy)
b6.grid(row=1,column=2) 

my_w.mainloop()
Button Tutorial & Exercise


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