import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("200x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
n=10 # number of buttons
i=3
for j in range(n):
e = Button(my_w, text=j)
e.grid(row=i, column=j)
my_w.mainloop() # Keep the window open
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("200x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
my_str = tk.StringVar()
l1 = tk.Label(my_w, textvariable=my_str, width=10 )
l1.grid(row=1,column=2,columnspan=6)
def my_fun(k):
my_str.set("Btn No is : "+ str(k) )
n=10 # number of buttons
i=2
for j in range(n):
e = Button(my_w, text=j,command=lambda k=j: my_fun(k))
e.grid(row=i, column=j)
my_w.mainloop() # Keep the window open
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("200x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
my_str = tk.StringVar()
l1 = tk.Label(my_w, textvariable=my_str, width=10 )
l1.grid(row=0,column=1,columnspan=5)
def show_lan(my_language):
my_str.set(my_language)
list_languages = ("PHP","Python","HTML","Tkinter")
var = 0
for language in list_languages:
btn = tk.Button(my_w, text=language, command=lambda lan=language:show_lan(lan))
btn.grid(row=1,column=var)
var += 1
root.mainloop()
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
my_str = tk.StringVar()
l1 = tk.Label(my_w, textvariable=my_str, width=10 )
l1.grid(row=0,column=2,columnspan=6)
def show_lan(my_language,var):
my_str.set(my_language)
#loop through all the buttons to enable or disable each one
for i in range(len(buttons)):
if i==var:
buttons[i].config(state="disabled")
else:
buttons[i].config(state="normal")
list_languages = ("PHP","Python","HTML","Tkinter","Pandas")
var = 0
buttons = [] # to store button references
#command=lambda index=index, n=n: appear(index, n)
for language in list_languages:
btn = tk.Button(my_w, text=language, command=lambda var=var,lan=language:show_lan(lan,var))
btn.grid(row=1,column=var)
var += 1
buttons.append(btn) # adding button reference
my_w.mainloop()
var =0
for i in range(10):
btn = tk.Button(my_w, text=str(i),font=font1)
btn.grid(row=0,column=var,padx=5,pady=20)
var += 1
We will add one more button with onClick event to trigger a function my_upd()
b1=tk.Button(my_w,text='Update Green',bg='lightgreen',command=lambda:my_upd())
b1.grid(row=1,column=0,columnspan=5)
The function my_upd() will update the options of the Buttons and change the background colour. As we are reading the text written over the button and converting the same to integer, we may get error if any button text we fail to convert to integer.def my_upd():
for wgd in my_w.winfo_children(): # all widgets
try:
if int(wgd['text']) %2 ==0: # if the number written is even number
wgd['bg']='lightgreen' # change the background colour
else:
wgd['bg']='lightpink'
except:
pass
import tkinter as tk
my_w = tk.Tk()
my_w.geometry('400x200')
my_w.title("www.plus2net.com") # Adding a title
font1=('Times',14,'bold')
var =0
for i in range(10):
btn = tk.Button(my_w, text=str(i),font=font1)
btn.grid(row=0,column=var,padx=6,pady=30)
var += 1
def my_upd():
for wgd in my_w.winfo_children(): # all widgets
try:
if int(wgd['text']) %2 ==0: # if the number written is even number
wgd['bg']='lightgreen' # change the background colour
else:
wgd['bg']='lightpink'
except:
pass
b1=tk.Button(my_w,text='Update Green',bg='lightgreen',command=lambda:my_upd())
b1.grid(row=1,column=0,columnspan=5,padx=10,pady=10)
my_w.mainloop()
View and Download tkinter-button-dynamic ipynb file ( .html format )
09-04-2022 | |
Thank you. Very helpful demonstration. I even liked the mistake towards the end and the correction |
25-10-2022 | |
Thank You so much sir/madam.. You were my life saver. The logic in your code really helped me proceed with my project. THANKS A TON !!! |
26-11-2022 | |
Thank you very much for the solution to create multiple tkinter Buttons with for a loop. I searched and followed may pages, none works -- all buttons invoke the lambda function with the same argument. Your code works! The key is: k=j in 'command=lambda k=j: my_fun(k)' [as compared to 'command=lambda: my_fun(j), which does not work. Seems k=j creates a true copy of j. |
24-12-2022 | |
Yes, you are right, the variable k is unique to the button generated. |