Quantcast

How to set optionmenu without setting the variable

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to set optionmenu without setting the variable

david.giesen
I don't believe there is any way to create an OptionMenu without using a Tk variable.

However, if all you want to do is to change the value of an already created OptionMenu without using the variable, you can do this:

import Tkinter as Tk
root = Tk.Tk()
var = Tk.StringVar()
om = Tk.OptionMenu(root, var, 'a', 'b', 'c')
om['menu'].invoke(index)

where index is the index of the value you want to select.  In this case, index=0 for 'a', 1 for 'b', or 2 for 'c'.

I'm not sure that's really an improvement for you, but it works.

Also note - you can add additional options after creation to the OptionMenu through a bit of a backdoor using a private Tk function (so this isn't strictly recommended, I guess) using the following syntax:

om['menu'].add_command(label='new_value', command=Tk._setit(var, 'new_value'))

Dave

> The conventional way goes this
> var = Stringvar()
> var.set('value')
> But i want to happen without using the variable. Can someone help me out on
> this?

David J. Giesen | Research Scientist | FPEG US Display OLED Materials R+D |
Eastman Kodak Company | 2/83/KRL MC02216 | Rochester, NY 14650 |

david.giesen@... | 1-585-588-0480 Office |
www.kodak.com


_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to set optionmenu without setting the variable

Michael Lange
Hi,

On Wed, 4 Nov 2009 08:23:39 -0500
[hidden email] wrote:

> I don't believe there is any way to create an OptionMenu without
> using a Tk variable.

Actually you can:

<code>

from Tkinter import *

root = Tk()
om = OptionMenu(root, None, '1', '2')
om.pack(padx=100, pady=100)

items = ('a', 'b', 'c', 'd')
def callback(item):
    om.configure(text=item)
    print 'selected:', item

om['menu'].delete(0, 'end')
for x in items:
    om['menu'].add_command(label=x, command=lambda item=x: callback(item))
callback('a')

root.mainloop()

</code>

I needed this once, because I was not able to handle unicode filenames
properly using a Tk textvariable, so you see it looks overly
complicated but in some cases may have its use.

I hope this helps

Michael

_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to set optionmenu without setting the variable

david.giesen
Well - now I can say I learned something today!

Your method also demonstrates how to add additional items after creation without using the Tkinter private routine _setit, which is also nice.

Dave

David J. Giesen | Research Scientist | FPEG US Display OLED Materials R+D |
Eastman Kodak Company | 2/83/KRL MC02216 | Rochester, NY 14650 |

david.giesen@... | 1-585-588-0480 Office |
www.kodak.com



From: Michael Lange <[hidden email]>
To: [hidden email]
Date: 11/04/2009 02:30 PM
Subject: Re: [Tkinter-discuss] How to set optionmenu without setting the variable
Sent by: tkinter-discuss-bounces+david.giesen=[hidden email]





Hi,

On Wed, 4 Nov 2009 08:23:39 -0500
[hidden email] wrote:

> I don't believe there is any way to create an OptionMenu without
> using a Tk variable.

Actually you can:

<code>

from Tkinter import *

root = Tk()
om = OptionMenu(root, None, '1', '2')
om.pack(padx=100, pady=100)

items = ('a', 'b', 'c', 'd')
def callback(item):
   om.configure(text=item)
   print 'selected:', item

om['menu'].delete(0, 'end')
for x in items:
   om['menu'].add_command(label=x, command=lambda item=x: callback(item))
callback('a')

root.mainloop()

</code>

I needed this once, because I was not able to handle unicode filenames
properly using a Tk textvariable, so you see it looks overly
complicated but in some cases may have its use.

I hope this helps

Michael

_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss



_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Loading...