所以我正在尝试构建一个侮辱生成器,它将获取列表,随机化输入,并在按下按钮时显示随机代码。
现在,代码看起来像......
import Tkinter
import random
section1 = ["list of stuff"]
section2 = ["list of stuff"]
section3 = ["list of stuff"]
class myapp(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid() # creates grid layout manager where we can place our widgets within the window
button = Tkinter.Button(self, text=u"Generate!", command=self.OnButtonClick)
button.grid(column=1, row=0)
self.labelVariable = Tkinter.StringVar()
label = Tkinter.Label(self, textvariable=self.labelVariable, anchor='w', fg='white', bg='green')
label.grid(column=0, row=1, columnspan=2, sticky='EW')
self.labelVariable.set(u"Oh hi there !")
self.grid_columnconfigure(0, weight=1)
self.resizable(True, False)
self.update()
self.geometry(self.geometry())
def generator():
a = random.randint(0, int(len(section1))-1)
b = random.randint(0, int(len(section2))-1)
c = random.randint(0, int(len(section3))-1)
myText = "You are a "+ section1[a]+" "+section2[b]+'-'+section3[c]+"! Fucker."
return myText
def OnButtonClick(self):
self.labelVariable.set(myText + "(You clicked the button !)")
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
if __name__=='__main__':
app = myapp(None) # Instantiates the class
app.title('Random Insult Generator') # Names the window we're creating.
app.mainloop() # Program will loop indefinitely, awaiting input
现在,它给出的错误myText
是未定义。关于如何解决它的任何想法?
编辑: 错误消息是...
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1470, in __call__
return self.func(*args)
File "...", line 41, in OnButtonClick
self.labelVariable.set(myText+"(You clicked the button !)")
NameError: global name 'myText' is not defined