[Tkinter-discuss] tkFileDialog

Michael Lange klappnase at web.de
Thu Aug 15 20:58:57 CEST 2013


On Tue, 30 Jul 2013 15:01:16 +0200
Michael Lange <klappnase at web.de> wrote:

> Hmm... the "center on screen" gimmick works only with the first dialog,
> can someone fix that ;)
> Anyway, unless we come up with something better, I'd classify this as
> strictly "don't try this at home"-ish :)

Just for the fun of it I've been playing with this for a few more minutes
and finally found something that seems almost usable ;) 
The trick seems to be that we must use a timer to make sure the geometry
is set only after the dialog window shows up, which however causes an ugly
"flashing" effect. Besides that this code snippet seems to work ok, the
geometry is set to one third of the screen's width and height and the
dialog is centered on the screen. 

Regards

Michael

#################################################################
from Tkinter import *
import tkFileDialog

root=Tk()

def set_filedialog_geometry():
    w, h = root.winfo_screenwidth(), root.winfo_screenheight()
    w0, h0 = w / 3, h / 3
    x, y = w / 2 - w0 / 2, h / 2 - h0 / 2
    try:
        root.tk.call('wm', 'geometry', '.__tk_filedialog',
                        '%dx%d+%d+%d' % (w0, h0, x, y))
    except TclError:
        print('TclError')
        #root.after(100, set_filedialog_geometry)

def ask_open_filename(**kw):
    root.after(100, set_filedialog_geometry)
    tkFileDialog.askopenfilename(**kw)

def ask_saveas_filename(**kw):
    root.after(100, set_filedialog_geometry)
    tkFileDialog.asksaveasfilename(**kw)

def test1(ev):
    ask_open_filename()
def test2(ev):
    ask_saveas_filename()

root.bind('<F1>', test1)
root.bind('<F2>', test2)

root.mainloop()


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Witch!  Witch!  They'll burn ya!
		-- Hag, "Tomorrow is Yesterday", stardate unknown


More information about the Tkinter-discuss mailing list