在Python中调试RadioButtons程序

时间:2011-06-20 18:30:05

标签: python debugging radio-button tkinter

我写了一个简单的程序导入Tkinter只是为了播放Radio Buttons。我发现我在非常非常奇怪的地方遇到错误。

 from Tkinter import *

class Application (Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):


        Label(self, text = "Select the last book you read.").grid (row = 0, column = 0, sticky = W)

        self.choice = StringVar() 

        Radiobutton (self,text = "Nausea by Jean-Paul Sartre",variable = self.choice,
                 value = "Wake up. This is a dream. This is all only a test of the emergency broadcasting system.",
                 command = self.update_text).grid (row = 2, column = 1, sticky = W)

        Radiobutton (self,
                 text = "Infinite Jest by David Foster Wallace",
                 variable = self.choice,
                 value = "Because an adult borne without the volition to choose the thoughts that he thinks, is going to get hosed ;)",
                 command = self.update_text).grid (row = 3, column = 1, sticky = W)

        Radiobutton (self,
                 text = "Cat's Cradle by Kurt Vonnegut",
                 variable = self.choice,
                 value = " \"Here we are, trapped in the amber of the moment. There is no why!\" ",
                 command = self.update_text.grid (row = 4, column = 1, sticky = W)

        self.txt_display = Text (self, width = 40, height = 5, wrap = WORD)
        self.txt_display.grid (row = 6, column = 0, sticky = W)


    #There is only one choice value - self.choice. That can be "printed."

    def update_text(self):
        message = self.choice.get()
        self.txt_display.delete (0.0, END)
        self.txt_display.insert (0.0, message)

# The Main
root = Tk()
root.title ("The Book Critic One")
root.geometry ("400x400")

app = Application (root)
root.mainloop()

我似乎在非常奇怪的地方遇到错误。其中一个出现在Label归属中的“=”符号中,当我在玩游戏时将其更改为==时,下一个符号出现在RadioButton属性的变量部分中。

非常感谢任何帮助。我不能立即回应,因为我必须稍微离开工作,但如果你确实发现了虫子的位置,请告诉我。

1 个答案:

答案 0 :(得分:2)

这里有很多事情要发生。我只想指出一些我很快发现的事情。

对于您的Label,您不应该在参数之前= ...

Label = (self, text = "Select the last book you read.").grid (row = 0, column = 0, sticky = W)

为:

Label(self, text = "Select the last book you read.").grid (row = 0, column = 0, sticky = W)

RadioButton的所有实例更改为Radiobutton,因为这是Tkinter中类的实际名称。

choice1中不存在

choice2choice3Application

更多资料:

def create_widgets()缺少self参数:def create_widgets(self)

您的update_text()功能无法正常工作,因为您引用了self.text_display,我相信您希望这是self.txt_display,因为这是您之前定义的方式。