【发布时间】:2018-06-10 17:06:35
【问题描述】:
下面给出的代码旨在计算收银员在输入小部件 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
【问题讨论】:
-
小心!
list是 Python 中的内置类型。你正在遮蔽它。给这个变量另一个名字 -
但它不会解决主要问题
-
不,这无关紧要。实际上,从你的问题很难理解你想要达到什么,你遇到了什么问题。您的代码不会引发任何错误。而且不相关的信息太多了。见How to create a Minimal, Complete, and Verifiable example
-
@UmerNaeem 如果是这样,那将是一个答案而不是评论。
-
现在怎么样???你现在明白了吗??