what's the difference between mainloop and app.mainloop and root.mainloop?do they have different meaning?which one is right in code1,code2,code3??_______________________________________________ Tkinter-discuss mailing list [hidden email] http://mail.python.org/mailman/listinfo/tkinter-discuss |
Hi,
Thus spoketh "守株待兔" <[hidden email]> unto us on Wed, 24 Aug 2011 10:49:06 +0800: (...) > code1,code2.code3 all can run , > > what's the difference between mainloop and app.mainloop and > root.mainloop? do they have different meaning?which one is right in > code1,code2,code3?? If you want to know things like this, the code from Tkinter.py might be a good source ;) So let's have a look at the methods' definitions; if you search Tkinter.py for "def mainloop" you will find two matches: 1. Tkinter.mainloop(): def mainloop(n=0): """Run the main loop of Tcl.""" _default_root.tk.mainloop(n) (remember, _default_root is in fact the application's Tk() instance) 2. Tkinter.Misc.mainloop(): def mainloop(self, n=0): """Call the mainloop of Tk.""" self.tk.mainloop(n) That is which both the Tk and the Frame class inherit from. So you see that calls to root.mainloop() and mainloop() are equivalent, behind the scene both will call: your_tk_instance.tk.mainloop() Apparently the case is slightly different when you do call app.mainloop(), because then of course your_frame_instance.tk.mainloop() is actually called. So now the question is, if the Tk() and the Frame() widget's tk attribute actually point to the same object. To test this, your friend is the python prompt: >>> from Tkinter import * >>> root = Tk() >>> f = Frame() >>> root.tk == f.tk True >>> root.tk <tkapp object at 0xf7115988> >>> f.tk <tkapp object at 0xf7115988> >>> So you see, all Tkinter widgets share the same object as their tk attribute and so in the end all the calls to mainloop() do exactly the same. I hope this helps Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Time is fluid ... like a river with currents, eddies, backwash. -- Spock, "The City on the Edge of Forever", stardate 3134.0 _______________________________________________ Tkinter-discuss mailing list [hidden email] http://mail.python.org/mailman/listinfo/tkinter-discuss |
Thus spoketh Steve Oldner <[hidden email]>
unto us on Wed, 24 Aug 2011 06:47:51 -0500: > So all three can be used, but which would be the more 'pythonic' way? How would I dare to decide this ;) ? If we look at the relevant parts of the IDLE code we find the following, and if they do it this way I guess that at least it can't be bad ;) def main(): (...lots of code...) # start editor and/or shell windows: root = Tk(className="Idle") fixwordbreaks(root) root.withdraw() (...some more lots of code...) root.mainloop() root.destroy() if __name__ == "__main__": sys.modules['PyShell'] = sys.modules['__main__'] main() Regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Suffocating together ... would create heroic camaraderie. -- Khan Noonian Singh, "Space Seed", stardate 3142.8 _______________________________________________ Tkinter-discuss mailing list [hidden email] http://mail.python.org/mailman/listinfo/tkinter-discuss |
Thanks. I have seen this in some code snippets, but not have seen the destroy()
def main(): root = Tk() #stuff here root.mainloop() if __name__ == '__main__': main() -----Original Message----- From: tkinter-discuss-bounces+steven.oldner=[hidden email] [mailto:tkinter-discuss-bounces+steven.oldner=[hidden email]] On Behalf Of Michael Lange Sent: Wednesday, August 24, 2011 1:05 PM To: [hidden email] Subject: Re: [Tkinter-discuss] the difference between mainloop and app.mainloop and root.mainloop? Thus spoketh Steve Oldner <[hidden email]> unto us on Wed, 24 Aug 2011 06:47:51 -0500: > So all three can be used, but which would be the more 'pythonic' way? How would I dare to decide this ;) ? If we look at the relevant parts of the IDLE code we find the following, and if they do it this way I guess that at least it can't be bad ;) def main(): (...lots of code...) # start editor and/or shell windows: root = Tk(className="Idle") fixwordbreaks(root) root.withdraw() (...some more lots of code...) root.mainloop() root.destroy() if __name__ == "__main__": sys.modules['PyShell'] = sys.modules['__main__'] main() Regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Suffocating together ... would create heroic camaraderie. -- Khan Noonian Singh, "Space Seed", stardate 3142.8 _______________________________________________ Tkinter-discuss mailing list [hidden email] http://mail.python.org/mailman/listinfo/tkinter-discuss _______________________________________________ Tkinter-discuss mailing list [hidden email] http://mail.python.org/mailman/listinfo/tkinter-discuss |
Free forum by Nabble | Edit this page |