我如何解决此名称未定义
How I can solved this name is not defined
我想让我做的程序可以运行
我编写这样的代码
def cafe_food(self):
friedrice_items = tk.Entry(F_FItems)
friedrice_items.config(width=7, borderwidth=4, relief="sunken", font=("calibri", 10,"bold"),foreground="white", background="#248aa2")
friedrice_items.place(x=81, y=1)
def Total_Biil(self):
friedrice_price = 10
pizza_price = 20
if friedrice_items.get() != "":
friedrice_cost = friedrice_price * int(friedrice_items.get())
else:
friedrice_cost = 0
if pizza_items.get() != "":
friedrice_cost = pizza_price * int(pizza_items.get())
else:
pizza_cost = 0
total_bills = friedrice_cost + pizza_cost
如果我 运行 此代码和..
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\kisah tegar\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__
return self.func(*args)
File "D:\Code\Python\Project\restaurant_mg\main.py", line 498, in Total_Bill
if friedrice_items.get() != "":
NameError: name 'friedrice_items' is not defined
[Finished in 6.3s]
这是我的问题:(
我怎样才能在那个函数
中得到friedrice_items
如果这段代码在 class 中,请尝试在变量名称前添加 self.
:
def cafe_food(self):
self.friedrice_items = tk.Entry(F_FItems)
self.friedrice_items.config(width=7, borderwidth=4, relief="sunken", font=("calibri", 10,"bold"),foreground="white", background="#248aa2")
self.friedrice_items.place(x=81, y=1)
def Total_Biil(self):
self.friedrice_price = 10
self.pizza_price = 20
if self.friedrice_items.get() != "":
self.friedrice_cost = self.friedrice_price * int(self.friedrice_items.get())
else:
self.friedrice_cost = 0
if self.pizza_items.get() != "":
self.friedrice_cost = self.pizza_price * int(self.pizza_items.get())
else:
self.pizza_cost = 0
self.total_bills = self.friedrice_cost + self.pizza_cost
self
或任何你给它起的名字,是一个 global keyword,在 class 中,你可以访问 class.[=15 中的任何地方的变量=]
如果您不使用 self
,您将无法在任何地方访问它,就像您之前遇到的情况一样。
我想让我做的程序可以运行 我编写这样的代码
def cafe_food(self):
friedrice_items = tk.Entry(F_FItems)
friedrice_items.config(width=7, borderwidth=4, relief="sunken", font=("calibri", 10,"bold"),foreground="white", background="#248aa2")
friedrice_items.place(x=81, y=1)
def Total_Biil(self):
friedrice_price = 10
pizza_price = 20
if friedrice_items.get() != "":
friedrice_cost = friedrice_price * int(friedrice_items.get())
else:
friedrice_cost = 0
if pizza_items.get() != "":
friedrice_cost = pizza_price * int(pizza_items.get())
else:
pizza_cost = 0
total_bills = friedrice_cost + pizza_cost
如果我 运行 此代码和..
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\kisah tegar\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__
return self.func(*args)
File "D:\Code\Python\Project\restaurant_mg\main.py", line 498, in Total_Bill
if friedrice_items.get() != "":
NameError: name 'friedrice_items' is not defined
[Finished in 6.3s]
这是我的问题:( 我怎样才能在那个函数
中得到friedrice_items如果这段代码在 class 中,请尝试在变量名称前添加 self.
:
def cafe_food(self):
self.friedrice_items = tk.Entry(F_FItems)
self.friedrice_items.config(width=7, borderwidth=4, relief="sunken", font=("calibri", 10,"bold"),foreground="white", background="#248aa2")
self.friedrice_items.place(x=81, y=1)
def Total_Biil(self):
self.friedrice_price = 10
self.pizza_price = 20
if self.friedrice_items.get() != "":
self.friedrice_cost = self.friedrice_price * int(self.friedrice_items.get())
else:
self.friedrice_cost = 0
if self.pizza_items.get() != "":
self.friedrice_cost = self.pizza_price * int(self.pizza_items.get())
else:
self.pizza_cost = 0
self.total_bills = self.friedrice_cost + self.pizza_cost
self
或任何你给它起的名字,是一个 global keyword,在 class 中,你可以访问 class.[=15 中的任何地方的变量=]
如果您不使用 self
,您将无法在任何地方访问它,就像您之前遇到的情况一样。