Python Forum
[Tkinter] Clicking a RadioButton in a for Loop & Getting the Appropriate Return - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Clicking a RadioButton in a for Loop & Getting the Appropriate Return (/thread-15824.html)



Clicking a RadioButton in a for Loop & Getting the Appropriate Return - Vicolas - Feb-02-2019

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.



RE: Clicking a RadioButton in a for Loop & Getting the Appropriate Return - woooee - Feb-02-2019

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()