Python resturant管理系统如何添加库存收货

时间:2017-12-30 17:55:24

标签: python layout tkinter

下面给出的代码用于计算收银员在条目小部件 order_raw 中输入的项目清单,为此我在定义之前已经放置 sum = 0 一个函数,然后增加到总和,但我的程序仍然没有计算总账单。它一直给我零。目前我的代码没有错误。

from tkinter import *
order=[]
window=Tk()
order_raw=Entry(window)
order_raw.pack()
global bill
bill=0
def totalbill():
    global bill
    global billvar
    order.append(order_raw.get())
    if order_raw.get()=="burger":
        bill=bill+200
    if order_raw.get()=="fries":
        bill=bill+200
    print(bill)
billvar=StringVar(value=bill)
checkout=Button(window, text="total", command=totalbill)
checkout.pack()
total=Entry(window, textvariable=billvar)
total.pack()
window.mainloop()

然后我将 StringVar 内部移动到修改了可变总和的函数中。然后它给出了我的错误,下面是代码及其错误。

from tkinter import *
order=[]
window=Tk()
order_raw=Entry(window)
order_raw.pack()
global bill
bill=0
def totalbill():
    global bill
    global billvar
    order.append(order_raw.get())
    if order_raw.get()=="burger":
        bill=bill+200
    if order_raw.get()=="fries":
        bill=bill+200
    print(bill)
    billvar=StringVar(value=bill)
checkout=Button(window, text="total", command=totalbill)
checkout.pack()
total=Entry(window, textvariable=billvar)
total.pack()
window.mainloop()

错误

"Traceback (most recent call last):
  File "C:/Users/umerk/.PyCharmCE2017.2/config/scratches/scratch_7.py", line 20, in <module>
    total=Entry(window, textvariable=billvar)
NameError: name 'billvar' is not defined

1 个答案:

答案 0 :(得分:0)

如果您想更新第一个代码段中的底部条目,只需添加:

billvar.set(bill)

作为totalbill的最后一行。请参阅以下示例。

variable classesEntry的示例:

import tkinter as tk

def sum_cb():
    global item, sum_var

    if item.get() == "burger":
        sum_var.set(sum_var.get() + 30)

    elif item.get() == "fries":
        sum_var.set(sum_var.get() + 10)

root = tk.Tk()
item = tk.Entry(root)
sum_var = tk.IntVar(value=0)
sum_entry = tk.Entry(root, textvariable=sum_var)
sum_btn = tk.Button(root, text="Sum", command=sum_cb)

item.pack()
sum_btn.pack()
sum_entry.pack()

root.mainloop()

这是一个GUI的最小示例,它计算输入数字的总和并将其显示为按钮文本:

import tkinter as tk

def sum_cb():
    global sum_btn, item

    if item.get() == "burger":
        sum_btn['text'] = int(sum_btn['text']) + 30

    elif item.get() == "fries":
        sum_btn['text'] = int(sum_btn['text']) + 10

root = tk.Tk()
item = tk.Entry(root)
sum_btn = tk.Button(root, text=0, command=sum_cb)

item.pack()
sum_btn.pack()

root.mainloop()

在其他Entry而非Button文字中显示:

import tkinter as tk

def sum_cb():
    global item, sum_entry

    cur_sum = int(sum_entry.get())
    sum_entry.delete(0, 'end')

    if item.get() == "burger":
        sum_entry.insert(0, cur_sum + 30)

    elif item.get() == "fries":
        sum_entry.insert(0, cur_sum + 10)

root = tk.Tk()
item = tk.Entry(root)
sum_entry = tk.Entry(root)
sum_entry.insert(0, 0)
sum_btn = tk.Button(root, text="Sum", command=sum_cb)

item.pack()
sum_btn.pack()
sum_entry.pack()

root.mainloop()