Python Forum

Full Version: Radio Buttons Working Bassackwards
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
Your code is working on my box. You could try setting the tristatevalue to None.
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
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.
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/tkinte...gebox.html

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

https://www.geeksforgeeks.org/python-tki...el-widget/
Ah!! I thought you needed Tk() for each window. Thanks.
This link has example of a toplevel window if it helps.