Python: Trying to fix my insult generator
So I'm trying to build an insult generator that will take lists, randomize
the inputs, and show the randomized code at the push of a button.
Right now, the code looks like...
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) ##instanciates the class
app.title('Random Insult Generator') ##names the window we're creating.
app.mainloop() ##Program will loop indefinitely, awaiting input
Right now, the error it's giving is that the myText isn't defined. Have
any thoughts on how to fix it? Thanks in advance for your help!
Edit: The error message is...
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
No comments:
Post a Comment