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()
Every week has a number of the year ( 00,01,02,03 ... 53 ). On Click of a button, if the week number is odd then output should be No and it is Yes if week number is even.
We used int() to convert string to number.
Check the formatted output when we use strftime(). Here we used %U to get the week number as string.
Week number on button click
import tkinter  as tk 
from datetime import date
my_w = tk.Tk()
my_w.geometry("350x300") # change width height 

def my_upd():
    dt=date.today().strftime('%U') # week number as string
    if(int(dt)%2==0):
        str1="Yes" # for even week number 
    else:
        str1="No"  # for odd week number
    l1.config(text=str1) # update the text on label 
b1=tk.Button(my_w,text='Click me',command=lambda:my_upd())
b1.grid(row=1,column=1,padx=60,pady=30)        
l1=tk.Label(my_w,text='Data')
l1.grid(row=1,column=2)
my_w.mainloop()
Button Tutorial & Exercise

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





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