Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter窗口停止自动启动_Python_Tkinter - Fatal编程技术网

Python Tkinter窗口停止自动启动

Python Tkinter窗口停止自动启动,python,tkinter,Python,Tkinter,我想用tkinter构建一个与python商店系统交互的小窗口。但是如果我启动这个窗口,代码也会自动启动simpledialog窗口。我想这是因为我启动simpledialog窗口的方式有点特别 有谁能帮我把产品数据交给下一个函数,以正确的方式启动窗口吗 class Window: product_1 = Product(price=5.40, stock=200, name="Nudel", id="1") product_2 = Pro

我想用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=产品(价格=5.40,库存=200,name=“Nudel”,id=“1”)
产品2=产品(价格=5.30,库存=15,name=“Steine”,id=“2”)
产品3=产品(价格=4.30,库存=200,name=“Tassen”,id=“3”)
商店产品=[产品1、产品2、产品3]
购物车产品=[]
定义初始化(自):
self.main=Tk()
self.main.title=“店铺菜单”
self.label=label(self.main,text=“欢迎!”)
self.products\u button=按钮(self.main,text=“shop”,command=self.build\u shop\u窗口)
self.cart\u按钮=按钮(self.main,text=“cart”,command=self.build\u cart\u窗口)
self.add\u product\u button=按钮(self.main,text=“add”)
self.remove\u product\u button=按钮(self.main,text=“remove”)
self.label.pack()
self.products_button.pack()
self.cart_button.pack()
self.add\u product\u button.pack()
self.remove_产品_按钮.pack()
def添加到购物车(自身、产品):
打印(产品id)
quantity=simpledialog.askfloat(“quantity”,“您是否经常生产考芬?”,parent=self.window1)

如果int(product.stock)将参数传递给 你的钮扣

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

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()
,对于Toplevel,无需说
mainloop()
,例如:

self.window1 = Toplevel()
希望它解决了错误,并愉快地编码:D


干杯

这不是为了修复错误,但不要多次使用
Tk()
,这样你就可以将
self.window1=Tk()
更改为
self.window1=Toplevel()
,也不必调用
mainloop()
,因为它不会使用带有tkinter的类,但我非常确定使用
Tk()
不止一次是不好的。是的,终于成功了,谢谢。但正如你所说,“自动启动”问题还在继续。。。在给定的代码中,我是否使用正确的方法启动了带有按钮的函数“add_to_cart”?哦,等等,你必须使用
lambda
wen-ur为按钮传递参数,比如
command=lambda:self.add_to_cart(self.shop_products[0])
如果仍然不工作,请尝试删除
self
。对所有需要大量参数的按钮也要这样做,只是忘记了lamda函数。终于成功了!