Python Forum
[Tkinter] Radio Buttons Working Bassackwards - 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] Radio Buttons Working Bassackwards (/thread-35728.html)



Radio Buttons Working Bassackwards - gw1500se - Dec-06-2021

I have the following radio buttons defined (sanitized):

myPhone=tk.StringVar()
tk.Radiobutton(top,text='123-456-7932',variable=myPhone,value='123-456-7932').grid(row=rw,column=0,pady=5,columnspan=2)
tk.Radiobutton(top,text='123-456-6305',variable=myPhone,value='123-456-6305').grid(row=rw,column=2,pady=5,columnspan=2)
tk.Radiobutton(top,text='123-456-7477',variable=myPhone,value='123-456-7477').grid(row=rw,column=4,pady=5,columnspan=2)
myPhone.set('123-456-7932')
When they are displayed the 2nd and 3rd buttons show selected. In my mind that should be impossible as 'myPhone' can only have 1 value. It does not matter which number I 'set', the display is the same. What am I doing wrong?


RE: Radio Buttons Working Bassackwards - menator01 - Dec-06-2021

Your code is working on my box. You could try setting the tristatevalue to None.


RE: Radio Buttons Working Bassackwards - deanhystad - Dec-06-2021

Please provide runnable examples. What you leave out may be what you got wrong.

I rewrote you code as this:
import tkinter as tk

phone_numbers = ('123-456-7932', '123-456-6305', '123-456-7477')

root = tk.Tk()
phone_var = tk.StringVar(root, phone_numbers[0])
for phone in phone_numbers:
    tk.Radiobutton(root, text=phone, variable=phone_var, value=phone).pack()

phone_entry = tk.StringVar(root, '')
entry = tk.Entry(root, textvariable=phone_entry)
entry.pack()
entry.bind('<Return>', lambda x: phone_var.set(phone_entry.get()))

root.mainloop()
I added an entry so I can type in phone numbers and see if they phone_var is working as I expect.
When I run this code it appears to


RE: Radio Buttons Working Bassackwards - gw1500se - Dec-07-2021

Reproducing the problem with a simple program is non-trivial. However, I found out what the problem is but I don't know why or how to fix it. Prior to displaying the above UI, I also display a message panel with this code:

def msgPanel(msg):
    panel=tk.Tk()
    panel.title('Do Not Call Interface')
    panel.geometry('200x100')
    tk.Label(panel,text=msg).place(relx=.5,rely=.5,anchor=tk.CENTER)
    panel.update()
    return(panel)
.
.
.
intro=msgPanel('Copy line from Phonetray panel')
.
<some pywin32 code>
.
intro.destroy()
.
<some selenium code>
.
<the code to produce the problem UI>
When I comment out the 'msgPanel' code the UI works as expected. Somehow, the 'msgPanel' code is doing something to the problem UI even though it has not yet been initialized or built.


RE: Radio Buttons Working Bassackwards - deanhystad - Dec-07-2021

You can only use Tk() once in a program. This command initializes tkinter in addition to creating a window. In your msgPanel() function you should create a messagebox instead of using Tk().

https://docs.python.org/3/library/tkinter.messagebox.html

An alternative is to create an additional Toplevel window using tkinter.Toplevel()

https://www.geeksforgeeks.org/python-tkinter-toplevel-widget/


RE: Radio Buttons Working Bassackwards - gw1500se - Dec-07-2021

Ah!! I thought you needed Tk() for each window. Thanks.


RE: Radio Buttons Working Bassackwards - menator01 - Dec-07-2021

This link has example of a toplevel window if it helps.