Python: Appending A Value To A List Outside The Class, Function With Append Also Outside The Class, But Function Is Called Within A Class
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
Solution 1:
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
You will have to also use self.
for labels so you have access to labels in other functions.
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
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()
and button executes this function
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()
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()
Don't use name update()
because it is used by tkinter
.
Post a Comment for "Python: Appending A Value To A List Outside The Class, Function With Append Also Outside The Class, But Function Is Called Within A Class"