Tkinter Button exercise solutions


By preseing the + button we can increase the button width and by pressing the - we can decrease the button width.
Managing button width
#increase and decrease button size
import tkinter as tk
my_w=tk.Tk()
my_w.geometry('400x300')

def my_fun(todo):
    w=b2.cget('width')
    if(todo=='increase'):
       w=w+5
    else:
       w=w-5
    b2.config(width=w) # Update the width option 
    
b1=tk.Button(my_w,text='+',width=5,font=18,bg='yellow',
    command=lambda: my_fun('increase'))
b1.grid(row=1,column=1,padx=5,pady=30)
b2=tk.Button(my_w,text='Close',width=10,bg='Green',fg='white',
    command=my_w.destroy)
b2.grid(row=1,column=3)
b3=tk.Button(my_w,text='-',width=5,font=18,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()


Updating width option of a Tkinter button by connecting to a Spinbox value by using config()


Change the width of a button based on selection of value of a Spinbox.
Read more on how to manage options by using config().
Configure width of the button by using Spinbox

import tkinter as tk
from tkinter import *

my_w = tk.Tk()
my_w.geometry("450x300")  # Size of the window, width & height
my_w.title("www.plus2net.com")

# Creating list from a range with start , stop and step values
my_list = list(range(10, 30, 2))
t2 = tk.IntVar()  # to store the selected value of the Spinbox
sb2 = Spinbox(
    my_w, values=my_list, width=4, textvariable=t2, font=18, command=lambda: my_upd()
)
sb2.grid(row=0, column=0, padx=20, pady=60)

b1 = tk.Button(my_w, text="Demo", font=16, width=10, bg="yellow")
b1.grid(row=0, column=1, pady=60)

def my_upd():  # Update the width of the button
    b1.config(width=t2.get())


my_w.mainloop()  # Keep the window open
Button Tutorial & Exercise

Subscribe to our YouTube Channel here


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-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer