import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x120") # Size of the window
r1_v = tk.IntVar()
r1 = tk.Radiobutton(my_w, text='Passed', variable=r1_v, value=1)
r1.grid(row=1,column=1,padx=30,pady=30)
r2 = tk.Radiobutton(my_w, text='Failed', variable=r1_v, value=0)
r2.grid(row=1,column=2)
r3 = tk.Radiobutton(my_w, text='Appearing', variable=r1_v, value=5)
r3.grid(row=1,column=3)
my_w.mainloop() # Keep the window open
my_val=r1_v1.get() #r1_v1 is the variable connected to radiobutton
How to set the value of the radiobutton ( to select or Unselect )?
r1_v1.set(1)
We will use the above concepts. Note that radiobutton can have values 1 or 0 or any other integer( integer variable ), Yes or No or any other string ( string variable) , True or False ( Boolean variable ). We will learn all three types here.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
def my_upd():
print('Radiobutton value :',r1_v.get())
r1_v = tk.IntVar() # We used integer variable here
r1 = tk.Radiobutton(my_w, text='Passed', variable=r1_v, value=1,command=my_upd)
r1.grid(row=1,column=1)
r2 = tk.Radiobutton(my_w, text='Failed', variable=r1_v, value=0,command=my_upd)
r2.grid(row=1,column=2)
r3 = tk.Radiobutton(my_w, text='Appearing', variable=r1_v, value=5,command=my_upd )
r3.grid(row=1,column=3)
my_w.mainloop() # Keep the window open
Capturing click event of Radiobutton and display the string variable
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
def my_upd():
print('Radiobutton value :',r1_v.get())
r1_v = tk.StringVar() # We used string variable here
r1_v.set('Passed') # Can assign value Appear or Failed
r1 = tk.Radiobutton(my_w, text='Passed', variable=r1_v, value='Passed',command=my_upd)
r1.grid(row=1,column=1)
r2 = tk.Radiobutton(my_w, text='Failed', variable=r1_v, value='Failed',command=my_upd)
r2.grid(row=1,column=2)
r3 = tk.Radiobutton(my_w, text='Appearing', variable=r1_v, value='Appear',command=my_upd )
r3.grid(row=1,column=3)
my_w.mainloop() # Keep the window open
Now let us use one Boolean variable
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
def my_upd():
print('Radiobutton value :',r1_v.get())
r1_v = tk.BooleanVar() # We assigned Boolean variable here
r1_v.set(True) # Can assign False
r1 = tk.Radiobutton(my_w, text='Passed', variable=r1_v, value=True,command=my_upd)
r1.grid(row=1,column=1)
r2 = tk.Radiobutton(my_w, text='Failed', variable=r1_v, value=False,command=my_upd)
r2.grid(row=1,column=2)
my_w.mainloop() # Keep the window open
r1_v.set('Passed') # Can assign value Appear or Failed
We can change above code to set the radio button to a different selection.
r1_v.set(None)
How Reset button is used to delete all user enterd data and selections
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
def my_upd():
r2_v.set(r1_v.get()) # read from first group and set the second group
print('Radiobutton value :',r1_v.get())
r1_v = tk.StringVar()
r1_v.set('Passed') # (default value ) Can assign value Appear or Failed
r1 = tk.Radiobutton(my_w, text='Passed', variable=r1_v, value='Passed',command=my_upd)
r1.grid(row=1,column=1)
r2 = tk.Radiobutton(my_w, text='Failed', variable=r1_v, value='Failed',command=my_upd)
r2.grid(row=1,column=2)
r3 = tk.Radiobutton(my_w, text='Appearing', variable=r1_v, value='Appear',command=my_upd )
r3.grid(row=1,column=3)
r2_v = tk.StringVar()
r2_v.set('Passed') # default value
r4 = tk.Radiobutton(my_w, text='Passed', variable=r2_v, value='Passed',command=my_upd)
r4.grid(row=2,column=1)
r5 = tk.Radiobutton(my_w, text='Failed', variable=r2_v, value='Failed',command=my_upd)
r5.grid(row=2,column=2)
r6 = tk.Radiobutton(my_w, text='Appearing', variable=r2_v, value='Appear',command=my_upd )
r6.grid(row=2,column=3)
my_w.mainloop()
deselect()
: DeSelect or off the radio buttonflash()
: flash the radio button but leave it in its original state.invoke()
: Select the radio button and call the function.select()
: Select the radio button. import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x250") # Size of the window
my_w.title("www.plus2net.com") # Window title
font1=['Arial',15,'normal'] # Font type,size, style
def my_upd():
print('Radiobutton value :',r1_v.get())
r1_v=tk.StringVar(value='MySQL')
r1=tk.Radiobutton(my_w,text='PHP',variable=r1_v,value='PHP',command=my_upd,font=font1)
r1.grid(row=1,column=1,padx=20,pady=30)
r2=tk.Radiobutton(my_w,text='MySQL',variable=r1_v,value='MySQL',command=my_upd)
r2.grid(row=1,column=2,padx=20,pady=30)
r3=tk.Radiobutton(my_w,text='Python',variable=r1_v,value='Python',command=my_upd)
r3.grid(row=1,column=3,padx=20,pady=30)
bt1=tk.Button(my_w,text='select()',command=lambda:r1.select())
bt1.grid(row=2,column=1)
bt2=tk.Button(my_w,text='deselect()',command=lambda:r1.deselect())
bt2.grid(row=2,column=2)
bt3=tk.Button(my_w,text='flash()',command=lambda:r1.flash())
bt3.grid(row=2,column=3)
bt4=tk.Button(my_w,text='invoke()',command=lambda:r1.invoke())
bt4.grid(row=2,column=4)
my_w.mainloop() # Keep the window open
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x150") # Size of the window
r1_v = tk.IntVar()
r1 = tk.Radiobutton(my_w, text='Passed', variable=r1_v, value=1)
r1.grid(row=1,column=1,padx=25,pady=25)
r2 = tk.Radiobutton(my_w, text='Failed', variable=r1_v, value=0)
r2.grid(row=1,column=2)
r3 = tk.Radiobutton(my_w, text='Appearing', variable=r1_v,
value=5, state='disabled')
r3.grid(row=1,column=3)
r2.config(state='disabled') # active , normal , disabled
my_w.mainloop() # Keep the window open
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("200x150") # Size of the window
r1_v = tk.IntVar()
r1 = tk.Radiobutton(my_w, bitmap='info', variable=r1_v, value=1)
r1.grid(row=1,column=1,padx=15)
r2 = tk.Radiobutton(my_w, bitmap='question', variable=r1_v, value=2)
r2.grid(row=1,column=2,padx=15,pady=40)
r3 = tk.Radiobutton(my_w, bitmap='warning', variable=r1_v,value=3)
r3.grid(row=1,column=3)
my_w.mainloop() # Keep the window open
import datetime
today = datetime.datetime.now() # creating date object
year=today.year # Present Year as Integer
year1 = today.year+1 #Next Year
year2= today.year-1 #Previous Year
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x200") # Size of the window
r1_v = tk.IntVar(value=year) # declaring Intvar
r1 = tk.Radiobutton(my_w, text=year2, variable=r1_v, value=year2)
r1.grid(row=0,column=0,padx=10,pady=30)
r2 = tk.Radiobutton(my_w, text=year, variable=r1_v, value=year)
r2.grid(row=0,column=1,padx=5)
r3 = tk.Radiobutton(my_w, text=year1, variable=r1_v, value=year1)
r3.grid(row=0,column=2,padx=5)
lb=tk.Label(my_w,text='Query',bg='yellow',width=50)
lb.grid(row=1,column=0,columnspan=3,padx=5)
def my_upd(*args): # on click of radio buttons
query="SELECT * FROM my_tasks \
WHERE strftime('%Y',dt)='"+str(r1_v.get())+"'"
print(query)
lb.config(text=query)
r1_v.trace('w',my_upd)
my_w.mainloop()
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x150") # Size of the window
def selected():
print("Selected value:", my_var.get())
my_w.title("Radiobutton Customization")
my_font=['Arial',18,'normal']
# Variable to hold the selected Radiobutton's value
my_var = tk.IntVar()
# List of options
options = [("PHP ", 1), ("Python ", 2), ("JSP ", 3)]
i=0
for (text, value) in options:
rd1=tk.Radiobutton(my_w, text=text, variable=my_var, value=value,
command=selected, fg="blue", bg="light gray",font=my_font,
selectcolor="yellow", activebackground="light blue",
activeforeground="yellow", indicatoron=0, width=10,
compound="left")
rd1.grid(row=0,column=i,pady=30 ,padx=8)
i=i+1
my_w.mainloop()
Food Menu system using Checkbutton indicatoron attribute 14-03-2024 | |
Hi, my radio buttons using customtkinter work just debbuging into VSCODE, but when I use pyinstaller for .exe they not show, i print the error in messagebox and the error is just 'CTkRadiobutton' |