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()
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) -
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.varableNam e (for example). All your functions take at least one argument named "self". There is a good example here.
Hope that helps.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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)
|
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...
|
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
| |
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?
|
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...
|
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
|
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)
|
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):
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |