#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    2
    Rep Power
    0

    "Hello World" gui program crashes!?


    Hi,

    I've just installed Python v2.4.1. I'm learning it for the first time. The first program I decided to try running, just as a demo, was a simple gui program which I found in the Python documentation:

    Code:
    from Tkinter import *
    
    class Application(Frame):
        def say_hi(self):
            print "hi there, everyone!"
    
        def createWidgets(self):
            self.QUIT = Button(self)
            self.QUIT["text"] = "QUIT"
            self.QUIT["fg"]   = "red"
            self.QUIT["command"] =  self.quit
    
            self.QUIT.pack({"side": "left"})
    
            self.hi_there = Button(self)
            self.hi_there["text"] = "Hello",
            self.hi_there["command"] = self.say_hi
    
            self.hi_there.pack({"side": "left"})
    
        def __init__(self, master=None):
            Frame.__init__(self, master)
            self.pack()
            self.createWidgets()
    
    app = Application()
    app.mainloop()
    It runs OK, but when I click the Quit button the program freezes and Windows eventually says it's not responding and asks me to terminate it. So what gives? When I installed Python I accepted all the default settings. My OS is Windows XP Home Edition (512 MB RAM).


    Thanks,
    stretch65.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    Well, it works on Linux (I don't have Windows so I can't test it there). Do you run this script from idle? If so, don't do it idle behaves strange with Tk scripts.

    PS: You can shorten your code a bit by creating the Quit-Button like this:
    Code:
    self.QUIT = Button(self, text="Quit", fg="red", command=self.quit)
    Regards, mawe
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    11
    That code works just fine here. I'm on win2k


    Edit:
    You can also try this (a way I which I would prefer over the other one, since its less code):

    Code:
    from Tkinter import *
    
    root = Tk()
    
    class Application(object):
        def say_hi(self):
            print "hi there, everyone!"
    
        def __init__(self, master):
            frame = Frame(master)
            frame.pack()
            Button(frame, text="QUIT", fg="red", command=master.quit).pack(side=LEFT)
            Button(frame, text="Hello", command=self.say_hi).pack(side=LEFT)
    
    
    app = Application(root)
    root.mainloop()
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    possible try a bit older of a python version. I seem to remember reading soemthing about there being a bug in 2.4 somewhere with tkinter. Try python 2.3 or something, basicly the same for what your are doing, just without a few of the newer stuff that isn't really a huge thing to use.

    If it works with python 2.3 then it is an error in 2.4, please post back if it does work in 2.3 and pleast tell us some of your system stats, things like services packs and everything else will be helpfull.

    also, I would try active states python, possible may run better.
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    618
    Rep Power
    65

    Smile


    My vertiginous aunt, we just had a thread on this very subject:
    http://forums.devshed.com/t259069/s.html
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Are you running it in the interpreter? Tkinter applications and wxPython applications are known to crash while being run from the interpreter, no matter how powerful your computer is. Save the program as a .pyw file. Then open it, it should work just fine.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    2
    Rep Power
    0
    Originally Posted by †Yegg†
    Are you running it in the interpreter? Tkinter applications and wxPython applications are known to crash while being run from the interpreter, no matter how powerful your computer is. Save the program as a .pyw file. Then open it, it should work just fine.
    Alright, so I can't run it from IDLE, and I can't run it from the interpreter ( ). How the hell do I run it so that clicking the QUIT button doesn't cause it to freeze?? Can you be more specific about how I run it. I've saved the source code as a .pyw file, now what?


    Thanks, stretch65.
  14. #8
  15. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    618
    Rep Power
    65

    Smile


    Hello,
    anybody out there?

    Grim says:
    quit tries to terminate the graphics and TCL interpreter - which seems to cause a lock-up if the program is launched within a graphical environment like IDLE or Pythonwin.

    destroy is better behaved - it kills only the resources you create. Calling destroy will cause the mainloop to terminate and deletes the widgets. Which is probably what most people want anyway.

    Comments on this post

    • Grim Archon agrees
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    2
    Rep Power
    0

    Run from command prompt


    Originally Posted by stretch65
    Alright, so I can't run it from IDLE, and I can't run it from the interpreter ( ). How the hell do I run it so that clicking the QUIT button doesn't cause it to freeze?? Can you be more specific about how I run it. I've saved the source code as a .pyw file, now what?


    Thanks, stretch65.
    Well, open a dos command window, and go to the appropriate directory (best to save everything to the Python24 directory. Type python <filename> and it will run in the command window. (you can find the command prompt by going to start, accessories.)
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    use root or app.destroy to terminate the program
    Those people who think they know everything are a great annoyance to those of us who do.

IMN logo majestic logo threadwatch logo seochat tools logo