在這段代碼中。我想顯示我點擊“購買”按鈕的框架中的資訊。我確信這是一個簡單的解決方案,但它讓我望而卻步。我一直在瀏覽谷歌和堆疊溢位,但還沒有解決方案。
我嘗試將 NewFrame 設為全域,但這只是最后一個所做的。
from tkinter import *
root = Tk()
FrameNum = 1
ItemCount = 1
def combine_funcs(*funcs):
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return combined_func
def place_order():
global ItemCount
Product = Product_Box.get()
Label(root, text="Your Purchase of " str(Product) " Placed").grid(row=1, column=3, stick="w")
ItemCount = 1
def create_frame():
global FrameNum
NewFrame = LabelFrame(root,name=("newframe" str(FrameNum)), text=("Item " str(FrameNum)),width=100, height=100)
NewFrame.grid(row=FrameNum, column=0)
Product_Lab = Label(NewFrame, text="Product").grid(row=0, column=0)
Qty_Lab = Label(NewFrame, text="Qty").grid(row=0, column=1)
Price_Lab = Label(NewFrame, text="Price").grid(row=0, column=2)
# Entry Boxes
Product_Box = Entry(NewFrame, width=10).grid(row=1, column=0)
Qty_Box = Entry(NewFrame, width=12).grid(row=1, column=1)
Price_box = Entry(NewFrame, width=6).grid(row=1, column=2)
Remove_Bet_Button = Button(NewFrame, text="Del", command=NewFrame.destroy).grid(row=3, column=2)
Buy_Button = Button(NewFrame, text="Buy", command=combine_funcs(place_order, NewFrame.destroy)).grid(row=3, column=0)
FrameNum = 1
framebutton = Button(root, text="Frame", command=create_frame).grid(row=0, column = 3, stick ="n")
root.mainloop()
點擊“購買”按鈕時出現此錯誤。(無論是否在該框架的框中輸入資訊)
NameError: name 'Product_Box' is not defined
uj5u.com熱心網友回復:
Product_Box是區域變數中創建create_frame()-在其他功能上訪問它,你必須分配給global變數-所以你需要添加global Product_Box在create_frame()
但還有一個問題:
variable = tk.Widget().grid()受讓人None到variable因為grid()/ pack()/place()回報None。你要做的兩個步驟variable = tk.Widget()和varaible.grid()。
Product_Lab = Label(NewFrame, text="Product")
Product_Lab.grid(row=0, column=0)
現在代碼可以作業,但稍后您將遇到與其他變數相同的問題。
具有其他更改的完整作業代碼
PEP 8 -- Python 代碼風格指南
import tkinter as tk # PEP8: `import *` is not preferred
# --- functions ---
def place_order(product_box, qty_box, price_box, new_frame):
global item_count
global label
product = product_box.get()
qty = int(qty_box.get())
price = int(price_box.get())
total = qty * price
if not label:
label = tk.Label(root)
label.grid(row=1, column=3, stick="w")
# replace text
#label['text'] = f"Your Purchase of {product} Placed. (Qty: {qty}, Price: {price}, Total: {total})"
# or append in new line
if len(label['text']) > 0:
label['text'] = "\n"
label['text'] = f"Your Purchase of {product} Placed. (Qty: {qty}, Price: {price}, Total: {total})"
item_count = 1
new_frame.destroy()
def create_frame():
global frame_number
new_frame = tk.LabelFrame(root, name=f"newframe {frame_number}", text=f"Item {frame_number}", width=100, height=100)
new_frame.grid(row=frame_number, column=0)
tk.Label(new_frame, text="Product").grid(row=0, column=0)
tk.Label(new_frame, text="Qty").grid(row=0, column=1)
tk.Label(new_frame, text="Price").grid(row=0, column=2)
# Entry Boxes
product_box = tk.Entry(new_frame, width=10)
product_box.grid(row=1, column=0)
qty_box = tk.Entry(new_frame, width=12)
qty_box.grid(row=1, column=1)
price_box = tk.Entry(new_frame, width=6)
price_box.grid(row=1, column=2)
tk.Button(new_frame, text="Del", command=new_frame.destroy).grid(row=3, column=2)
tk.Button(new_frame, text="Buy", command=lambda:place_order(product_box, qty_box, price_box, new_frame)).grid(row=3, column=0)
frame_number = 1
# --- main ---
frame_number = 1
item_count = 1
root = tk.Tk()
tk.Button(root, text="Frame", command=create_frame).grid(row=0, column=3, stick="n")
label = None # it will add later but only once
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383493.html
上一篇:如何將字串轉換為浮點數
