Python Forum
[Tkinter] Radio Buttons Working Bassackwards
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Radio Buttons Working Bassackwards
#1
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?
Reply
#2
Your code is working on my box. You could try setting the tristatevalue to None.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
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
Reply
#4
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.
Reply
#5
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/
Reply
#6
Ah!! I thought you needed Tk() for each window. Thanks.
Reply
#7
This link has example of a toplevel window if it helps.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Is there a way to determine if a radio button has been selected? TWB 5 5,026 Jan-31-2023, 09:44 AM
Last Post: Vadanane
  tkinter toggle buttons not working Nu2Python 26 6,994 Jan-23-2022, 06:49 PM
Last Post: Nu2Python
  [Tkinter] Grid the radio buttons Joni_Engr 6 4,778 Nov-24-2021, 07:20 PM
Last Post: menator01
  Radio butto to enable/disable combo box in Tkinter cybertooth 5 5,525 Oct-09-2021, 07:30 AM
Last Post: cybertooth
  problem with radio button crook79 3 3,712 Aug-12-2021, 02:30 PM
Last Post: deanhystad
  [Tkinter] I can't get information from a radio button aquerci 2 2,756 May-20-2020, 10:31 AM
Last Post: aquerci
  [Tkinter] Radio button help Muzz 5 3,691 Apr-28-2019, 07:43 AM
Last Post: Muzz

Forum Jump:

User Panel Messages

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