Tkinter按钮导致无限循环

时间:2019-05-18 10:11:32

标签: python-3.x user-interface button tkinter infinite-loop

我对编码非常陌生,目前正在尝试创建游戏。我一直在使用一个简单的代码使用tkinter框架和python中的按钮来处理玩家的死亡。当使用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)

0 个答案:

没有答案