在python3.7下无法识别tkinter

2024-04-19 19:09:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是python3.7,据我所知,它已经自带了tkinter。在

import tkinter as tk在我的智能思想中引起了警告:

“Python 2.7版没有tkinter模块”

在|首选项->;项目:'Project name'->;Project interpretor |下,显然是选择的3.7版本。在

实际上,本规范:

import tkinter as tk

root = tk.Tk()

root.title("mein GUI")

root.resizable(False, False)
w = 500  # width for the Tk root
h = 500  # height for the Tk root

sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()

x = (sw / 2) - (w / 2)
y = (sh / 2) - (h / 2)

root.geometry('%dx%d+%d+%d' % (w, h, x, y))
print("test")

运行,打印“test”并且不抛出异常,但是没有窗口出现。在


我还清楚地知道,Tkinter用于3.0以下的Python版本,tkinter用于等于或高于3.0的版本


Tags: theimportgt版本projectfalsefortkinter
2条回答

您需要在代码末尾添加root.mainloop()以显示窗口。这可能是this question的间接副本,但是在您的例子中,您的代码只需要在结尾处使用它,它对我有效。在

import tkinter as tk

root = tk.Tk()

root.title("mein GUI")

root.resizable(False, False)
w = 500  # width for the Tk root
h = 500  # height for the Tk root

sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()

x = (sw / 2) - (w / 2)
y = (sh / 2) - (h / 2)

root.geometry('%dx%d+%d+%d' % (w, h, x, y))
print("test")
root.mainloop()

import tkinter as tk

root = tk.Tk()

root.title("mein GUI")

root.resizable(False, False)
w = 500  # width for the Tk root
h = 500  # height for the Tk root

sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()

x = (sw / 2) - (w / 2)
y = (sh / 2) - (h / 2)

root.geometry('%dx%d+%d+%d' % (w, h, x, y))
print("test")
root.mainloop()

添加根.mainloop()到代码末尾,窗口将出现

相关问题 更多 >