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.quitbutton = Button(wself)
wself.quitbutton["text"] = "Quit"
wself.quitbutton["fg"] = "red"
wself.quitbutton["command"] = wself.destroy
wself.quitbutton.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=Frame(master)
CS = CommonStuff(self.frame)
displayedwidget=MyWidgets(CS)
displayedwidget.grid(row=0, column=0)
self.frame.grid(row=0, column=0)
self.frame.columnconfigure(0)
displayedwidget.bind("<Destroy>", self.quit)
self.frame.update()
def quit(self, event):
print"Quitting..."
self.frame.destroy # Destroy frame and all children
root = Tk()
mainWin = Application(root)
root.wait_window(mainWin.frame) 5 7151
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.quitbutton = Button(wself) wself.quitbutton["text"] = "Quit" wself.quitbutton["fg"] = "red" wself.quitbutton["command"] = wself.destroy
wself.quitbutton.pack({"side": "left"})
like that:
wself.quitbutton = Button(wself, text='Quit', fg='red', command=wself.destroy)
wself.quitbutton.pack(side=LEFT)
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.destroy)
quitbutton.pack(side=LEFT)
or even:
Button(wself, text='Quit', fg='red', command=wself.destroy).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"})
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(side=LEFT)
or even:
Button(wself, text="Hello", command=CS.say_hi).pack(side=LEFT) class Application: def __init__(self, master): self.frame=Frame(master) CS = CommonStuff(self.frame)
displayedwidget=MyWidgets(CS) displayedwidget.grid(row=0, column=0) self.frame.grid(row=0, column=0) self.frame.columnconfigure(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.update()
def quit(self, event): print"Quitting..." self.frame.destroy # 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.update(). So to call the destroy
method on self.frame, you must do self.frame.destroy(). self.frame.destroy
merely returns the destroy method of the self.frame object, but doesn't do
anything with it.
root = Tk() mainWin = Application(root) root.wait_window(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*********@pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
Many thanks for such a comprehensive answer.
Altered root.wait_window(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.destroy as the function
self.frame.destroy(), 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.destroy)
self.quitbutton.pack(side=LEFT)
self.hi_there = Button(self, text='Hello', command=CS.say_hi)
self.hi_there.pack(side=LEFT)
class Application:
def __init__(self, master):
self.frame=Frame(master)
CS = CommonStuff(self.frame)
displayedwidget=MyWidgets(CS)
displayedwidget.grid(row=0, column=0)
self.frame.grid(row=0, column=0)
displayedwidget.bind("<Destroy>", self.quit)
self.frame.update()
def quit(self, event):
print"Quitting..."
self.frame.destroy() # Destroy frame and all children
root = Tk()
mainWin = Application(root)
root.mainloop()
> # 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.destroy) self.quitbutton.pack(side=LEFT) self.hi_there = Button(self, text='Hello', command=CS.say_hi) self.hi_there.pack(side=LEFT)
class Application: def __init__(self, master): self.frame=Frame(master) CS = CommonStuff(self.frame)
displayedwidget=MyWidgets(CS) displayedwidget.grid(row=0, column=0) self.frame.grid(row=0, column=0) displayedwidget.bind("<Destroy>", self.quit) self.frame.update()
def quit(self, event): print"Quitting..." self.frame.destroy() # Destroy frame and all children
root = Tk() mainWin = Application(root) 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.quit)
self.quitbutton.pack(side=LEFT)
self.hi_there = Button(self, text='Hello', command=CS.say_hi)
self.hi_there.pack(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=Frame(master)
CS = CommonStuff(self.frame)
displayedwidget=MyWidgets(CS)
displayedwidget.grid(row=0, column=0)
self.frame.grid(row=0, column=0)
displayedwidget.bind("<Destroy>", self.quit)
self.frame.update()
self.master = master
def quit(self, event):
print"Quitting..."
self.master.destroy() # 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
Eric Brunel <er*********@pragmadev.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.quit()
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
furliz wrote: I did try self.frame.destroy as the function self.frame.destroy(), 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,text="Quit",command=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*********@pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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()....
|
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...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |