首页 > 解决方案 > Tkinter 按钮导致无限循环

问题描述

我对编码非常陌生,目前正在尝试创建游戏。我一直在编写一个简单的代码来使用 python 中的 tkinter 帧和按钮来处理玩家的死亡。当使用 tk.Button(command = lambda: self.new()) 我的代码会产生无限量的死亡弹出窗口。有没有办法来解决这个问题?问题是 configure = ... 。没有这条线, deny = ... 可以正常工作。

 def handle_death(self):
    death = tk.Tk()
    death.title("You Have Died")
    label = tk.Label(death, text="Would You Like to Restart?")
    label.pack(side=tk.TOP, pady=10, padx=10)
    death_button = tk.Frame(death)
    deny = tk.Button(death_button, text="No", command=lambda: [self.close(), death.destroy()])
    configure = tk.Button(death_button, text="Yes", command=lambda: [self.new(), death.destroy()])
    configure.pack(side=tk.LEFT)
    deny.pack(side=tk.LEFT)
    death_button.pack(side=tk.BOTTOM)
 def new(self):
    """ Creates a new game."""
    self._world = World((GRID_WIDTH, GRID_HEIGHT), BLOCK_SIZE)
    load_simple_world(self._world)
    self._world.add_player(self._player, 250, 150)
    self._hot_bar.select((0, 0))
    starting_inventory = [
        ((1, 5), Stack(Item('dirt'), 10)),
        ((0, 2), Stack(Item('wood'), 10)),
    ]
    self._inventory = Grid(rows=3, columns=10)
    for position, stack in starting_inventory:
        self._inventory[position] = stack

    self._health = 1
    self._food = 1
    self._update_health(self._health)
    self._update_food(self._food)

标签: python-3.xuser-interfacebuttontkinterinfinite-loop

解决方案


推荐阅读