[Tutor] Wizards in Tkinter

jfouhy@paradise.net.nz jfouhy at paradise.net.nz
Wed May 25 00:11:08 CEST 2005


Quoting William O'Higgins <william.ohiggins at utoronto.ca>:

> I am writing a small application that takes a user through a set of
> steps - like a wizard. What I need is an idea how I can start with a
> window, have the use click "Next" and get another window. My
> understanding is that it is undesirable to have more than one mainloop
> per program. Thanks.

If you want more than one window, you use Tkinter.Toplevel.  But I think this is
not what you want here.

A few options spring to mind..

You could build multiple frames, but only pack the one you are interested in.  eg:

from Tkinter import *
tk = Tk()

page1 = Frame(tk)
Label(page1, text='This is page 1 of the wizard').pack()
page1.pack(side=TOP)

page2 = Frame(tk)
Label(page2, text='This is page 2 of the wizard.').pack()

page3 = Frame(tk)
Label(page3, text='This is page 3.  It has an entry widget too!').pack()
Entry(page3).pack()

pages = [page1, page2, page3]
current = page1
def move(dirn):
    global current
    idx = pages.index(current) + dirn
    if not 0 <= idx < len(pages):
        return
    current.pack_forget()
    current = pages[idx]
    current.pack(side=TOP)

def next():
    move(+1)

def prev():
    move(-1)

Button(tk, text='Next', command=next).pack(side=BOTTOM)
Button(tk, text='Previous', command=prev).pack(side=BOTTOM)

------------------

Another option is to use Pmw.Notebook without the tabs (and with next/previous
buttons to change pages).  This could be a bit easier (and it will do stuff like
making the wizard stay the same size).

Thirdly, a google search turned up this: http://www.freshports.org/devel/wizard/

HTH.

-- 
John.


More information about the Tutor mailing list