473,564 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter programming problem

Could someone help me out with these few lines of code: I would like
to know why the Quit button in this application removes the buttons
and causes "Quitting" to be printed, but does not close the outer
frame.

Andrew.
# Demonstration TK interface Windows application
# Runs ok from within IDLE
#
from Tkinter import *

class CommonStuff: # to get common access to variables and functions
def __init__(cself, frame):
cself.frame = frame

def say_hi(cself):
print "Hello all"
class MyWidgets(Frame , CommonStuff):
def __init__(wself, CS):
Frame.__init__( wself, CS.frame)
wself.quitbutto n = Button(wself)
wself.quitbutto n["text"] = "Quit"
wself.quitbutto n["fg"] = "red"
wself.quitbutto n["command"] = wself.destroy

wself.quitbutto n.pack({"side": "left"})

wself.hi_there = Button(wself)
wself.hi_there["text"] = "Hello",
wself.hi_there["command"] = CS.say_hi

wself.hi_there. pack({"side": "left"})
class Application:
def __init__(self, master):
self.frame=Fram e(master)
CS = CommonStuff(sel f.frame)

displayedwidget =MyWidgets(CS)
displayedwidget .grid(row=0, column=0)
self.frame.grid (row=0, column=0)
self.frame.colu mnconfigure(0)
displayedwidget .bind("<Destroy >", self.quit)
self.frame.upda te()

def quit(self, event):
print"Quitting. .."
self.frame.dest roy # Destroy frame and all children
root = Tk()
mainWin = Application(roo t)
root.wait_windo w(mainWin.frame )
Jul 18 '05 #1
5 7192
Andrew Gregory wrote:
Could someone help me out with these few lines of code: I would like
to know why the Quit button in this application removes the buttons
and causes "Quitting" to be printed, but does not close the outer
frame.

Andrew.
# Demonstration TK interface Windows application
# Runs ok from within IDLE
#
from Tkinter import *

class CommonStuff: # to get common access to variables and functions
def __init__(cself, frame):
cself.frame = frame
It is a Bad Idea to give the first parameter of a method any other name than
"self"... Checking tools like PyChecker will complain if you do that, and your
programs will be harder to read for anyone else doing Python...
def say_hi(cself):
print "Hello all"
class MyWidgets(Frame , CommonStuff):
def __init__(wself, CS):
Frame.__init__( wself, CS.frame)
Where have you found this type of Tkinter object initialization? Apparently,
there are weird style guides lying around somewhere... You can rewrite all of this:
wself.quitbutto n = Button(wself)
wself.quitbutto n["text"] = "Quit"
wself.quitbutto n["fg"] = "red"
wself.quitbutto n["command"] = wself.destroy

wself.quitbutto n.pack({"side": "left"})
like that:

wself.quitbutto n = Button(wself, text='Quit', fg='red', command=wself.d estroy)
wself.quitbutto n.pack(side=LEF T)

This is the most common way to do things. BTW, since you never do anything to
the buttons themselves ouside this method, there's no need at all to store them
in attributes. So you can just do:

quitbutton = Button(wself, text='Quit', fg='red', command=wself.d estroy)
quitbutton.pack (side=LEFT)

or even:

Button(wself, text='Quit', fg='red', command=wself.d estroy).pack(si de=LEFT)
wself.hi_there = Button(wself)
wself.hi_there["text"] = "Hello",
wself.hi_there["command"] = CS.say_hi

wself.hi_there. pack({"side": "left"})
Same here:

wself.hi_there = Button(wself, text="Hello", command=CS.say_ hi)
wself.hi_there. pack(side=LEFT)

or:

hi_there = Button(wself, text="Hello", command=CS.say_ hi)
hi_there.pack(s ide=LEFT)

or even:

Button(wself, text="Hello", command=CS.say_ hi).pack(side=L EFT)

class Application:
def __init__(self, master):
self.frame=Fram e(master)
CS = CommonStuff(sel f.frame)

displayedwidget =MyWidgets(CS)
displayedwidget .grid(row=0, column=0)
self.frame.grid (row=0, column=0)
self.frame.colu mnconfigure(0)
This statement is a no-op: you say you'll configure the column n#0 of
self.frame, but you do not give any features for the column. What are you trying
to do?
displayedwidget .bind("<Destroy >", self.quit)
self.frame.upda te()

