Thread: tkinter help

    #1
  1. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511

    tkinter help


    im trying to make a game where when you win a window pops up and says you win and when you lose it says you lose... you know the usual stuff. i tried using toplevel but it just popped up when i ran the program. i put

    [code]done1 = Toplevel()[\code] then if you lose i make it run the function [code]def you_lose(self):
    done1[\code]

    now i think i should put the done1 in the function but i don't have my program on my right now to try it. so is there a better way to do this than using toplevel? if not how can i make it work.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    Well, I think a better way might be to use a tkMessageBox. Here's a little example:
    Code:
    from Tkinter import *
    import tkMessageBox
    
    def win():
        tkMessageBox.showinfo("Yeah!","You win")
    
    def loose():
        tkMessageBox.showinfo("D'oh!","You loose")
    
    root = Tk()
    Button(text="Win",command=win).pack()
    Button(text="Loose",command=loose).pack()
    root.mainloop()
    Regards, mawe
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    I'd go with tkMessageBox as well. I was going to agree with you on that one but I guess that last person I agreed with was also you. :/
  6. #4
  7. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    quick question... is there a way to make it so when you click the "ok" button on the message window it closes the whole program? or even better when you click the "ok" it runs the program over again?
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Code:
    import sys
    from Tkinter import *
    import tkMessageBox
    import os
    import atexit
    def on_exit():
        if tkMessageBox.askokcancel('Exit', 'Do you really wish to exit?'):
            sys.exit()
            os.startfile(your_file_name)
    atexit.register(on_exit)
    Last edited by †Yegg†; April 20th, 2005 at 08:01 PM.
  10. #6
  11. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    im having trouble with the "your_file_name" part. say the whole target is C:\python2.4\go.py

    what would you put in then?
  12. #7
  13. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    You put the location of your file.
  14. #8
  15. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511

    :(


    alright this is for a different part.. the part where i need the top level window. this is my code

    Code:
    class App(Tk):
        def __init__(self, root):
            self.root = root
            root.geometry('600x200')
    
            t1 = Toplevel(root)
            t1.geometry('600x200')
            Button(t1, text = "Done", command = t1.quit).grid(column = 2, row = 1)
    when i click done it doesn't quit. the other problem im having is the top level window appears behind the normal window. is that suppose to happen? i figured it was going to be on top of the root window.
  16. #9
  17. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    My skills in Tkinter are very limited, I use wxPython. The code I showed you for Tkinter, if you use all of it, will bring up that message box when you attempt to exit the program. I'm not sure how Tkinter handles things, but can you bind the "Done" button with a function? Or better yet, if you replaced t1.quit with a function, would Tkinter still run properly?If you would like for the "Done" button to simply exit the program, no message box's etc, replace t1.quit with sys.exit(). Remember to import sys.
  18. #10
  19. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    well i was just looking to quit the top level not the whole program. all of my knowledge is based off of looking at other people's programs. for example i got this idea of a program someone wrote for a root calculator.

    Code:
    Button(buttons, text='Quit',
                   command=self.quit).pack(side=LEFT, padx=3)
    i saw that line in there. i checked if there was a quit function and there wasn't. i just figured that that made it exit the program. so will it work? and also, is the top level window suppose to go in top?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    Use t1.destroy instead of t1.quit.
    Why dont you use a tkMessageBox as †Yegg† and I suggested? I think it's much easier than with the Toplevel.

    PS: If you haven't seen it yet, I sent you a PM (well, a reply to your PM )

    Regards, mawe
  22. #12
  23. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    i did use the message boxes you guys suggested. this is for an instruction screen when you first run the program. so it pops up as soon as you start.

    and sorry i didn't see the pm. i forgot to check. sorry
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    You can let a tkMessageBox pop up as soon as you start too:
    Code:
    from Tkinter import *
    import tkMessageBox
    
    root = Tk()
    tkMessageBox.showinfo("hello","hello")
    root.mainloop()
    You don't have to worry about quit or destroy or something. Really handy, hm?

    Regards, mawe

IMN logo majestic logo threadwatch logo seochat tools logo