首页 > 解决方案 > 我想将默认值应用于 python tkinter 中的程序

问题描述

我正在制作一个餐厅管理系统,所以,在设置菜单中,我包含了一个更改食品价格的选项,而不是总是写食品价格,我希望有一些食品的默认价格,但是它没有按预期工作,请先检查以下代码:

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 等是作为输入给出的价格,它们将在计算总价时执行。

现在,当我运行程序时,我总是需要转到设置并单击应用,以应用默认值......但我希望在不单击应用的情况下应用这些值。

请帮忙!

标签: pythonfunctionuser-interfacetkinterproject

解决方案


一种简单的方法是使用默认值初始化这些全局变量:

# initialise default prices
bir = 200
ch65 = 180
coke1 = 20
vf = 120
samo = 15
te = 10
ndles = 150

然后使用这些变量来初始化它们对应的Entry小部件:

biriyani_entry1.insert(0, bir)
Chicken65_entry1.insert(0, ch65)
coke_entry1.insert(0, coke1)
vf_rice1.insert(0, vf)
samosa_entry1.insert(0, samo)
tea_entry1.insert(0, te)
Noodles_entry1.insert(0, ndles)

如果要更新默认值,则可以将值保存到setting_change()函数内部的外部文件中。当然,如果程序启动时存在,则需要加载外部文件。


推荐阅读