-1

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.

aek8
  • 319
  • 1
  • 8
  • 22
  • How exactly do you 'set' them? `v.set(1)` doesn't work? – Nae Feb 20 '18 at 14:36
  • Yeah I tried v.set(1) and it does not set it to the option with the value of 1. I also tried setting it with v.set(0) and that doesn't allow me to click any of the buttons either. I also tried setting it within the IntVar() initializer, which also didn't work – wolfschoolwitcher Feb 20 '18 at 14:39
  • 1
    Please provide a [mcve] as opposed to some relevant code with unrelated parts. – Nae Feb 20 '18 at 14:39
  • @Nae I think OP is only missing two import lines, namely `import tkinter as TestBed` and `from tkinter import *`, the latter of which is usually not advised (and I find it odd that `tkinter` actually uses this in their demo) – r.ook Feb 20 '18 at 14:44
  • @Idlehands That's not the point. `v.set(1)` supposed to work in the right scope. So I have to assume how the its written _is_ the issue. So I believe OP needs to isolate the issue as much as possible. – Nae Feb 20 '18 at 14:46
  • I do have import tkinter as TestBed and from tkinter import *, I just didn't include it in the post. I recreated a minimal example and it seems to work and I have it written exactly the same way so the problem must be coming from somewhere else I'm just not sure where – wolfschoolwitcher Feb 20 '18 at 14:54
  • Something's weird, when you remove the `fg='white', bg='navy'` lines in the RadioButton it seems to work fine, but I couldn't reproduce it on a clean copy. – r.ook Feb 20 '18 at 15:02
  • @Nae I played around with it and after removing the fg and bg colors, it worked normally. I'm not sure why adding a foreground and background color would cause this error – wolfschoolwitcher Feb 20 '18 at 15:08
  • @Idlehands I just did that as well. Is there a reason that adding a color would cause it not to work? I'd prefer to have the colors but if I can't use them I guess I'll have to live without them – wolfschoolwitcher Feb 20 '18 at 15:09
  • 1
    @wolfschoolwitcher: the foreground and background colors aren't causing the error because there is no error. The problem is simply that you've modified the visual so that you can't tell whether the buttons are selected or not. They are being selected, you just can't see it because of your color choices. – Bryan Oakley Feb 20 '18 at 15:50

1 Answers1

3

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

r.ook
  • 13,466
  • 2
  • 22
  • 39