May-30-2018, 12:41 AM
(This post was last modified: May-31-2018, 01:19 AM by AceScottie.)
Simplified code:
Pass the buttons lable to the popup menu so i can refernce it later
I have tried quite a few things. I tried putting the popup menu inside the generate_buttons function but then its not defined in the popup function, or i have issues passing the event variable to the popup function.
from Tkinter import *
root = Tk()
def hello():
print("this is a hello:")
def test():
print("this is a test:")
def popup(event):
try:
popup_menu.tk_popup(event.x_root, event.y_root, 0)
finally:
popup_menu.grab_release()
def generate_buttons():
rowset = 0
colset = 0
for i in range(10):
Packer = Frame(root, bg="white", borderwidth=1, relief=RAISED)
B= Button(Packer, command=test, borderwidth=0, bg="red")
L = Label(Packer, text="button %s" %i, justify=CENTER, bg="white")
B.bind("<Button-3>", popup)
B.pack(side=TOP, fill=BOTH, expand=1)
L.pack(side=TOP)
Packer.pack(side=LEFT)
Packer.grid(padx=5, pady=5, sticky="nsew", row=rowset, column=colset)
if colset == 3:
colset = 0
rowset +=1
else:
colset += 1
popup_menu = Menu(root, tearoff=0)
popup_menu.add_command(label="test ", command=hello)
generate_buttons()
root.mainloop()Objective:Pass the buttons lable to the popup menu so i can refernce it later
def hello():
print("this is a hello:")
##OUTPUT "this is a hello:"
#to
##
def hello(text):
print("this is a hello: %s" %text)
##OUTPUT "this is a hello: button 1"So each button can pass a differnet variable to the popup menu.I have tried quite a few things. I tried putting the popup menu inside the generate_buttons function but then its not defined in the popup function, or i have issues passing the event variable to the popup function.
