Reputation: 467
I've been trying to make a choice menu with Radiobutton using *kwargs
.
Unfortunately the sent variable order is not kept as wished for: Easy, Medium, Hard, Extreme. And, even though I did set v to a specific value, all the choices are selected at once.
Am I missing something here?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Tkinter as tk
from Tkinter import *
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
def onClick(self, event):
print("Clicked")
def qChoice(self, **kwargs):
v = IntVar()
v.set(1)
for key, value in kwargs.iteritems():
self.Rbutton = Radiobutton(text=key, variable=v, value=value)
self.Rbutton.grid(sticky=W)
def basics(self):
self.label = Label(text="Enter name:")
self.label.grid(column=0, row=0, sticky="E")
self.entry = Entry()
self.entry.grid(column=1, row=0)
self.button = Button(text="Enter")
self.button.bind("<Button-1>", self.onClick)
self.button.grid(column=3, row=0)
self.qChoice(Easy=1,Medium=2,Hard=3,Extreme=4)
if __name__ == "__main__":
root = tk.Tk()
App = MainApplication(root)
App.basics()
root.mainloop()
Upvotes: 1
Views: 1795
Reputation: 3848
Your IntVar()
is local and is garbaged.
def qChoice(self, **kwargs):
# changed to self.v from v
self.v = IntVar()
# .set(None) leaves all the self.v instances blank
# if you did .set('1'), the first one will be auto-selected
# you can also remove this line if you want them to start blank
self.v.set(None)
for key, value in kwargs.iteritems():
self.Rbutton = Radiobutton(text=key, variable=self.v, value=value)
self.Rbutton.grid(sticky=W)
Similar topics for you:
Upvotes: 2
Reputation: 385910
You are using a local variable for v
which is getting garbage collected when the function exits. You need to keep a permanent reference:
def qChoice(self, **kwargs):
self.v = Intvar()
...
On a side note, you don't need both import statements. Use one or the other, but not both. Ideally, use the first one:
import Tkinter as tk
Upvotes: 1