我有以下代码:
class Test:
def __init__(self, master): # master is a Tk or Toplevel instance
self.master, self.typeFrame = master, tk.Frame(master)
self.typeVar = tk.StringVar(self.typeFrame)
for column, wordType in enumerate(['Noun', 'Verb', 'Adjective', 'Adverb'], 1):
typeRadioButton = tk.Radiobutton(self.typeFrame, text = wordType, textvariable = self.typeVar, value = wordType, command = self.createLambda(wordType))
typeRadioButton.grid(row = 1, column = column)
self.typeFrame.grid()
def createLambda(self, obj):
return lambda: self.changeInfo(obj)
def changeInfo(self, obj):
pass # this will do something later
但是,当我运行这样的代码时,Radiobuttons没有与之关联的文本。
root = tk.Tk()
test_instance = Test(root)
test_instance.master.mainloop()
我该如何解决这个问题?提前谢谢!
答案 0 :(得分:2)
将textvariable=...
更改为variable=...
。
顺便说一句,您的示例不包含self.typeFrame.pack()
或self.typeFrame.grid(..)
。