gui radio button python 1

gui radio button python

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import askyesno, askquestion




def selected():
    print(confirmed.get())


root = tk.Tk()

confirmed = tk.StringVar() #used to get the 'value' property of a tkinter.Radiobutton

# Note that I added a command to each radio button and a different 'value'
# When you press a radio button, its corresponding 'command' is called.
# In this case, I am linking both radio buttons to the same command: 'selected'

rconfirmed = tk.Radiobutton(text='Radio Button 1', variable=confirmed, 
                          value="yes", command=selected)
rconfirmed.pack()
rconfirmed= tk.Radiobutton(text='Radio Button 2', variable=confirmed, 
                          value="no", command=selected)
rconfirmed.pack()

root.mainloop()

Here is what the above code is Doing:
1. When you press the first radio button, the ‘selected’ function is called.
2. The ‘selected’ function prints the value of the ‘confirmed’ variable.
3. The ‘confirmed’ variable is linked to the ‘value’ property of the radio button.
4. The ‘value’ property of the radio button is set to ‘yes’.
5. The ‘selected’ function prints ‘yes’.

Similar Posts