tkinter: loading file before entering mainloop

Peter Otten __peter__ at web.de
Sat Mar 14 05:45:29 EDT 2009


Peter Billam wrote:

>> Peter Billam wrote:
>>   window = MainWindow(application)
>>   if (len(sys.argv) > 1) and os.path.exists(sys.argv[1]):
>>       window.loadFile(sys.argv[1])
>>   application.mainloop()
>>   File "./midimix", line 465, in loadFile
>>     space0.grid(row=grid_row,
>>      pady=round(0.5*(ymid[track_num]-ymid[track_num-1]))-50)
>>   ...
>>   _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 ?
> 
> On 2009-03-14, Peter Otten <__peter__ at web.de> wrote:
>> The latter. Try
>>         application.after_idle(window.loadFile, sys.argv[1])
> 
> Thank you! That almost worked :-) It opened a window (which it didn't
> do last time), and it laid out the frames and so on apparently OK,
> and even posted a "Loaded v.mid" message on the StatusBar, but then
> just drew a couple of zero-thickness lines right at the top of the
> canvas, and failed with the same message:
>   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
> 
> but I say "almost" because I googled after_idle, and the very similar:
>     application.after(500, window.loadFile, sys.argv[1])
> does work, exactly as intended :-) except of course that it's a
> race condition, and will fail for very impatient people or on very
> slow machines.  On my machine it still works with 20ms, but fails
> with 10ms.  Is there one extra magic trick I need to know?
> 
> I also tried invoking after_idle on the canvas widget:
>    window.canvas.after_idle(window.loadFile, sys.argv[1])
> but that fails with the same message.
> (I am using python 3.0.1 in case that's, er, relevant.)
> 
> Thanks for your help,  Regards,  Peter
> 

Well, I don't know where the ymid[...] values come from. If you can
guarantee that ymid[track_num] - ymit[track_num-1] > 50 at some point you
could reschedule loadFile() from within loadFile() and return immediately
as long as that condition is not met.

Peter



More information about the Python-list mailing list