Tkinter窗口停止自动启动

2024-04-24 20:21:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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

有谁能帮我把产品数据交给下一个函数,以正确的方式启动窗口吗

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

非常感谢

汤姆


1条回答
网友
1楼 · 发布于 2024-04-24 20:21:36

将参数传递给函数时,缺少了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

干杯

相关问题 更多 >