P: 1
|
I cant make this code work. I am new at Python, and in this program I have made a simple mastermind program.
After creating the widgets, I try to insert text in the textbox in a function but all I get is "NameError: global name 'display_txt' is not defined" for the textbox I have created earlier in the widgets
What am I missing???!!!!
Here is some code (part of it) -
def display_instruct():
-
message = """This is a master mind game """
-
display_txt.delete(0.0, END)
-
display_txt.insert(0.0, message)
-
-
-
def start():
-
display_instruct()
-
message="Hello"
-
display_txt.delete(0.0, END)
-
display_txt.insert(0.0, message)
-
-
-
def makeWindow():
-
root = Tk()
-
root.title("MasterMind game")
-
root.geometry("500x500")
-
-
#Start button
-
b1=Button(root, text="start game",command=start)
-
b1.grid(row = 1, column = 0, sticky = W)
-
-
-
#reply button
-
b1=Button(root, text="answer",command=start)
-
b1.grid(row = 1, column = 2, sticky = W)
-
-
-
#output window
-
display_txt=Text(root,width = 50, height = 26, wrap = WORD)
-
display_txt.grid(row = 0, column = 0, columnspan = 2, sticky = W)
-
-
#Entry filed
-
svar_ent = Entry()
-
svar_ent.grid(row = 1, column = 1, sticky = W)
-
return root
-
-
-
# main
-
makeWindow()
-
-
mainloop()
| |
Share this Question
Expert 5K+
P: 6,596
|
I cant make this code work. I am new at Python, and in this program I have made a simple mastermind program.
After creating the widgets, I try to insert text in the textbox in a function but all I get is "NameError: global name 'display_txt' is not defined" for the textbox I have created earlier in the widgets
What am I missing???!!!!
Here is some code (part of it) -
def display_instruct():
-
message = """This is a master mind game """
-
display_txt.delete(0.0, END)
-
display_txt.insert(0.0, message)
-
-
-
def start():
-
display_instruct()
-
message="Hello"
-
display_txt.delete(0.0, END)
-
display_txt.insert(0.0, message)
-
-
-
def makeWindow():
-
root = Tk()
-
root.title("MasterMind game")
-
root.geometry("500x500")
-
-
#Start button
-
b1=Button(root, text="start game",command=start)
-
b1.grid(row = 1, column = 0, sticky = W)
-
-
-
#reply button
-
b1=Button(root, text="answer",command=start)
-
b1.grid(row = 1, column = 2, sticky = W)
-
-
-
#output window
-
display_txt=Text(root,width = 50, height = 26, wrap = WORD)
-
display_txt.grid(row = 0, column = 0, columnspan = 2, sticky = W)
-
-
#Entry filed
-
svar_ent = Entry()
-
svar_ent.grid(row = 1, column = 1, sticky = W)
-
return root
-
-
-
# main
-
makeWindow()
-
-
mainloop()
What you want to do is make a subclass of frame so that Tk has something to show. All actions taking place in this frame belong in your subclass: -
class MyFrame(Frame):
-
"""A subclass of Tkinter.Frame."""
-
def __init__(self, root, *args, **kwargs):
-
Frame.__init__(self, root, *args, **kwargs)
-
self.makeWindow(root)
-
-
-
def makeWindow(self, root):
-
#Start button
-
b1=Button(root, text="start game",command=self.start)
-
b1.grid(row = 1, column = 0, sticky = W)
-
-
-
#reply button
-
b1=Button(root, text="answer",command=start)
-
b1.grid(row = 1, column = 2, sticky = W)
-
-
-
#output window
-
self.display_txt=Text(root,width = 50, height = 26, wrap = WORD)
-
self.display_txt.grid(row = 0, column = 0, columnspan = 2, sticky = W)
-
-
#Entry filed
-
self.svar_ent = Entry()
-
self.svar_ent.grid(row = 1, column = 1, sticky = W)
-
-
def display_instruct(self):
-
message = """This is a master mind game """
-
self.display_txt.delete(0.0, END)
-
self.display_txt.insert(0.0, message)
-
-
def start(self):
-
self.display_instruct()
-
message="Hello"
-
self.display_txt.delete(0.0, END)
-
self.display_txt.insert(0.0, message)
-
-
if __name__ == "__main__": # we do this so that the subclass can be used in other programs by importing it.
-
-
root = Tk()
-
root.title("MasterMind game")
-
root.geometry("500x500")
-
theFrame = MyFrame(root)
-
theFrame.pack()
-
mainloop()
I don't have my IDE on this machine, so I wont try to indent all your code for you, sorry. With a subclass, all variables that are shared (global, but in a limited scope) in the frame get named self.varableName (for example). All your functions take at least one argument named "self". There is a good example here.
Hope that helps.
| | | | Question stats - viewed: 2087
- replies: 1
- date asked: May 8 '07
|