[英]Debugging RadioButtons program in Python
我寫了一個簡單的程序導入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屬性的變量部分中。
任何幫助將不勝感激。 因為我必須稍微離開工作,所以無法立即回復,但如果您確實發現了錯誤的位置,請告訴我。
這里有很多事情要發生。 我只想指出一些我很快發現的事情。
對於你的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
, choice2
,和choice3
不存在Application
。
更多東西:
def create_widgets()
缺少self
參數: def create_widgets(self)
你的update_text()
函數無法正常工作,因為你引用的是self.text_display
,我相信你希望它是self.txt_display
因為這是你之前定義它的方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.