Popup menu return value?

This is the place for queries that don't fit in any of the other categories.

Popup menu return value?

Postby eslavko » Sat Dec 28, 2013 11:07 am

Hello..
How to get result from popup (right mouse click) menu? I know that command can call some subs. But what if there is a lot of selections and only index or text is required?
There is example for COM port selection (just for example). So how to get string COM1, COM2... depends on wich butonn is clicked? Or at least the index of button.

Code: Select all
from Tkinter import *

def do_popup(event):
    popup.tk_popup(event.x_root, event.y_root, 0)

def mycom():
    print "COM?" #want to know wich one COM
   
root = Tk()

popup = Menu(root, tearoff=0)
popup.add_command(label="COM1",command=mycom)
popup.add_command(label="COM2",command=mycom)
popup.add_command(label="COM3",command=mycom)
popup.add_command(label="COM4",command=mycom)
popup.add_command(label="COM5",command=mycom)
popup.add_command(label="COM6",command=mycom)
popup.add_command(label="COM7",command=mycom)
popup.add_command(label="COM8",command=mycom)
popup.add_command(label="COM9",command=mycom)

root.bind("<Button-3>", do_popup)
root.mainloop()
eslavko
 
Posts: 25
Joined: Thu Dec 26, 2013 10:23 am

Re: Popup menu return value?

Postby metulburr » Sat Dec 28, 2013 2:37 pm

you have to use lambda when sending arguments to a function in command on tkinter. You have the
Code: Select all
lambda x=label:mycom(x))
because you are sending in label on each iteration of the loop, and you want it evaulated at creation time as opposed to
Code: Select all
lambda :mycom(label)

which evaulates at call time. Which the result of call time is the last iteration of the loop, thus giving COM9 regardless of what selection you hit on the menu.

Code: Select all
try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk
   
def do_popup(event):
    popup.tk_popup(event.x_root, event.y_root, 0)

def mycom(label):
    print(label)
   
root = tk.Tk()

popup = tk.Menu(root, tearoff=0)
for i in range(1,10):
    label = 'COM{}'.format(i)
    popup.add_command(label=label,command=lambda x=label:mycom(x))

root.bind("<Button-3>", do_popup)
root.mainloop()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1324
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Popup menu return value?

Postby eslavko » Sat Dec 28, 2013 3:33 pm

Thanks...

Function work, but I (still) don't understand that lambda function... (to new in python?!?)
eslavko
 
Posts: 25
Joined: Thu Dec 26, 2013 10:23 am

Re: Popup menu return value?

Postby Yoriz » Sat Dec 28, 2013 3:56 pm

Here is a link to the forums tutorial Lambda: How, Why, and Why not.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 726
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Popup menu return value?

Postby eslavko » Sat Dec 28, 2013 6:31 pm

Yoriz wrote:Here is a link to the forums tutorial Lambda: How, Why, and Why not.


Thank's for that...
eslavko
 
Posts: 25
Joined: Thu Dec 26, 2013 10:23 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests