简体   繁体   English

Tkinter 窗口未在 pycharm 中打开

[英]Tkinter window not opening in pycharm

I am eriting code in pycharm with tkinter but the window is not opening.我正在使用 tkinter 在 pycharm 中编辑代码,但窗口没有打开。 May someone assist?有人可以帮忙吗? ` `

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

` `

i tried checking my script for bugs but nothing我试着检查我的脚本是否有错误但没有

This has nothing to do with Pycharm, but with tkinter library and how to use it.这与 Pycharm 无关,而是与 tkinter 库以及如何使用它有关。

You are missing 2 important stuff:您缺少 2 个重要的东西:

  • Button is in ttk.py file inside tkinter library: from tkinter import ttk按钮位于 tkinter 库内的 ttk.py 文件中: from tkinter import ttk
  • Execute the whole script with mainloop使用mainloop执行整个脚本

Try this:试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM