python-3.x - 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)
解决方案
推荐阅读
- javascript - req.body 是空的,为什么?
- android - 尽管调试 apk 运行良好,但 Flutter 发布 apk 无法正确重建我的小部件
- c++ - 为什么 C++ 禁止非模板 std:: 类型的前向声明?
- ssis - 如何将 SSIS 日期变量转换为字符串以返回 2 位数月份值?
- c++ - 事件查看器(C++ APP):在一般选项卡中显示消息
- javascript - 在 React Native 中动画组件卸载淡出
- c# - 制作一个仅对从控制台输入的偶数求和的程序
- charts - 在交易视图中禁用股票/指数选择
- sql - SQL - 从没有 UNION 的条件连接中选择一个值
- json - Golang 可变字段