简体   繁体   中英

how to pass values to popup command on right click in python tkinter

I am developing a gui in which i created a right click popup menu with few option. now my query is how can i pass some variables or values or argument or strings to the command incorporated in popup menu. i used below code to generate popup menu.

from Tkinter import *

root = Tk()

w = Label(root, text="Right-click to display menu", width=40, height=20)
w.pack()

# create a menu
popup = Menu(root, tearoff=0)
popup.add_command(label="Next", command=next(a,b))
popup.add_command(label="Previous")
popup.add_separator()
popup.add_command(label="Home")

def do_popup(event,a,b):
    # display the popup menu
    try:
        popup.tk_popup(event.x_root, event.y_root)
    finally:
        # make sure to release the grab (Tk 8.0a1 only)
        popup.grab_release()
def next(event,a,b):
    print a
    print b

w.bind("<Button-3>",lambda e, a=1, b=2: do_popup(e,a,b))

b = Button(root, text="Quit", command=root.destroy)
b.pack()

mainloop()

I the above code i want to pass values of a and b to Next command. How to do that.

thanks.

You need to store this values in order to use them in the next event handler. You can do some walkarounds, like adding a reference in the Menu object with popup.values = (a, b) , but the cleanest way is to use classes to represent you GUI.

Note that it is as easy as subclassing Tkinter widgets, and adding the values you want to store:

from Tkinter import *

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.a = 1
        self.b = 2
        self.label = Label(self, text="Right-click to display menu", width=40, height=20)
        self.button = Button(self, text="Quit", command=self.destroy)
        self.label.bind("<Button-3>", self.do_popup)
        self.label.pack()
        self.button.pack()
    def do_popup(self, event):
        popup = Popup(self, self.a, self.b)
        try:
            popup.tk_popup(event.x_root, event.y_root)
        finally:
            popup.grab_release()

class Popup(Menu):
    def __init__(self, master, a, b):
        Menu.__init__(self, master, tearoff=0)
        self.a = a
        self.b = b
        self.add_command(label="Next", command=self.next)
        self.add_command(label="Previous")
        self.add_separator()
        self.add_command(label="Home")
    def next(self):
        print self.a, self.b

app = App()
app.mainloop()

@A. Rodas's answer is good, but here's a solution that does not require a new instance of the menu for each activation (but rather one instance of the menu which is used again and again whenever needed):

from tkinter import *

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.a = 1
        self.b = 2
        self.menu = Popup(self)
        self.label = Label(self, text="Right-click to display menu",
                           width=40, height=20)
        self.button = Button(self, text="Quit", command=self.destroy)
        self.label.bind("<Button-3>",
                        lambda e: self.menu.popup(e.x_root, e.y_root,
                                                  self.a, self.b))
        self.label.pack()
        self.button.pack()

class Popup(Menu):
    def __init__(self, master):
        super().__init__(master, tearoff=0)
        self.a = None
        self.b = None
        self.add_command(label="Do something", command=self.act)
    def act(self):
        print(self.a, self.b)
    def popup(self, x, y, a, b):
        self.a = a
        self.b = b
        self.tk_popup(x, y)

app = App()
app.mainloop()

Notice how we pass the parameters upon invoking the menu, rather than upon instantiating it.

you can use "partial" as below:

from functools import partial

popup.add_command(label="Next", command=partial(next,a,b))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM