【发布时间】:2023-03-16 07:27:01
【问题描述】:
我正在制作一个餐厅管理系统,因此,在设置菜单中,我包含了一个更改食品价格的选项,而不是总是写食品价格,我希望有一些默认价格食物,但它没有按预期工作,请先检查以下代码:
def settings():
sett = Toplevel(Tops)
sett.title('Settings')
sett.geometry('600x550')
Label(sett, text = '').grid(row = 0, column =0)
Label(sett, text='')
global biriyani_entry1
global Chicken65_entry1
global coke_entry1
global vf_rice1
global samosa_entry1
global tea_entry1
global Noodles_entry1
Label(sett, text = 'Biriyani: ', font = ('calibri', 20)).grid(row=1, column =0)
biriyani_entry1 = Entry(sett, font=('calibri', 20))
biriyani_entry1.grid(row=1, column=1)
biriyani_entry1.insert(0, 200)
Label(sett, text='').grid(row=2, column=0)
Label(sett, text='').grid(row=2, column=1)
Label(sett, text='Chicken 65: ', font=('calibri', 20)).grid(row=3, column=0)
Chicken65_entry1 = Entry(sett, font=('calibri', 20))
Chicken65_entry1.grid(row=3, column=1)
Chicken65_entry1.insert(0,180)
Label(sett, text='').grid(row=4, column=0)
Label(sett, text='').grid(row=4, column=1)
Label(sett, text='Coke ', font=('calibri', 20)).grid(row=5, column=0)
coke_entry1 = Entry(sett, font=('calibri', 20))
coke_entry1.grid(row=5, column=1)
coke_entry1.insert(0,20)
Label(sett, text='').grid(row=6, column=0)
Label(sett, text='').grid(row=6, column=1)
Label(sett, text='Veg Fried Rice: ', font=('calibri', 20)).grid(row=7, column=0)
vf_rice1 = Entry(sett, font=('calibri', 20))
vf_rice1.grid(row=7, column=1)
vf_rice1.insert(0,120)
Label(sett, text='').grid(row=8, column=0)
Label(sett, text='').grid(row=8, column=1)
Label(sett, text='Samosa ', font=('calibri', 20)).grid(row=9, column=0)
samosa_entry1 = Entry(sett, font=('calibri', 20))
samosa_entry1.grid(row=9, column=1)
samosa_entry1.insert(0,15)
Label(sett, text='').grid(row=10, column=0)
Label(sett, text='').grid(row=10, column=1)
Label(sett, text='Tea ', font=('calibri', 20)).grid(row=11, column=0)
tea_entry1 = Entry(sett, font=('calibri', 20))
tea_entry1.grid(row=11, column=1)
tea_entry1.insert(0,10)
Label(sett, text='').grid(row=12, column=0)
Label(sett, text='').grid(row=12, column=1)
Label(sett, text='Noodles ', font=('calibri', 20)).grid(row=13, column=0)
Noodles_entry1 = Entry(sett, font=('calibri', 20))
Noodles_entry1.grid(row=13, column=1)
Noodles_entry1.insert(0,150)
Label(sett, text='').grid(row=14, column=0)
Label(sett, text='').grid(row=14, column=1)
Label(sett, text='').grid(row=15, column=0)
Label(sett, text='').grid(row=15, column=1)
apply_button = Button(sett, text = 'Apply', width = 10, height =1, font=('calibri',17), relief = SOLID,command = setting_change)
apply_button.grid(row=16, column = 1 )
执行函数是这样的:
def setting_change():
global bir
global ch65
global coke1
global vf
global samo
global te
global ndles
bir = biriyani_entry1.get()
ch65 = Chicken65_entry1.get()
coke1 = coke_entry1.get()
vf = vf_rice1.get()
samo = samosa_entry1.get()
te = tea_entry1.get()
ndles = Noodles_entry1.get()
这些 bir、ch65 等是作为输入给出的价格,它们将在计算总价时执行。
现在,当我运行程序时,我总是需要转到设置并单击应用,以应用默认值...但我希望在不单击应用的情况下应用这些值。
请帮忙!
【问题讨论】:
-
我的想法只是一种解决方法,但鉴于您已经编写了
setting_change(),您可以尝试在 gui 定义之后在运行时调用它一次 -
只需用所需的值初始化这些全局变量。
-
补充acw的建议,您可以先将这些变量设置为默认值,然后在gui定义期间使用这些变量本身。这也将导致代码更具可读性
标签: python function user-interface tkinter project