【问题标题】:Tkinter command function callTkinter 命令函数调用
【发布时间】:2021-05-12 20:29:32
【问题描述】:

我是 python 新手, 自从我开始使用 python 以来,Tkinter 中的命令调用对我来说从来没有用过 我已经尝试了所有方法,我取下了刹车,但它仍然不起作用 我在这里看到了相关主题,但它们对我不起作用 这是我尝试过的一个简单代码,当我单击按钮时没有任何反应,那么问题出在哪里?

from tkinter import *
if __name__ == "__main__":
    
    root = Tk()
    root.geometry("400x400")
    compile_button=Button(root,text="Compiler",command=root.quit)
    compile_button.pack()

【问题讨论】:

  • compile_button.pack()之后添加root.mainloop()可以正常工作。

标签: python tkinter button command call


【解决方案1】:
  1. __main__ 末尾添加root.mainloop()。所以 GUI 开始工作。如需更多信息,请查看此question
  2. 也可以使用root.destroy方法。

整个代码:

from tkinter import *

if __name__ == "__main__":
    root = Tk()
    root.geometry("400x400")
    compile_button=Button(root,text="Compiler", command=root.quit)
    compile_button.pack()
    root.mainloop()

【讨论】:

  • 嗯。你使用的是什么版本的 Tkinter?您是从命令行还是在某些 IDE 中执行 python 文件?你确定你复制的代码正确吗?
  • 我正在使用python ide
  • Tkinter 8.6 版
  • 不,只是什么都没发生
猜你喜欢
  • 1970-01-01
  • 2013-03-13
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多