首页 > 解决方案 > Tkinter 窗口停止自动启动

问题描述

我想建立一个小窗口来与我的 python 商店系统与 tkinter 交互。但是,如果我启动窗口,代码也会自动启动我的 simpledialog 窗口。我认为这是因为我启动 simpledialog 窗口的方式有点特别。

任何人都可以通过将 product_data 提供给下一个函数来帮助我如何以正确的方式启动窗口吗?

class Window:
    product_1 = Product(price=5.40, stock=200, name="Nudel", id="1")
    product_2 = Product(price=5.30, stock=15, name="Steine", id="2")
    product_3 = Product(price=4.30, stock=200, name="Tassen", id="3")

    shop_products = [product_1, product_2, product_3]
    cart_products = []
    def __init__(self):
        self.main = Tk()
        self.main.title = "Shop Menu"
        self.label = Label(self.main, text="Welcome!")
        self.products_button = Button(self.main, text="shop", command=self.build_shop_window)
        self.cart_button = Button(self.main, text="cart", command=self.build_cart_window)
        self.add_product_button = Button(self.main, text="add")
        self.remove_product_button = Button(self.main, text="remove")
        self.label.pack()
        self.products_button.pack()
        self.cart_button.pack()
        self.add_product_button.pack()
        self.remove_product_button.pack()

    def add_to_cart(self, product):
        print(product.id)
        quantity = simpledialog.askfloat("Quantity", "Wie oft möchten Sie das Produkt kaufen?", parent=self.window1)
        if int(product.stock) <= quantity:
            messagebox.showinfo(title = 'Shop', message = 'Leider haben wir die geforderte Menge des Produktes ' + str(product.name) + " nicht auf Lager. Bitte bestellen Sie zunächst eine kleinere Menge, neue Ware ist bereits auf dem Weg!")
            #Message an Betreiber, dass Produkt bestellt werden muss (Abhängig von Verkaufsstatistik)



    def build_shop_window(self):
        self.window1 = Tk()
        self.window1.title("Shop")
        self.label = Label(self.window1, text="Shop")
        self.label_1 = Label(self.window1, text=str(self.product_1.name) + str(self.product_1.price) + "€ Noch verfügbar: " + str(self.product_1.stock))
        self.button_1 = Button(self.window1, text ="In den Warenkorb", command=self.add_to_cart(self.shop_products[0]))
        self.label_2 = Label(self.window1, text=str(self.product_2.name) + str(self.product_2.price) + "€ Noch verfügbar: " + str(self.product_2.stock))
        self.button_2 = Button(self.window1, text ="In den Warenkorb", command=self.add_to_cart(self.shop_products[1]))
        self.label_3 = Label(self.window1, text=str(self.product_3.name) + str(self.product_3.price) + "€ Noch verfügbar: " + str(self.product_3.stock))
        self.button_3 = Button(self.window1, text ="In den Warenkorb", command=self.add_to_cart(self.shop_products[2]))

        self.label.grid(row=0,column=0)
        self.label_1.grid(row=1,column=0)
        self.button_1.grid(row=1,column=1)
        self.label_2.grid(row=2,column=0)
        self.button_2.grid(row=2,column=1)
        self.label_3.grid(row=3,column=0)
        self.button_3.grid(row=3,column=1)

    def run(self):
        self.main.mainloop()

非常感谢!

汤姆

标签: pythontkinter

解决方案


lambda当您将参数传递给按钮的函数时,您会错过,

将按钮更改为以下内容将修复错误:

self.button_1 = Button(self.window1, text ="In den Warenkorb", command=lambda: self.add_to_cart(self.shop_products[0]))
self.button_2 = Button(self.window1, text ="In den Warenkorb", command=lambda: self.add_to_cart(self.shop_products[1]))
self.button_3 = Button(self.window1, text ="In den Warenkorb", command=lambda: self.add_to_cart(self.shop_products[2]))

此外,作为建议,Tk()仅用于主窗口且不超过一次,因此Tk()除主窗口之外的所有其他窗口都应替换为顶层窗口Toplevel(),无需多说mainloop(),例如:

self.window1 = Toplevel()

希望它解决了错误,并且编码愉快:D

干杯


推荐阅读