1

我有以下代码:

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

但是,当我像这样运行代码时,单选按钮没有与之关联的文本。

root = tk.Tk()
test_instance = Test(root)
test_instance.master.mainloop()

我怎样才能解决这个问题?提前致谢!

4

1 回答 1

2

更改textvariable=...variable=...

顺便说一句,您的示例不包含self.typeFrame.pack()or self.typeFrame.grid(..)

于 2013-06-17T03:03:11.417 回答