Tkinter's OptionMenu

Mike Callahan mcalla at home.com
Sat Apr 22 21:36:55 EDT 2000


Thanks, John. I see how to do it now.

<johngrayson at home.com> wrote in message news:8dship$83j$1 at nnrp1.deja.com...
> In article <ZiYL4.110745$8S2.1383832 at news1.rdc1.tn.home.com>,
>   "Mike Callahan" <mcalla at home.com> wrote:
> > How does one change an OptionMenu widget's list? I know that you can
> do this
> > easily using the PMW widget, but I would like to stay with basic
> Tkinter
> > widgets to keep the code as small as possible. I could not find any
> methods
> > in the code and I could not find any examples in either the online
> > documentation or the new book on Tkinter.
> >
> >
>
> I didn't expand on Tkinter's OptionMenu class too much in _Python and
> Tkinter Programming_, since using Pmw's version is usually more
> convenient. The problem is that you really have to 'roll your own'
> to do much with the vanilla widget.
>
> However, if you insist, here is some sample code to manipulate
> Tkinter's OptionMenu:
>
> from Tkinter import *
> root = Tk()
> root.title('OptionMenu')
>
> class setit:
> def __init__(self, var, value):
> self.__value = value
> self.__var = var
> def __call__(self, *args):
> self.__var.set(self.__value)
>
> class OptionM:
>     def __init__(self,parent):
>
>         self.var = StringVar()
>         self.var.set('BRM')
>
>         self.opt_menu = OptionMenu(parent, self.var, 'Stockbroker',
>                                    'Quantity Surveyor', 'Church Warden',
>                                    'BRM' )
>         self.opt_menu.pack(anchor=W, padx=20, pady=30)
>
>         Button(parent, text='Add item', command=self.addItem).pack()
>
>         Button(parent, text='Change items',
>                command=self.changeItems).pack()
>
>     def addItem(self):
>         self.opt_menu["menu"].add_command(label='Dinsdale',
>                                   command=setit(self.var, 'Dinsdale'))
>
>     def changeItems(self):
>         self.opt_menu["menu"].delete(0, END)
>         for item in ['Passion fruit','Loganberries','Mangoes in syrup',
>                       'Oranges','Apples','Grapefruit']:
>             self.opt_menu["menu"].add_command(label=item,
>                                       command=setit(self.var, item))
>         self.var.set('Loganberries')
>
> om = OptionM(root)
> root.mainloop()
>
> A bit crude, but it's a start. Use Pmw if you can!
>
>     John
>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.





More information about the Python-list mailing list