Updating an OptionMenu every time the text file it reads from is updated (Tkinter)

Alf P. Steinbach alfps at start.no
Tue Jan 19 05:57:09 EST 2010


* Dr. Benjamin David Clarke:
> I currently have a program that reads in values for an OptionMenu from
> a text file. I also have an option to add a line to that text file
> which corresponds to a new value for that OptionMenu. How can I make
> that OptionMenu update its values based on that text file without
> restarting the program? In other words, every time I add a value to
> the text file, I want the OptionMenu to immediately update to take
> note of this change. I'll provide code if needed.


It's a bit unclear to me whether the update of the text file is from within the 
process as where the menu is, or not.

If the problem is connecting file updates to menu changes:

   If it's the same process, perhaps you can wrap all access of the text file so 
that whenever an operation to change the text file is performed, it calls back 
on interested parties (like event handling)?

   If it's not the same process you need some "file changed" notification. 
Windows has this functionality. I don't know if it's there in *nix. Anyway, 
since I'm still essentially a Python newbie I don't know of any Python modules 
for such functionality, if that's what you need. But perhaps someone else does...

Otherwise, if the problem is actually updating the menu, and it is a tkinter 
OptionMenu:

It appears that an OptionMenu has a logical attribute 'menu' that is a tkinter 
Menu, representing the options; if your OptionMenu is an object 'o' then you can 
write 'o["menu"]' or 'o.cget( "menu" )' to get at that logical attribute. And 
tkinter Menu has various methods that you can use to inspect or update the menu; 
see <url: http://effbot.org/tkinterbook/menu.htm>. Disclaimer: I haven't tried 
updating, but I see no reason why it shouldn't work. :-)


Cheers & hth.,

- Alf



More information about the Python-list mailing list