def quit(self, event):
print"Quitting. .."
self.frame.dest roy # Destroy frame and all children
Compare this line to the last in the __init__ method just above. To call the
update method on self.frame, you did self.frame.upda te(). So to call the destroy
method on self.frame, you must do self.frame.dest roy(). self.frame.dest roy
merely returns the destroy method of the self.frame object, but doesn't do
anything with it.
root = Tk()
mainWin = Application(roo t)
root.wait_windo w(mainWin.frame )


I don't know if this works, but I know it's not the usual way to run a Tkinter
application. You'd better replace the last line by:

root.mainloop()

HTH

--
- Eric Brunel <er*********@pr agmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #2
Many thanks for such a comprehensive answer.

Altered root.wait_windo w(mainWin.frame ) to root.mainloop()
and found that it runs and closes ok within IDLE. I seem to remember
having crashes on closing within IDLE before.

I did try self.frame.dest roy as the function
self.frame.dest roy(), but the Quit button still didn't work. The
application can be closed via the window corner X, but I'm still
puzzled as to why it does not respond to Quit.

Any more suggestions?
Updated code below, Andrew.

# Demonstration TK interface Windows application
# Runs ok from within IDLE
#
from Tkinter import *

class CommonStuff: # to get common access to variables and functions
def __init__(self, frame):
self.frame = frame

def say_hi(self):
print "Hello all"
class MyWidgets(Frame , CommonStuff):
def __init__(self, CS):
Frame.__init__( self, CS.frame)
self.quitbutton = Button(self, text='Quit', fg='red',
command=self.de stroy)
self.quitbutton .pack(side=LEFT )
self.hi_there = Button(self, text='Hello', command=CS.say_ hi)
self.hi_there.p ack(side=LEFT)
class Application:
def __init__(self, master):
self.frame=Fram e(master)
CS = CommonStuff(sel f.frame)

displayedwidget =MyWidgets(CS)
displayedwidget .grid(row=0, column=0)
self.frame.grid (row=0, column=0)
displayedwidget .bind("<Destroy >", self.quit)
self.frame.upda te()

def quit(self, event):
print"Quitting. .."
self.frame.dest roy() # Destroy frame and all children
root = Tk()
mainWin = Application(roo t)
root.mainloop()
Jul 18 '05 #3
>

# Demonstration TK interface Windows application
# Runs ok from within IDLE
#
from Tkinter import *

class CommonStuff: # to get common access to variables and functions
def __init__(self, frame):
self.frame = frame

def say_hi(self):
print "Hello all"
class MyWidgets(Frame , CommonStuff):
def __init__(self, CS):
Frame.__init__( self, CS.frame)
self.quitbutton = Button(self, text='Quit', fg='red',
command=self.de stroy)
self.quitbutton .pack(side=LEFT )
self.hi_there = Button(self, text='Hello', command=CS.say_ hi)
self.hi_there.p ack(side=LEFT)
class Application:
def __init__(self, master):
self.frame=Fram e(master)
CS = CommonStuff(sel f.frame)

displayedwidget =MyWidgets(CS)
displayedwidget .grid(row=0, column=0)
self.frame.grid (row=0, column=0)
displayedwidget .bind("<Destroy >", self.quit)
self.frame.upda te()

def quit(self, event):
print"Quitting. .."
self.frame.dest roy() # Destroy frame and all children
root = Tk()
mainWin = Application(roo t)
root.mainloop()


I think you could have it easier, if you just want to exit you
application with the quit button:

class MyWidgets(Frame , CommonStuff):
def __init__(self, CS):
Frame.__init__( self, CS.frame)
self.quitbutton = Button(self, text='Quit', fg='red',
command=self.qu it)
self.quitbutton .pack(side=LEFT )
self.hi_there = Button(self, text='Hello', command=CS.say_ hi)
self.hi_there.p ack(side=LEFT)

def quit(self):
print "Quitting.. ."
sys.exit(0)

If you want to run it from within the interpreter (I am not sure if it
is that what you are trying) the following might work too:

class Application:
def __init__(self, master):
self.frame=Fram e(master)
CS = CommonStuff(sel f.frame)

displayedwidget =MyWidgets(CS)
displayedwidget .grid(row=0, column=0)
self.frame.grid (row=0, column=0)
displayedwidget .bind("<Destroy >", self.quit)
self.frame.upda te()

self.master = master

def quit(self, event):
print"Quitting. .."
self.master.des troy() # Destroy root window

