【问题标题】:How to solve '_tkinter' module not found?如何解决找不到“_tkinter”模块?
【发布时间】:2019-09-10 08:54:58
【问题描述】:

我一直在使用 tkinter 在 python 中学习 GUI,但我的第一个代码无法正常工作,并且错误提示 modulenotfounderror: '_tkinter' not found。

我已经安装了 python-tk 以及 python3-tk 包。

注意:请在回答时注意 tkinter 的下划线。 如何解决这个问题?

from tkinter import *
root = Tk()
Label(root, text="Hello World")
Label.pack()
root.mainloop()

【问题讨论】:

  • 什么系统 - Windows、Linux、Mac? Python 应该已经预装了tkinter,但是在一些不使用 GUI 的 Linux 服务器上,我看到没有 Tkinter 的 Python。 Tkinter 使用语言 tcl 和它的模块 tk 所以你可能也需要安装它。在 Linux Ubuntu 上,您使用 apt 安装 tcl/tk,而不是 pip
  • 首先Label.pack() 不是你打包小部件的方式使用Label(root, text="Hello World").pack()L = Label(root, text=' '), L.pack() 但这不会解决你得到的错误,tkinter 是一个构建-在库中,因此它可能与您安装的 python 有关。如果遇到任何错误,请在 cmd 中尝试 python -m tkinter 重新安装 python。
  • Linux 是我正在使用的操作系统
  • 哪个 Linux 发行版? Ubuntu?

标签: python python-3.x tkinter


【解决方案1】:

重新安装python,因为python已经升级,一些包的名字也改了。

from tkinter import *
root = Tk()
Label(root, text="Hello World").pack()
root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2021-08-31
    • 2020-02-23
    • 2019-04-25
    • 2021-10-13
    • 2021-04-08
    • 1970-01-01
    相关资源
    最近更新 更多