首页 > 解决方案 > Tkinter 命令函数调用

问题描述

我是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()

标签: pythontkinterbuttoncommandcall

解决方案


  1. root.mainloop()在你的末尾添加__main__。所以 GUI 开始工作。有关更多信息,请查看此问题
  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()

推荐阅读