However I would recommend to store the code in a file and then run the
file.
I am sorry that I cannot give you more detailed advice, but I am still
a beginner, too. I hope this helped anyway.

Good luck!

Michael
Jul 18 '05 #4
Eric Brunel <er*********@pr agmadev.com> wrote in message news:<bg******* ***@news-reader4.wanadoo .fr>...
This may also work, but the most common way is the one I describe above. If you
want to do it here, you can do:

def quit(self, event):
print "Quitting.. ."
self.master.qui t()

AFAIK, all Tkinter widgets have a quit method that will quit the Tk mainloop.

HTH


I think Tkinter's quit() method will not work while running from
within the interpreter, because there is no mainloop. I think you will
have to destroy() there.

Best regards

Michael
Jul 18 '05 #5
furliz wrote:
I did try self.frame.dest roy as the function self.frame.dest roy(),
but the Quit button still didn't work. The
application can be closed via the window corner X, but I'm still
puzzled as to why it does not respond to Quit.

Any more suggestions?

why don't use 'sys.exit(0)' as command?
Ex:
quitButton = Button(root,tex t="Quit",comman d=sys.exit(0))


Don't do that!! Written like above, sys.exit(0) will be called when the button
is *created*, not when it is pressed! If you want to do that, write:

quitButton = Button(root, text='Quit', command=lambda: sys.exit(0))
--
- Eric Brunel <er*********@pr agmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
639
by: Andrei | last post by:
I've installed Python 2.3b2 on Windows XP (in "e:\programming\Python2.3"). I also have an older ActivePython 2.2.2 distro on my C-drive and Ruby which apparently installs tcl as well. Now I'm trying to run Idle and get this little beauty (similar for other Tkinter apps): ===== E:\Programming\Python2.3\Lib\idlelib>idle.py Traceback (most...
5
4748
by: carljohan.rehn | last post by:
I would like like to start doing some GUI-programming in Python, but don't know which library to choose. Tkinter seems a bit old. Correct me if I am wrong! The layout doesn't look as nice as for the others. wxPython seems to be the first-hand choice for people doing W32-programming (with MFC-experience). PyGtk seems to be a modern,...
2
3233
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...
25
3330
by: BJörn Lindqvist | last post by:
See: http://www.wxpython.org/quotes.php. especially: "wxPython is the best and most mature cross-platform GUI toolkit, given a number of constraints. The only reason wxPython isn't the standard Python GUI toolkit is that Tkinter was there first." - Guido van Rossum Guess, that answers my question, but isn't "Tkinter was there first" a...
8
4474
by: Erik Johnson | last post by:
I am looking for some input on GUI libraries. I want to build a Python-driven GUI, but don't really understand the playing field very well. I have generally heard good things about wxPython. I happen to already own John Grayson's book about Tkinter programming, so that is rather handy if I decide to use Tkinter. I have done some things...
6
8242
by: Richard Lewis | last post by:
Hi there, I've got a tree control in Tkinter (using the ESRF Tree module) but I can't get it to layout how I want it. I'd like to have it so that it streches north/south (anchored to the top and bottom), is of a fixed width and is anchored to the left hand side. Here's my code (its derived from one of the examples from the ESRF web site):
2
2478
by: import newbie | last post by:
Hi all, I'm a programming dabbler trying learn Python, and I've got a few questions. Mainly: Where can I find a good open-source library or tutorial (preferably free) that explains how to easily manipulate text in a tKinter window? Basically, I want to be able to do anything that HTML can do (or close to it) but without the HTML. :-)
1
2004
by: jhujsak | last post by:
Hi, Can anyone point me to any good examples of how to get started developing new Tkinter widgets in Tcl/Tk? I have read a number of the popular books on Python and Tkinter: - Grayson, J.E., Python and Tkinter Programming, Manning Publications, 2000 - Lutz, M., Programming Python, O'Reilly, 1996.
2
1955
by: Kevin Walzer | last post by:
I am trying to structure a Tkinter application with classes instead of just with simple functions, but I'm not sure how to call methods from my main class. My main class is packetstreamApp(). Within that class I call various methods, including drawGUI() and authorizeDump(). My problem comes when I try to call authorizeDump from the Tkinter...
3
3906
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By this I mean that the format would be something like this: You have Page1 : This has 2-3 buttons on it. Clicking on each button opens up a new...
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
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. ...
1
7642
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...
0
7950
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...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
924
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...

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.