【问题标题】:Function only works once python函数只工作一次 python
【发布时间】:2018-09-27 10:56:22
【问题描述】:

我将 tkinter 用作具有函数的类,当我运行一个函数导致另一个函数时,再次以再次运行第一个函数结束,但是原始函数不起作用。请帮忙

def idle(self):
    self.next.destroy()
    self.info.destroy()

    self.prompt.configure(text=city_line + ", What do you want to do?")

    self.backpack = tk.Button(self, text="Backpack", command=self.backpack)
    self.shop = tk.Button(self, text="Shop")
    self.wander = tk.Button(self, text="Wander", command=self.wander)
    self.travel = tk.Button(self, text="Travel", command=self.travel)

    self.backpack.pack(side="right", padx=5, pady=5)
    self.shop.pack(side="right", padx=5, pady=5)
    self.wander.pack(side="right", padx=5, pady=5)
    self.travel.pack(side="right", padx=5, pady=5)

def backpack(self):
    self.backpack.destroy()
    self.shop.destroy()
    self.wander.destroy()
    self.travel.destroy()
    self.output.destroy()
    self.info = tk.Message(self, width=150, text=backpacke + ". Gold: " + str(gold))
    self.output = tk.Label(self, text="")

    self.next = tk.Button(self, text="Next", command=self.idle)
    self.info.pack(side="top", fill="x", pady=8)
    self.output.pack(side="top", fill="x", expand=True)
    self.next.pack(side="right", padx=5, pady=5)

【问题讨论】:

  • 请发布完整的课程以及您如何调用课程方法。

标签: python function tkinter


【解决方案1】:

当你的类被创建时,你有一个名为backpack 的方法。在类内部,self.backpack 指的是这个方法。当这个方法被调用时,它会执行self.backpack = tk.Button(...),这会有效地破坏该方法并将其替换为小部件。下次您尝试调用self.backpack 时,您调用的是小部件而不是您的方法。

您需要为保存小部件的方法或变量选择不同的名称。

【讨论】:

    猜你喜欢
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多