python:将值附加到类外部的列表中,带有append的函数也在类外部,但函数在类中调用 [英] python: Appending a value to a list outside the class, function with append also outside the class, but function is called within a class
问题描述
我对python很陌生,我一直在尝试使用tkinter button命令运行函数的代码,它可以工作,但是append()未执行,这意味着它不会追加到列表中
I am very new to python and I've been trying to do this code where i use a tkinter button command to run a function, it works but the append() is not executing, meaning it does not append to the list.
列表和包含append的函数在类之外,然后通过使用tkinter按钮命令在类内分类
The list and the function containing the append is outside the class and is then classed within a class through the use of tkinter button command
我尝试将函数放入类中,但可以正常工作,但追加没有再次添加到列表中。
I've tried putting the function inside the class, it works but the append is not adding into the list again.
import tkinter as tk
from threading import Thread
from queue import Queue
import time
from tkinter import messagebox
LARGE_FONT = ("Verdana", 12)
Receipt = []
prices = []
job_queue = Queue()
Orders = []
class SushiMenuApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (OrderPage, ReceiptPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(OrderPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def sushichef(job_queue):
while True:
a = job_queue.get()
if a is None:
print("There are no more orders")
break
messagebox.showinfo("Message", "Sushi Chef started doing you order")
time.sleep(a)
print("Your order will be coming to you now")
job_queue.task_done()
class OrderPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Sushi Selections", font=LARGE_FONT)
label.grid(row=0, column=0, columnspan=4)
checkout = tk.Button(self, text="Check Out", command=lambda:controller.show_frame(ReceiptPage))
checkout.grid(row=0, column=4)
for _ in range(4): # 4 Sushi Chefs
thread = Thread(target=sushichef, args=(job_queue,))
thread.start()
Orders.append(thread)
shrimptempura = tk.Button(self, text="Shrimp Tempura",
command= staction)
shrimptempura.grid(row=1, column=0)
def staction():
for item in [60]:
job_queue.put(item)
prices.append(69)
Receipt.append("Shrimp Tempura")
class ReceiptPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Receipt", font=LARGE_FONT)
label.grid(row=0, columnspan=4, sticky="nsew")
fullreceipt= tk.Label(self, text='\n'.join(map(str, Receipt)))
fullreceipt.grid(row=1, columnspan=4, rowspan=48)
sumprice= tk.Label(self, text=sum(prices))
sumprice.grid(row=49, column=1, columnspan=2)
app = SushiMenuApp()
app.mainloop()
我期望输出将prices.append()中的值和receiving.append()中的字符串以及job_queue相加,
I was expecting the output to add the value within the prices.append() and the string within the receipt.append() together along with the job_queue,
期望是在收据页面中看起来像这样
expected would be that in the receipt page it would look something like this
Receipt
Shrimp Tempura
70
但输出是只有job_queue是我可以看到它正在工作,因为消息框是工作,这是受job_queue影响的,但在ReceiptPage中只会显示
but the output is that only the job_queue is what i can see as working because the message box was working which was the effect of the job_queue but in the ReceiptPage it would only show
Receipt
推荐答案
框架 ReceiptPage
是在开始时创建的,在您按下任何按钮甚至看不到窗口之前,因此标签
从空列表中获取信息。您必须在 ReceiptPage
中添加更新标签
的函数,并在您<< c> c结帐时执行它
Frame ReceiptPage
is create at start, before you press any button or even see window, so Labels
get information from empty lists. You have to add function which updates Labels
in ReceiptPage
and execute it when you pless Check Out
您还必须使用 self。
作为标签,以便您可以访问
You will have to also use self.
for labels so you have access to labels in other functions.
ReceiptPage
具有更新标签的功能。 标签
使用自己。
ReceiptPage
has function which update labels. Labels
uses self.
class ReceiptPage(tk.Frame):
def update_labels(self):
self.fullreceipt['text'] = '\n'.join(map(str, Receipt))
self.sumprice['text'] = sum(prices)
OrderPage
具有更改页面并运行 update_labels
的功能。 控制器
也使用 self。
OrderPage
has function which change page and run update_labels
. controler
also uses self.
def show_check_out(self):
self.controller.show_frame(ReceiptPage)
self.controller.frames[ReceiptPage].update_labels()
和按钮执行此功能
checkout = tk.Button(self, text="Check Out", command=self.show_check_out)
完整代码:
Full code:
import tkinter as tk
from threading import Thread
from queue import Queue
import time
from tkinter import messagebox
LARGE_FONT = ("Verdana", 12)
Receipt = []
prices = []
job_queue = Queue()
Orders = []
class SushiMenuApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (OrderPage, ReceiptPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(OrderPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def sushichef(job_queue):
while True:
a = job_queue.get()
if a is None:
print("There are no more orders")
break
messagebox.showinfo("Message", "Sushi Chef started doing you order")
time.sleep(a)
print("Your order will be coming to you now")
job_queue.task_done()
class OrderPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Sushi Selections", font=LARGE_FONT)
label.grid(row=0, column=0, columnspan=4)
checkout = tk.Button(self, text="Check Out", command=self.show_check_out)
checkout.grid(row=0, column=4)
for _ in range(4): # 4 Sushi Chefs
thread = Thread(target=sushichef, args=(job_queue,))
thread.start()
Orders.append(thread)
shrimptempura = tk.Button(self, text="Shrimp Tempura", command=staction)
shrimptempura.grid(row=1, column=0)
def show_check_out(self):
self.controller.show_frame(ReceiptPage)
self.controller.frames[ReceiptPage].update_labels()
def staction():
for item in [6]:
job_queue.put(item)
prices.append(69)
Receipt.append("Shrimp Tempura")
class ReceiptPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.label = tk.Label(self, text="Receipt", font=LARGE_FONT)
self.label.grid(row=0, columnspan=4, sticky="nsew")
self.fullreceipt= tk.Label(self, text='\n'.join(map(str, Receipt)))
self.fullreceipt.grid(row=1, columnspan=4, rowspan=48)
self.sumprice= tk.Label(self, text=sum(prices))
self.sumprice.grid(row=49, column=1, columnspan=2)
def update_labels(self):
self.fullreceipt['text'] = '\n'.join(map(str, Receipt))
self.sumprice['text'] = sum(prices)
app = SushiMenuApp()
app.mainloop()
要使其更加通用,您可以在每个框架中创建函数 update_frame
(甚至为空函数),然后您可以在 show_frame
To make it more universal you can create in every frame function update_frame
(even empty function) and then you can execute it in show_frame
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
frame.update_frame()
不要使用名称 update()
,因为它被<$ c使用$ c> tkinter 。
这篇关于python:将值附加到类外部的列表中,带有append的函数也在类外部,但函数在类中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!