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

    Join Date
    Dec 2004
    Location
    Champaign - Urbana, IL
    Posts
    54
    Rep Power
    10

    Non-Standard Dialogs with Tkinter


    Greetings,

    I've explored the options of the standard dialogs (tkMessageBox, tkSimpleDialog, etc) that come with the Tkinter module, but I was wondering how one would go about creating their own. For example, in the program I'm writting, the user will enter a series of integers which defines a specific action. Then, a dialog will need to open displaying different options to the user based on their input. The user clicks one or more buttons on that dialog followed by the close button and now the program has all the necessary information to process the test and return the results.

    However, I can't figure out how to create the dialog. I created a second module that when run with test data creates a window the way I want it to. However, when I import that module into a second one and the instantiate the dialog, all of the interface widgets are added to the bottom of the original window instead of opening a new one.

    Any thoughts?

    Thanks,
    Dave
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Why not just use the Toplevel class and modify the widgets in this window accordingly?
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Here is one way - This example just uses a normal function to hold all the bits but you can also create new dialog sub-class which would handle the focus for you.
    python Code:
    import Tkinter
    def demodialog(parent,currentmode,currentvalue):
        device_mode = ["On","Off","Automatic","Manual"]
     
        win = Tkinter.Toplevel(parent)
        win.transient(parent)
        win.title('Demo Dialog')
     
        mode = Tkinter.IntVar(win)
        mode.set(currentmode)
     
        entryvalue = Tkinter.StringVar(win)
        entryvalue.set(currentvalue)
     
        Tkinter.Label(win,text='Radio Buttons').grid(row=0,column=1,columnspan=2,sticky =Tkinter.W)
        for n in range(4):
            Tkinter.Radiobutton(win,text=device_mode[n],variable=mode,value=n).grid(row=n+1,column=1,sticky =Tkinter.W)
     
        Tkinter.Label(win,text="Enter Value").grid(row=6,column = 0)
        Tkinter.Entry(win,textvariable = entryvalue).grid(row=6,column = 1)
     
        Tkinter.Button(win,text="OK",command = win.destroy).grid(row=7,column = 1)
        win.focus_set()
        win.grab_set()
        win.wait_window()
     
        return mode.get(),entryvalue.get()
     
     
     
    mode = 3
    level = 0.1
    main = Tkinter.Tk()
    demoreturn = demodialog(main,mode,level)
    main.mainloop()
    print demoreturn


    grim
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Champaign - Urbana, IL
    Posts
    54
    Rep Power
    10
    Originally Posted by †Yegg†
    Why not just use the Toplevel class and modify the widgets in this window accordingly?
    I'm afraid I don't understand what you mean. Can you elaborate? I was under the impression that if you altred the toplevel class to modify the widgets, the new widgets would appear in the original window. I would prefer a new window (modal dialog) with new widgets that could process user input and return that information back to the original window.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Champaign - Urbana, IL
    Posts
    54
    Rep Power
    10
    Answered my own question.

    However, I've a new one: So, this new dialog is going to have 5 or 7 buttons (depending on what input the user provided to configure the dialog). Each button will perform the same action but on different values maintained in StringVar variables. What I need to do is bind the same callback function to all of the buttons, but then that callback function needs to know which button was clicked. Any ideas?

    Thanks!
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Originally Posted by dgoldfdr
    I'm afraid I don't understand what you mean. Can you elaborate? I was under the impression that if you altred the toplevel class to modify the widgets, the new widgets would appear in the original window. I would prefer a new window (modal dialog) with new widgets that could process user input and return that information back to the original window.
    Oh. I did not know you wanted modal. I always prefer non-modal.
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Originally Posted by dgoldfdr
    Answered my own question.

    What I need to do is bind the same callback function to all of the buttons, but then that callback function needs to know which button was clicked. Any ideas?
    1: Each button has its own callback which calls a common function but passing a constant unique to the button.
    2. Variation on 1 - use lambda functions in each widget definition.

    grim

IMN logo majestic logo threadwatch logo seochat tools logo