Reputation: 21
how do i make it so when I click on a button with the items selected from the option menu, a label will display saying "you have ordered (the item selected from the option menu) and (the other item)"
food = ['Pizza','Burger','Nachos', 'French Toast']
drinks = ['Pepsi','Lemonade','Tea', 'Aperitivo Spritz']
def order_menu(self):
self.myFrame2.destroy()
self.myFrame4 = Frame(root)
self.myFrame4.pack(fill = "both", expand = 1)
self.tkvar = StringVar(self.myFrame4)
self.tkvar.set("Food")
self.tkvar.trace_add('write', lambda *args: print(self.tkvar.get()))
self.tkvar2 = StringVar(self.myFrame4)
self.tkvar2.set("Drinks")
self.tkvar2.trace_add('write', lambda *args: print(self.tkvar2.get()))
self.foodMenu = OptionMenu(self.myFrame4, self.tkvar, *food, command = self.callback)
self.foodMenu.place(x = 160, y = 110)
self.drinkMenu = OptionMenu(self.myFrame4, self.tkvar2, *drinks, command = self.callback)
self.drinkMenu.place(x = 385, y = 110)
self.order_btn78 = PhotoImage(file = 'new-dip-project\\orderb.png')
self.order_btn = Button(self.myFrame4, image = self.order_btn78, command = self.callback, bd = 0)
self.order_btn.place(x = 302, y = 160)
Upvotes: 0
Views: 43
Reputation:
This will update as soon as you select the menu.
from tkinter import *
root=Tk()
def set_food(*args):
lbl_1.config(text="Food: "+var1.get())
def set_drink(*args):
lbl_2.config(text="Drink: "+var2.get())
food = ['Pizza','Burger','Nachos', 'French Toast']
drinks = ['Pepsi','Lemonade','Tea', 'Aperitivo Spritz']
var1=StringVar()
var2=StringVar()
var1.set("Food")
var2.set("Drink")
Label(root,text="Food").place(x=10,y=15)
Label(root,text="Drinks").place(x=10,y=55)
OptionMenu(root,var1,*food).place(x=50,y=10)
OptionMenu(root,var2,*drinks).place(x=50,y=50)
var1.trace("w",set_food)
var2.trace("w",set_drink)
Label(root,text="Chosen:").place(x=30,y=90)
lbl_1=Label(root)
lbl_1.place(x=50,y=120)
lbl_2=Label(root)
lbl_2.place(x=50,y=150)
root.mainloop()
However, if you do want the buttons:
from tkinter import *
root=Tk()
def set_mwnu():
lbl_2.config(text="Drink: "+var2.get())
lbl_1.config(text="Food: "+var2.get())
food = ['Pizza','Burger','Nachos', 'French Toast']
drinks = ['Pepsi','Lemonade','Tea', 'Aperitivo Spritz']
var1=StringVar()
var2=StringVar()
var1.set("Food")
var2.set("Drink")
Label(root,text="Food").place(x=10,y=15)
Label(root,text="Drinks").place(x=10,y=55)
OptionMenu(root,var1,*food).place(x=50,y=10)
OptionMenu(root,var2,*drinks).place(x=50,y=50)
Button(root,command=set_menu()).place(x=30,y=90)
Label(root,text="Chosen:").place(x=30,y=120)
lbl_1=Label(root)
lbl_1.place(x=50,y=150)
lbl_2=Label(root)
lbl_2.place(x=50,y=180)
root.mainloop()
Upvotes: 1
Reputation: 337
Do you mean like this?
from tkinter import *
class Gui(Tk):
def __init__(self):
super(Gui, self).__init__()
self.menu_var = StringVar(self)
self.menu = OptionMenu(self, self.menu_var, *['Pizza','Burger','Nachos', 'French Toast'])
self.menu.pack(pady=10)
self.info_label = Label(self, text="")
self.info_label.pack(pady=10)
self.btn = Button(self, text="SHOW", command=self.callback)
self.btn.pack()
def callback(self):
self.info_label.configure(text=self.menu_var.get())
if __name__ == "__main__":
app = Gui()
app.mainloop()
Upvotes: 1