468,305 Members | 1,472 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,305 developers. It's quick & easy.

Somebody help with Tkinter Frame subclass

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)
Expand|Select|Wrap|Line Numbers
  1. def display_instruct():
  2.     message = """This is a master mind game """
  3.     display_txt.delete(0.0, END)
  4.     display_txt.insert(0.0, message)
  5.  
  6.  
  7. def start():
  8.       display_instruct()
  9.       message="Hello"
  10.       display_txt.delete(0.0, END)
  11.       display_txt.insert(0.0, message)
  12.  
  13.  
  14. def makeWindow():
  15.       root = Tk()
  16.       root.title("MasterMind game")
  17.       root.geometry("500x500")
  18.  
  19.     #Start button
  20.       b1=Button(root, text="start game",command=start)
  21.       b1.grid(row = 1, column = 0, sticky = W)
  22.  
  23.  
  24.     #reply button
  25.       b1=Button(root, text="answer",command=start)
  26.       b1.grid(row = 1, column = 2, sticky = W)
  27.  
  28.  
  29.     #output window
  30.       display_txt=Text(root,width = 50, height = 26, wrap = WORD)
  31.       display_txt.grid(row = 0, column = 0, columnspan = 2, sticky = W)
  32.  
  33.      #Entry filed
  34.       svar_ent = Entry()
  35.       svar_ent.grid(row = 1, column = 1, sticky = W)
  36.       return root
  37.  
  38.  
  39. # main
  40. makeWindow()
  41.  
  42. mainloop()
May 8 '07 #1
1 2299
bartonc
6,596 Expert 4TB
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)
Expand|Select|Wrap|Line Numbers
  1. def display_instruct():
  2.     message = """This is a master mind game """
  3.     display_txt.delete(0.0, END)
  4.     display_txt.insert(0.0, message)
  5.  
  6.  
  7. def start():
  8.       display_instruct()
  9.       message="Hello"
  10.       display_txt.delete(0.0, END)
  11.       display_txt.insert(0.0, message)
  12.  
  13.  
  14. def makeWindow():
  15.       root = Tk()
  16.       root.title("MasterMind game")
  17.       root.geometry("500x500")
  18.  
  19.     #Start button
  20.       b1=Button(root, text="start game",command=start)
  21.       b1.grid(row = 1, column = 0, sticky = W)
  22.  
  23.  
  24.     #reply button
  25.       b1=Button(root, text="answer",command=start)
  26.       b1.grid(row = 1, column = 2, sticky = W)
  27.  
  28.  
  29.     #output window
  30.       display_txt=Text(root,width = 50, height = 26, wrap = WORD)
  31.       display_txt.grid(row = 0, column = 0, columnspan = 2, sticky = W)
  32.  
  33.      #Entry filed
  34.       svar_ent = Entry()
  35.       svar_ent.grid(row = 1, column = 1, sticky = W)
  36.       return root
  37.  
  38.  
  39. # main
  40. makeWindow()
  41.  
  42. 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:
Expand|Select|Wrap|Line Numbers
  1. class MyFrame(Frame):
  2.     """A subclass of Tkinter.Frame."""
  3.     def __init__(self, root, *args, **kwargs):
  4.         Frame.__init__(self, root, *args, **kwargs)
  5.         self.makeWindow(root)
  6.  
  7.  
  8.       def makeWindow(self, root):
  9.           #Start button
  10.             b1=Button(root, text="start game",command=self.start)
  11.             b1.grid(row = 1, column = 0, sticky = W)
  12.  
  13.  
  14.           #reply button
  15.             b1=Button(root, text="answer",command=start)
  16.             b1.grid(row = 1, column = 2, sticky = W)
  17.  
  18.  
  19.           #output window
  20.             self.display_txt=Text(root,width = 50, height = 26, wrap = WORD)
  21.             self.display_txt.grid(row = 0, column = 0, columnspan = 2, sticky = W)
  22.  
  23.            #Entry filed
  24.             self.svar_ent = Entry()
  25.             self.svar_ent.grid(row = 1, column = 1, sticky = W)
  26.  
  27.     def display_instruct(self):
  28.         message = """This is a master mind game """
  29.         self.display_txt.delete(0.0, END)
  30.         self.display_txt.insert(0.0, message)
  31.  
  32.       def start(self):
  33.             self.display_instruct()
  34.             message="Hello"
  35.             self.display_txt.delete(0.0, END)
  36.             self.display_txt.insert(0.0, message)
  37.  
  38. if __name__ == "__main__":    # we do this so that the subclass can be used in other programs by importing it.
  39.  
  40.     root = Tk()
  41.     root.title("MasterMind game")
  42.     root.geometry("500x500")
  43.     theFrame = MyFrame(root)
  44.     theFrame.pack()
  45.     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.
May 8 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Thomas Nücker | last post: by
2 posts views Thread by James Ash | last post: by
2 posts views Thread by Paul A. Wilson | last post: by
reply views Thread by syed_saqib_ali | last post: by
1 post views Thread by C D Wood | last post: by
1 post views Thread by Michael Yanowitz | last post: by
3 posts views Thread by dwelch91 | last post: by
6 posts views Thread by JyotiC | last post: by
6 posts views Thread by Eric_Dexter | last post: by
1 post views Thread by vigacmoe | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.