tkinter: loading file before entering mainloop

Peter Otten __peter__ at web.de
Sat Mar 14 04:33:52 EDT 2009


Peter Billam wrote:

> I've got this tkinter program which allows you to load a file with
> File/LoadFile or Ctrl-L or from the Alt-F menu, as usual. But I'd
> also like to be able to invoke it with:
>   shellprompt> midimix x.mid
> and have it invoke its usual loadFile method on x.mid
> But with the top-level code:
> 
>   application = tkinter.Tk()
>   window = MainWindow(application)
>   if (len(sys.argv) > 1) and os.path.exists(sys.argv[1]):
>       window.loadFile(sys.argv[1])
>   application.mainloop()
> 
> it crashes:
>   File "./midimix", line 465, in loadFile
>     space0.grid(row=grid_row,
>      pady=round(0.5*(ymid[track_num]-ymid[track_num-1]))-50)
>   File "/usr/local/lib/python3.0/tkinter/__init__.py",
>    line 1845, in grid_configure
>     + self._options(cnf, kw))
>   _tkinter.TclError: bad pad value "-50": must be positive screen distance
> 
> presumably because the window doesn't have dimensions before mainloop
> is entered.  Can I force the window to be laid out before entering
> mainloop? Or can I invoke loadFile() after mainloop has started ?

The latter. Try

>   application = tkinter.Tk()
>   window = MainWindow(application)
>   if (len(sys.argv) > 1) and os.path.exists(sys.argv[1]):
        application.after_idle(window.loadFile, sys.argv[1])
>   application.mainloop()

Peter



More information about the Python-list mailing list