Issue
In the code below when choosing menubar item Recipient/email/ and an address is selected how can I capture what email address button has been pressed? I have found examples of using a drop down menu but I don't know how I would apply it the code below.
from tkinter import *
from tkinter import messagebox
class MenuBar(Menu):
def __init__(self, ws):
Menu.__init__(self, ws)
recipient= Menu(self, tearoff=0)
address = Menu(self, tearoff=0)
for email in ('[email protected]', '[email protected]'):
address.add_command(label=email)
recipient.add_cascade(label='email', menu=address)
self.add_cascade(label='Recipient', menu=recipient)
class MenuTest(Tk):
def __init__(self):
Tk.__init__(self)
menubar = MenuBar(self)
self.config(menu=menubar)
if __name__ == "__main__":
ws=MenuTest()
ws.title('email recipient')
ws.geometry('400x300')
ws.mainloop()
Solution
You should create a method that handles menu selections and accepts the email string as an argument
def __init__(self, ws):
# yadda yadda
for email in ('[email protected]', '[email protected]'):
address.add_command(
label=email,
# add a command to call your selection handler
# using a lambda to pass 'email' as an argument
command: lambda e = email: self.on_selection(e)
)
def on_selection(self, email): # this method will be fired on menu selection
print(email) # do whatever you need to do with the selected email
Answered By - JRiggles
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.