我一直在尝试使用#!/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()
制作Radiobutton的选择菜单。
不幸的是,发送的变量订单并不像以前那样保持:Easy,Medium,Hard,Extreme。而且,即使我将v设置为特定值,也会立即选择所有选项。
我在这里错过了什么吗?
dic1 = {
'a':2,
'b':3
}
dic2 = {
'a':2,
'b':3,
'c':5
}
...
答案 0 :(得分:1)
您正在使用v
的局部变量,该函数在函数退出时会收集垃圾。您需要保留永久性参考:
def qChoice(self, **kwargs):
self.v = Intvar()
...
在旁注中,您不需要两个import语句。使用其中一个,但不是两个。理想情况下,使用第一个:
import Tkinter as tk
答案 1 :(得分:1)
您的IntVar()
是本地的,并且是被盗的。
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)
类似主题: