pycharm中kinter窗口未打开



我正在用tkinter在pycharm中编写代码,但窗口没有打开。有人来帮忙吗?'

import tkinter
window = tkinter.Tk()
button = tkinter.Button(window, text="Do not press this button! >:-(", width=40)
button.pack(padx=10, pady=10)

我试着检查我的脚本的错误,但没有

这与Pycharm无关,而是与tkinter库以及如何使用它有关。

你错过了两个重要的东西:

  • 按钮在tkinter库中的ttk.py文件中:from tkinter import ttk
  • 使用mainloop
  • 执行整个脚本

试试这个:

import tkinter
from tkinter import ttk  # Import ttk file from tkinter library

window = tkinter.Tk()
window.title("Coolest title ever written")
button = ttk.Button(window, text="Do not press this button! >:-(", width=40)  # Use Button from the import ttk file
button.pack(padx=10, pady=10)
window.mainloop()  # Execute the whole script

最新更新