Tkinter and dialogs

dwelch91 donald.welch at hp.com
Wed Jul 5 19:31:34 EDT 2006


I'm trying unsuccessfully to do something in Tk that I though would be 
easy. After Googling this all day, I think I need some help. I am 
admittedly very novice with Tk (I started with it yesterday), so I am 
sure I am overlooking something simple.

The basic idea is that my application will consist of a series of modal 
dialogs, that are chained together in "wizard" fashion. There will be 
some processing in between dialogs, but for the most part, the user will 
do some interaction with each dialog and then click "Next" or 
"Previous". My thinking was that the main (root) Tk window would be 
hidden and that each dialog would be modal and child to that hidden 
window. Is this a reasonable way to do this in Tkinter?

I grabbed the "Dialog" class from effbot's site figuring that was good 
starting point. I did modify it somewhat to convert to the grid layout 
manager, based on advice in the New Mexico Tech Tkinter Reference (by 
John Shipman).

When I run this (Ubuntu 6.06), I get no windows, not even the root Tk one.

Any ideas???

Thanks,

Don




#!/usr/bin/env python
from Tkinter import *

root = None

class Dialog(Toplevel):

     def __init__(self, parent, title = None):
         Toplevel.__init__(self, parent)
         self.transient(parent)

         if title:
             self.title(title)

         print repr(parent)
         self.parent = parent
         self.result = None

         #body = Frame(self)
         #self.initial_focus = self.body(body)
         #body.pack(padx=5, pady=5)

         self.initial_focus = self.body(self)

         self.buttonbox()

         self.bind("<Return>", self.ok)
         self.bind("<Escape>", self.cancel)

         self.grab_set()

         if not self.initial_focus:
             self.initial_focus = self

         self.protocol("WM_DELETE_WINDOW", self.cancel)

         self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
                                   parent.winfo_rooty()+50))

         self.initial_focus.focus_set()

         self.wait_window(self)


     #
     # construction hooks

     def body(self, master):
         # create dialog body.  return widget that should have
         # initial focus.  this method should be overridden
         pass

     def buttonbox(self):
         # add standard button box. override if you don't want the
         # standard buttons

         box = Frame(self)

         w = Button(box, text="OK", width=10, command=self.ok, 
default=ACTIVE)
         w.pack(side=LEFT, padx=5, pady=5)
         w = Button(box, text="Cancel", width=10, command=self.cancel)
         w.pack(side=LEFT, padx=5, pady=5)

         self.bind("<Return>", self.ok)
         self.bind("<Escape>", self.cancel)

         box.pack()

     #
     # standard button semantics

     def ok(self, event=None):
         if not self.validate():
             self.initial_focus.focus_set() # put focus back
             return

         self.withdraw()
         self.update_idletasks()
         self.apply()
         self.cancel()

     def cancel(self, event=None):
         # put focus back to the parent window
         self.parent.focus_set()
         self.destroy()

     #
     # command hooks

     def validate(self):
         print "validate"
         return True # override

     def apply(self):
         print "apply"
         pass # override


class WelcomeDialog(Dialog):
     def body(self, master):

         Label(master, text="First:").grid(row=0, sticky=W)
         Label(master, text="Second:").grid(row=1, sticky=W)

         self.e1 = Entry(master)
         self.e2 = Entry(master)

         self.e1.grid(row=0, column=1)
         self.e2.grid(row=1, column=1)

         self.cb = Checkbutton(master, text="Hardcopy")
         self.cb.grid(row=2, columnspan=2, sticky=W)

         self.w1 = Button(master, text="OK", width=10, command=self.ok, 
default=ACTIVE)
         #w.pack(side=LEFT, padx=5, pady=5)
         self.w1.grid(row=3, column=0)
         self.w2 = Button(master, text="Cancel", width=10, 
command=self.cancel)
         #w.pack(side=LEFT, padx=5, pady=5)
         self.w2.grid(row=3, column=1)


     def apply(self):
         print "apply"
         first = int(self.e1.get())
         second = int(self.e2.get())
         self.result = first, second

     def buttonbox(self):
         pass


def show_ui():
     welcome = WelcomeDialog(root, "test1")



global root
root = Tk()
root.after_idle(show_ui)
root.mainloop()



More information about the Python-list mailing list