473,803 Members | 4,195 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Somebody help with Tkinter Frame subclass

1 New Member
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 2641
bartonc
6,596 Recognized Expert Expert
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.varableNam e (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

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

Similar topics

1
5909
by: Thomas Nücker | last post by:
Hi! I am creating a dialog-box within my application, using tkinter. The problem is the following: After the dialogbox is started, the main application window comes again to top and the dialogbox is covered by the window of the main application and must be "fetched" again via the taskbar to continue. Is there a way to "force" the dialogbox on top of all other windows? (I'm using MSWindows and Python22) The source of my dialogbox-class...
2
2104
by: James Ash | last post by:
I'm writing a very simple and small Ptyhon/Tkinter application and I'm having trouble getting the menus to appear correctly. Rather than a name appearing on the menu bar, I see "()" instead. Clicking on these "()" does nothing (other than changing the appearance of them to indicated they've been pressed). I'm using Python 2.2.3 on Win2K, using a release downloaded from one of the Cygwin mirrors. This is most likely a simple mistake...
2
3251
by: Paul A. Wilson | last post by:
I'm new to Tkinter programming and am having trouble creating a reusable button bar... I want to be able to feed my class a dictionary of button names and function names, which the class will make. My button bar is implemented a Frame subclass, which takes the button dictionary as an argument and displays the buttons on the screen: class OptionsBar(Frame): def __init__(self, buttonDict, parent=None) Frame.__init__(self, parent)
0
3589
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you shrink/resize the window the scrollbars adjust accordingly. However, what I really want to happen is that the area of the canvas that the scrollbars show (the Scrollregion) should expand as the window grows. It doesn't currently do this. although, if the window shrinks smaller than the...
1
2665
by: C D Wood | last post by:
To whom this may concern, Below is the source code, which demonstrates a problem I am having making a GUI for my python project work. 'table.txt' is a file that is read from the same folder. My code writes to a text file 'table.txt', and 'table.txt' is displayed in
1
3603
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that I am hoping someone knows what I am doing wrong: 1) Pressing the Settings.. Button multiple times, brings up many instances of the Settings Panel. I just want it to bring up one. Is there an easy way to do that?
3
3613
by: dwelch91 | last post by:
I'm trying unsuccessfully to do something in Tk that I though would be easy. After Googling this all day, I think I need some help. I am admittedly very novice with Tk (I started with it yesterday), so I am sure I am overlooking something simple. The basic idea is that my application will consist of a series of modal dialogs, that are chained together in "wizard" fashion. There will be some processing in between dialogs, but for the most...
6
2448
by: JyotiC | last post by:
hi, i am making a GUI using Tkinter, I have a button and a checkbutton. i want the button to be enable when checkbutton is on and disble when the checkbutton is off. thanx
6
1888
by: Eric_Dexter | last post by:
Instead of creating my buttons and waiting for me to press them to execute they are executing when I create them and won't do my callback when I press them.. thanks for any help in advance button = Tkinter.Button(frame,text = returnstring, command=callback(returnstring))# this line executes on creation my output on startup is (should output when I choose an option)
1
5127
by: vigacmoe | last post by:
Hi all, I'm trying to write a simple tkinter program, then this problem popped up. The followin code will describe the problem. ------------------------------------------ import Tkinter class countdown(Tkinter.Frame):
0
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10546
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10292
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.