Python Forum
[Tkinter] Clicking a RadioButton in a for Loop & Getting the Appropriate Return
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Clicking a RadioButton in a for Loop & Getting the Appropriate Return
#1
from tkinter import *
root = Tk()
root.title('Register')

def status():
    word = varr.get()
    gender = var.get()
    if gender == 1:
        text.insert(END,f'Hi Mr {word}\nHow are you doing today?')
    else:
        text.insert(END, f'Hi Miss {word}\nHow are you doing today?')

varr = StringVar()
entryfield = Pmw.EntryField(root,labelpos=W,label_text='Name:',entry_width=22,entry_textvariable=varr)
entryfield.grid(row=0,column=0,sticky=W,padx=5,pady=5)

button = Button(root,text='Status',command=status)
button.grid(row=0,column=0,sticky=E,padx=5,pady=5,ipadx=10)

var = IntVar()
f = Frame(root)
f.grid()
for i,k,j in [('Male',1,0),('Female',1,1)]:
    radiobutton = Radiobutton(f,text=i,variable=var,value=i,indicatoron=0)
    radiobutton.grid(row=k,column=j,sticky=W,padx=5,pady=5,ipadx=30)
frame = Frame(root)
frame.grid()
text = Text(frame,width=30,height=5)
text.grid(row=2,column=0,padx=5,pady=5)
root.mainloop()
Please, how can I click on either of the RadioButtons and then the 'Status' Button and get the appropriate text inserted into my Text widget.
I tried doing it but I got this error: _tkinter.TclError: expected floating-point number but got "Male".
Thanks.
Reply
#2
You can attach a "command=" to the radiobutton which would make a status button unnecessary. You can insert the appropriate text based on the number of the button clicked. An example from my toolbox.
import tkinter

class CrudGUI:
    def __init__(self, master):
        self.master = master

        self.radio_var = tkinter.IntVar()
        self.radio_var.set(1)

        # create radio buttons

        self.look = tkinter.Radiobutton(self.master, text="Look up customer",
                                        variable=self.radio_var, value=1,
                                        command=self.open_menu)
        self.add = tkinter.Radiobutton(self.master, text="Add a customer",
                                       variable=self.radio_var, value=2,
                                       command=self.open_menu)
        self.change = tkinter.Radiobutton(self.master, text="Change customer information",
                                       variable=self.radio_var, value=3,
                                       command=self.open_menu)
        self.delete = tkinter.Radiobutton(self.master, text="Delete a customer",
                                          variable=self.radio_var, value=4,
                                          command=self.open_menu)

        # pack radio buttons

        self.look.pack(anchor='w', padx=20)
        self.add.pack(anchor='w', padx=20)
        self.change.pack(anchor='w', padx=20)
        self.delete.pack(anchor='w', padx=20)

        self.quit_button = tkinter.Button(self.master, text="QUIT",
                           command=self.master.quit, bg="orange")
        self.quit_button.pack(side='left', expand="yes")

    def open_menu(self):
        print(self.radio_var.get())

root=tkinter.Tk()
VF=CrudGUI(root)
root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,196 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  How to dynamically change radiobutton text kenwatts275 2 3,282 Mar-05-2021, 02:25 AM
Last Post: deanhystad
  Python3 tkinter radiobutton problem Nick_tkinter 14 5,828 Feb-15-2021, 11:01 PM
Last Post: Nick_tkinter
  [Tkinter] RadioButton Maryan 2 2,127 Oct-23-2020, 09:36 PM
Last Post: Maryan
  [Tkinter] How to create radiobutton in numpy gui python? luthfidali 2 2,567 May-23-2020, 10:35 AM
Last Post: timo
  Tkinter: increasing numbers and Radiobutton issue PeroPuri 1 2,118 Apr-13-2020, 05:48 PM
Last Post: deanhystad
  Need tkinter help with clicking buttons pythonprogrammer 2 2,398 Jan-03-2020, 04:43 AM
Last Post: joe_momma
  [PyQt] how to deselect radiobutton in pyqt5 atlass218 3 12,587 Oct-23-2019, 03:25 PM
Last Post: Denni
  [Tkinter] RE: status bar to return to the centre after 1 minute of clicking a button ? chano 6 4,606 May-27-2019, 04:24 PM
Last Post: Yoriz
  [Tkinter] Adding New TAB to NoteBook Widget by Clicking Vicolas 0 2,582 Feb-15-2019, 06:03 PM
Last Post: Vicolas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020