1

I am using Python 2.7.6

I just started working with Tkinter recently and I am in the process of turning a command prompt quiz into a GUI quiz. So far I have been successful except when it comes to the radio buttons I am using. I can create a group of radio buttons and pull their values when need be, but I am uncertain how, if possible, to update the text and values of the radio buttons after I click the "Next" button.

This isn't my whole code since it is over 800 lines, but here is where I create the radio buttons and the watered down version of the function def Guide(): where I want to update the radio buttons.

Assume Guide_list[Current].R1 and Guide_list[Current].V1 retrieve the proper string and integer respectively.

"Current" is my counter.

from Tkinter import *
app=Tk()

Current=0
R=IntVar()
R.set(None)
Resp1=Radiobutton(app, text=Guide_list[Current].R1, value=Guide_list[Current].V1,variable=R).pack(anchor=W)
Resp2=Radiobutton(app, text=Guide_list[Current].R2, value=Guide_list[Current].V2,variable=R).pack(anchor=W)
Resp3=Radiobutton(app, text=Guide_list[Current].R3, value=Guide_list[Current].V3,variable=R).pack(anchor=W)
Resp4=Radiobutton(app, text=Guide_list[Current].R4, value=Guide_list[Current].V4,variable=R).pack(anchor=W)
Resp5=Radiobutton(app, text=Guide_list[Current].R5, value=Guide_list[Current].V5,variable=R).pack(anchor=W)
Resp6=Radiobutton(app, text="N/A", value="N/A",variable=R).pack(anchor=W)
def Guide():
    global Current
    Current=Current+1
    ### Insert code to update the radio buttons ###

button2=Button(app,text="Next", width=15,command=Guide)
button2.pack(side='bottom')

app.mainloop()
Hooked
  • 84,485
  • 43
  • 192
  • 261
  • 1
    Hey, can I ask if you are using any tutorials to learn Tkinter? I see a lot of people using the same "assignment+pack antipattern", and I'm trying to track down where they're all getting it from. – Kevin Sep 20 '14 at 15:17
  • I was using various tutorials I found on Youtube. – Der Pysiker Sep 23 '14 at 14:58

1 Answers1

1

You can modify the attributes of a widget using the config method.

    def Guide():
        global Current
        Current=Current+1
        Resp1.config(text="Hello")

However, you can only do this if you still have an existing reference to the widget you're trying to config. As your code is now, you don't have this; Resp1 through Resp6 are all None, because you pointed them to the return value of .pack instead of the actual Radiobuttons. See also Why is None returned instead of tkinter.Entry object. You need to pack on a separate line.

Resp1=Radiobutton(app, text=Guide_list[Current].R1, value=Guide_list[Current].V1,variable=R)
Resp2=Radiobutton(app, text=Guide_list[Current].R2, value=Guide_list[Current].V2,variable=R)
Resp3=Radiobutton(app, text=Guide_list[Current].R3, value=Guide_list[Current].V3,variable=R)
Resp4=Radiobutton(app, text=Guide_list[Current].R4, value=Guide_list[Current].V4,variable=R)
Resp5=Radiobutton(app, text=Guide_list[Current].R5, value=Guide_list[Current].V5,variable=R)
Resp6=Radiobutton(app, text="N/A", value="N/A",variable=R)

Resp1.pack(anchor=W)
Resp2.pack(anchor=W)
Resp3.pack(anchor=W)
Resp4.pack(anchor=W)
Resp5.pack(anchor=W)
Resp6.pack(anchor=W)
Community
  • 1
  • 1
Kevin
  • 74,910
  • 12
  • 133
  • 166