[Tkinter-discuss] Re: making tkFileDialog and tkMessageBox modal on Win32

Greg Lee glee at pharsight.com
Wed Apr 6 20:04:50 CEST 2005


The esteemed Fredrik Lundh responded:

> well, they sure are modal on my machine.  maybe you could post an example, tell
> us how it behaves on your machine, and tell us how you expected it to behave.

I, too, have seen the file dialogs behave modally on my machine---but not always.

This following has a top-level window with a menu item that launches a child.  The child has two browse buttons.  On my machine I can use the browse buttons to bring up about eleven simultaneous file dialogs.  What I had hoped for was that the first file dialog to appear would be modal, i.e. the browse buttons would be inactive until the file dialog was dismissed.
--------------------------------------------

import os
import Tkinter
import tkMessageBox
import tkFileDialog
import sys

# constants for NewPhx widgets
CONFIG_ROW = 0
SOURCEDIR_ROW = 1
ENTRY_WIDTH = 64
LABEL_WIDTH= 21

class NewPhx:
    def __init__(self, top):
        self.top = top
        self.title = "See how many browse dialogs we can launch"
        self.top.title(self.title)
        self.frame = Tkinter.Frame(self.top)
        self.frame.grid()
        self.count = 0

        # Control variables
        self.configfile = Tkinter.StringVar()

        # Add the widgets
        self.browserow('File 1', self.configfile, self.cb_file1, CONFIG_ROW)
        self.browserow('File 2', self.configfile, self.cb_file2, SOURCEDIR_ROW)

    def browserow(self, labeltext, entryvar, cb, row):
        '''Make a row to browse for a file or directory.'''
        Tkinter.Label(self.frame, text=labeltext + ":", anchor=Tkinter.W, width=LABEL_WIDTH).grid(row=row, column=0)
        Tkinter.Entry(self.frame, textvariable=entryvar, width=ENTRY_WIDTH).grid(row=row, column=1, columnspan=3)
        self.spacer(row, 4)
        Tkinter.Button(self.frame, text='Browse...', command=cb).grid(row=row, sticky=Tkinter.W, column=5)

    def spacer(self, row, column=0, text='  '):
        Tkinter.Label(self.frame, text=text).grid(row=row, column=column)

    def cb_file1(self):
        self.cb_configfile('file1')

    def cb_file2(self):
        self.cb_configfile('file2')
        
    def cb_configfile(self, which):
        '''Callback for configuration-file button'''
        self.count += 1
        kw = {'title': '%s %d' % (which, self.count)}
        retval = tkFileDialog.askopenfilename(**kw)
        if retval:
            self.configfile.set(os.path.normpath(retval))
        
class App:
    def __init__(self, root):
        self.root = root
        menubar = Tkinter.Menu(self.root)
        self.root.config(menu=menubar)

        taskmenu = Tkinter.Menu(menubar, tearoff=0)
        taskmenu.add_command(label='Launch child', command=self.menu_new)
        menubar.add_cascade(label='Pick task', menu=taskmenu)
        
    def menu_new(self):
        NewPhx(Tkinter.Toplevel(self.root))

def main(argv):
    root = Tkinter.Tk()
    App(root)
    root.mainloop()    

if __name__ == '__main__':
    main(sys.argv)


--------------------------------------------
Greg Lee / Pharsight Corporation / Suite 200
800 W El Camino / Mountain View CA 94040
voice: 650-314-3860 / fax: 650-314-3810




More information about the Tkinter-discuss mailing list