Search code examples
pythonpython-3.xtkinterradio-buttontoplevel

Tkinter Python RadioButton not selecting


Hi I am a relatively new developer (about 1 year of java and just started working with python a few weeks ago) and am having trouble getting radio buttons to work on a toplevel window in python. I have searched through different questions and answers on here and tried several of them but none seemed to work. Here is the code that's relevant:

class MPTest(TestBed.Frame):
   def __init__(self, master=NONE):
       TestBed.Frame.__init__(self, master)
       self.createWidgets()

   def createWidgets(self):
      ucThree = Button(root, text='Bids', font='Jokerman', 
                       fg='white', bg='royal blue',
                       command=self.BidWindow)
      ucThree.grid(row=2)

   def BidWindow(self):
       t = TestBed.Toplevel(self)
       t.wm_title("Bid Info")
       t.configure(background="navy")
       v = IntVar()
       v2 = IntVar()
       bidtypelabel = Label(t, text='Bid Type: ', fg='white', bg='navy')
       bidtypelabel.grid(row=0)
       realtime = Radiobutton(t, text='Real Time', variable=v, value=1, 
                             fg='white', bg='navy')
       realtime.grid(row=1)
       priority = Radiobutton(t, text='Priority', variable=v, value=2, 
                             fg='white', bg='navy')
       priority.grid(row=2)
       listsearch = Radiobutton(t, text='List Search', variable=v, value=3, 
                                fg='white', bg='navy')
       listsearch.grid(row=3)
       bidactionlabel = Label(t, text='Action: ', fg='white', bg='navy')
       bidactionlabel.grid(row=0, column=1)
       acceptbid = Radiobutton(t, text='Accept Bid', variable=v2, value=1, 
                               fg='white', bg='navy')
       acceptbid.grid(row=1, column=1)
       rejectbid = Radiobutton(t, text='Reject Bid', variable=v2, value=2, 
                               fg='white', bg='navy')
       rejectbid.grid(row=2, column=1)
       submit = Button(t, text='Submit', fg='white', bg='royal blue')
       submit.grid(row=4, column=2)


root = TestBed.Tk()
root.configure(background="navy")
root.rowconfigure((0, 1, 2, 3, 4, 5, 6, 7, 8, 9), weight=1, pad=50)
root.columnconfigure(1, weight=1, pad=200)
app = MPTest(master=root)

app.mainloop()

I have tried setting the variables equal to both 0 and 1, both inside the IntVar() and then also tried setting it after with a set on the next line. However, neither of these allow the Radio Buttons to be selectable and setting them as 1 (a value that is already assigned) does not result in the first option being selected upon opening the window. I have also tried setting the master for the variables to both t (TopLevel) and TestBed. Nothing I try seems to work. Sometimes hovering over them will select all of them, which seems glitchy. However, when I click them they do not stay selected no matter what I try based on answers I found here and on other sites. I am new to Python so I'm sorry if I'm doing something blatantly stupid or wrong but any help would be appreciated.


Solution

  • The issue is the fg='white' param collides with the radio button's background colour (I'm seeing white, which I assume is the same case for you). The selection IS happening, it's just drawing a white dot on white background so you don't see it.

    To remedy, add the following param in each of your radio button:

    selectcolor='navy'
    # or any colour of your preference that highlights the white dot
    

    Now your radio button will have the same background colour and the white will stand out.

    Reference thread