你如何在 python tkinter 中将 .get() 与多帧购物车一起使用?

How do you use .get() with a multiframe shopping cart in python tkinter?

在此代码中。我想显示我点击“购买”按钮的框架中的信息。我确信这是一个简单的解决方案,但它让我望而却步。我一直在经历 google 和堆栈溢出,但还没有解决方案。

我尝试将 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

Product_Box 是在 create_frame() 中创建的局部变量 - 要在其他函数中访问它,你必须分配给 global 变量 - 所以你需要在 create_frame()

但还有其他问题:

variable = tk.Widget().grid()None 分配给 variable 因为 grid()/pack()/place() returns None .您必须分两步完成 variable = tk.Widget()varaible.grid().

Product_Lab = Label(NewFrame, text="Product")
Product_Lab.grid(row=0, column=0)

现在代码可以运行了,但稍后您将遇到与其他变量相同的问题。


具有其他更改的完整工作代码

PEP 8 -- Style Guide for Python Code

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()