You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python脚本启动时other_modules.Main自动运行问题求助

解决Tkinter启动时自动执行游戏模块的问题

问题核心是你给Start按钮绑定命令的方式错了——你直接调用了Startgame()函数,而非传递函数本身。Tkinter的command参数需要的是可调用对象(仅写函数名,不带括号),加括号会导致程序启动时立刻执行函数,而非等按钮点击触发。

修复步骤:

  1. 修正按钮绑定代码
    把原来的错误写法:
start_btn = Button(root, text="Start", command=Startgame())

改成:

start_btn = Button(root, text="Start", command=Startgame)

注意去掉Startgame后的括号,这样按钮点击时才会触发函数执行。

  1. 确认Startgame函数逻辑
    确保Startgame内部仅在被调用时才执行游戏模块,示例如下:
def Startgame():
    # 可选:添加启动前的准备逻辑
    other_modules.Main()  # 仅点击按钮时才会执行这一行

如果需要给Startgame传递参数,用lambda包裹即可:

start_btn = Button(root, text="Start", command=lambda: Startgame("自定义参数1", "自定义参数2"))

修改后,只有点击Start按钮时,才会触发Startgame函数并运行other_modules.Main模块,其他按钮的正常逻辑不受影响。

内容的提问来源于stack exchange,提问作者Jed The Student

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 11:59:55