安装 Python 版 tkinter
我正在尝试导入 Tkinter
。但是,我收到一条错误消息,指出 Tkinter 尚未安装:
ImportError: No module named _tkinter, please install the python-tk package
我可能可以使用突触管理器安装它(可以吗?),但是,我必须在我编程的每台计算机上安装它。是否可以将 Tkinter 库添加到我的工作区并从那里引用它?
I am trying to import Tkinter
. However, I get an error stating that Tkinter
has not been installed:
ImportError: No module named _tkinter, please install the python-tk package
I could probably install it using synaptic manager (can I?), however, I would have to install it on every machine I program on. Would it be possible to add the Tkinter library into my workspace and reference it from there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
在本地安装 Tkinter 来与系统提供的 Python 一起使用并不是很容易。您可以从源代码构建它,但这对于您显然正在运行的基于二进制包的发行版来说通常不是最好的主意。
在您的计算机上
apt-get install python3-tk
更安全。(适用于 Debian 派生的发行版,例如 Ubuntu;请参阅其他发行版上的包管理器和包列表。)
It is not very easy to install Tkinter locally to use with system-provided Python. You may build it from sources, but this is usually not the best idea with a binary package-based distro you're apparently running.
It's safer to
apt-get install python3-tk
on your machine(s).(Works on Debian-derived distributions like for Ubuntu; refer to your package manager and package list on other distributions.)
实际上,您只需要使用以下命令来安装 python3 的 tkinter:
另外,对于 Fedora 用户,请使用以下命令:
Actually, you just need to use the following to install the tkinter for python3:
In addition, for Fedora users, use the following command:
如果像我一样,由于 IS 中的好朋友,您没有网络上的 root 权限,并且您正在本地安装,则上述方法可能会遇到一些问题。
我在 Google 上花了很长时间 - 但最终,这很容易。
从 http://www.tcl.tk/software/tcltk/ 下载 tcl 和 tk download.html 并在本地安装它们。
要在 Linux 上本地安装(我是在我的主目录中安装),请提取 tcl 和 tk 的 .tar.gz 文件。然后打开 ./unix 目录中的自述文件。我运行了
它可能看起来很痛苦,但是文件很小并且安装速度非常快。
然后在 python 安装目录中重新运行 python setup.py build 和 python setup.py install - 它应该可以工作。它对我有用 - 我现在可以随心所欲地导入 Tkinter 等 - yipidy-yay。整个下午都花在了这个问题上——希望这篇文章可以帮助其他人免受痛苦。
If, like me, you don't have root privileges on your network because of your wonderful friends in I.S., and you are working in a local install you may have some problems with the above approaches.
I spent ages on Google - but in the end, it's easy.
Download the tcl and tk from http://www.tcl.tk/software/tcltk/download.html and install them locally too.
To install locally on Linux (I did it to my home directory), extract the .tar.gz files for tcl and tk. Then open up the readme files inside the ./unix directory. I ran
It may seem a pain, but the files are tiny and installation is very fast.
Then re-run
python setup.py build
andpython setup.py install
in your python installation directory - and it should work. It worked for me - and I can now import Tkinter etc to my heart's content - yipidy-yay. An entire afternoon spent on this - hope this note saves others from the pain.如果您使用的是 Python 3,可能是因为您输入的是
Tkinter
而不是tkinter
If you are using Python 3 it might be because you are typing
Tkinter
nottkinter
对于 Arch Linux 用户来说,它有点像
For Arch Linux users, it goes a bit like
您将需要该包及其依赖项。
既然您提到了 synaptic,那么您必须使用基于 Debian 的系统。获得所需内容的一种方法:
you will need the package and its dependencies.
since you mentioned synaptic, you must be using a Debian based system. one way to get what you need:
对于Python 2.7:
正如此处所说,
就我而言,在 Windows 上,重新安装 Python 发行版很有帮助。很久以前,我没有选中“Tcl/Tk”安装功能。重新安装后,一切正常,我可以
导入_tkinter
和导入Tkinter
。For Python 2.7:
As it says here,
In my case, on Windows, what helped was reinstalling the Python distribution. A long time ago, I had unchecked the "Tcl/Tk" installation feature. After reinstalling, all works fine and I can
import _tkinter
andimport Tkinter
.如果您使用的是 RHEL、CentOS、Oracle Linux 等,您可以使用 yum 安装 tkinter 模块
If you're using RHEL, CentOS, Oracle Linux, etc. You can use yum to install tkinter module
对于 python3 用户,通过以下命令安装 python3-tk 包
<代码>
sudo apt-get 安装 python3-tk
for python3 user, install python3-tk package by following command
sudo apt-get install python3-tk
对于 ubuntu 上的 python 3.7,我必须使用 sudo apt-get install python3.7-tk 才能使其工作
For python 3.7 on ubuntu I had to use
sudo apt-get install python3.7-tk
to make it work我的情况下还需要安装 tk-devel
安装这些并重建 python
tk-devel also needs to be installed in my case
install these and rebuild python
Fedora 版本 25(二十五)
这对我有用。
Fedora release 25 (Twenty Five)
This worked for me.
要在流行的 Linux 发行版上安装 Tkinter:
Debian/Ubuntu:
Fedora:
Arch:
REHL/CentOS6/CentOS7:
OpenSUSE:
To install the Tkinter on popular Linux distros:
Debian/Ubuntu:
Fedora:
Arch:
REHL/CentOS6/CentOS7:
OpenSUSE:
有 _tkinter 和 Tkinter - 都适用于 Py 3.x
但为了安全起见,下载 Loopy 并将你的 python 根目录(如果你使用的是 PyCharms 之类的 IDE)更改为 Loopy 的安装目录。您将获得这个库以及更多其他内容。
There is _tkinter and Tkinter - both work on Py 3.x
But to be safe- Download Loopy and change your python root directory(if you're using an IDE like PyCharms) to Loopy's installation directory. You'll get this library and many more.
如果您使用的是
Python 3
,则必须按如下方式安装:Tkinter for
Python 2
(python-tk
) 与不同Python 3 (
python3-tk
)。If you're using
Python 3
then you must install as follows:Tkinter for
Python 2
(python-tk
) is different fromPython 3
's (python3-tk
).Tkinter 是 python 的 GUI 模块。您可以使用它在 python 中制作基于 GUI 的应用程序。 Tkinter 提供了几个 GUI 小部件,如按钮、菜单、画布、文本、框架、标签等来开发桌面应用程序。虽然 Tkinter 非常流行并且包含在 windows、macosx 安装 python 中,但还有其他选择,如 pyQt、wxPython。 ..
在本教程中,我们将了解如何在 Linux 上安装它并通过示例使用它。
首先,检查是否安装了 python 并检查其版本
打开终端并输入 python。如果已安装,那么它将显示版本等信息,帮助...检查您的版本(我的是 python 2.7.9)
aman@vostro:~$ python
Python 2.7.9(默认,2015 年 4 月 2 日,15:33:21)
[GCC 4.9.2] 在 linux2 上
输入“帮助”、“版权”、“制作人员”或“许可证”以获取更多信息。
如果你没有 python 那么安装它
sudo apt-get install python
如果您想安装 python 3,请输入以下内容。如果您是新手,我会推荐 python 2 而不是 python 3。Python 2 仍然非常流行,许多应用程序都是用它制作的。在 ubuntu 上 python2 仍然是默认的
sudo apt-get install python3
安装 Tkinter
最后,为 python 3
如何使用
现在,让我们通过这个小示例检查 Tkinter 是否运行良好,
打开终端并进入 python shell。
python
用于 python3 的
python3
如果 python 安装正确,您将得到 >>>迅速的。
aman@vostro:~$ python
现在导入 Tkinter 模块。如果正确导入,它不会显示任何错误。注意:确保在 python2 中输入 Tkinter(不是 tkinter),在 python3 中输入 tkinter(不是 Tkinter)。
现在,只是为了检查您是否可以使用 Tkinter 创建一个空窗口。
Tkinter is a GUI module for python. you can use it to make GUI based applications in python. Tkinter provides several GUI widgets like buttons,menu, canvas,text,frame,label etc. to develop desktop applications.Though Tkinter is very popular and is included with windows, macosx install of python, There are also alternative choices like pyQt, wxPython...
In this tutorial we will see how to install it on linux and use it with an example.
First, check if you have python installed and also check its version
Open up your terminal and type python. if its installed then it will show information like version, help... check your version (mine is python 2.7.9)
aman@vostro:~$ python
Python 2.7.9 (default, Apr 2 2015, 15:33:21)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
If you don't have python then install it
sudo apt-get install python
If you want to install python 3 then enter the following. If you are a newbie, I would recommend python 2 instead of python 3. Python 2 is still very popular and many apps are made on it. On ubuntu python2 is still the default
sudo apt-get install python3
Finally, Install Tkinter
for python 3
How to Use it
Now, lets check if Tkinter is working well with this little example
open your terminal and enter into your python shell.
python
for python3
python3
if python was installed correctly you will get a >>> prompt.
aman@vostro:~$ python
Now import Tkinter module. it wont show any error if it got imported correctly. NOTE: Make sure you type Tkinter (not tkinter) in python2 and tkinter (not Tkinter) in python3.
Now, just to check you can create an empty window using Tkinter.
macOS 上的情况仍然有点复杂,但是-able:
Python.org 强烈建议从 ActiveState 下载 tkinter,但你应该< /strong> 首先阅读他们的许可证(提示:不要重新分发或需要支持) 。
当下载打开时,OS X 10.11 拒绝了它,因为它找不到我的收据:“ActiveTcl-8.6.pkg 无法打开,因为它来自身份不明的开发人员”。
我关注了 2012 年的 OSXDaily 修复 建议从任何地方允许。但 OS X 现在添加了“无论如何打开”选项,以允许(例如)Active-Tcl 一次性关闭,并且“任何地方”选项已获得超时。
The situation on macOS is still a bit complicated, but do-able:
Python.org strongly suggest downloading tkinter from ActiveState, but you should read their license first (hint: don't redistribute or want Support).
When the download is opened OS X 10.11 rejected it because it couldn't find my receipt: "ActiveTcl-8.6.pkg can’t be opened because it is from an unidentified developer".
I followed an OSXDaily fix from 2012 which suggested allowing from anywhere. But OS X has now added an "Open Anyway" option to allow (e.g.) Active-Tcl as a once off, and the "Anywhere" option has gained a timeout.
对于 Fedora >= 25 和 python >= 3,我们可能需要包含
Tkinter
的依赖项sudo dnf install python3-tkinter
For Fedora >= 25 and python >= 3, we might need to include the dependencies for
Tkinter
sudo dnf install python3-tkinter
我认为你必须将 tkinter 安装到你的代码存储库目录中。
对于 liunx(Ubuntu 或 debian),打开终端。
通过在终端上运行此命令来安装 tkinter 软件包。
在终端上安装 python-tk 包后,创建一个新的 python 文件。将其命名为文件名.py。
在 filename.py 的第一行,将 tkinter 导入到 python 文件中。
要创建具有不同字段的 tkinter,您可以使用本教程。
https://www.delftstack.com/tutorial/tkinter-tutorial/ tkinter 的文档可以在这里找到:
https://docs.python.org/3.7/library/tkinter.html
希望有帮助。如果您仍然陷入困境,请询问我们。
请记住在实施之前确定您在 tkinter 上的 GUI 中需要什么。绘制 GUI 外观的线框图将有助于编写 Tkinter GUI 代码。
I think you have to install the tkinter onto your code repository directory.
For liunx (Ubuntu or debian), open the terminal.
Install the tkinter package by running this command on the terminal.
After installing the python-tk package on the terminal, create a new python file. Call it filename.py.
On the first line of the filename.py, import tkinter into the python file.
To create a tkinter with different fields, you can use this tutorial.
https://www.delftstack.com/tutorial/tkinter-tutorial/
The documentation of tkinter can be found here:
https://docs.python.org/3.7/library/tkinter.html
Hope that helps. Do ask us if you are still stuck.
Remember to identify what you need in the GUI on tkinter before implementing it. Drawing wireframes of how the GUI will look like will help in coding the Tkinter GUI.
使用 ntk 作为您的桌面应用程序,它在 tkinter 之上工作,为您提供功能更强大且美观的 ui编码。
通过
pip install ntk
安装 ntk此处正确的文档:ntk.readthedocs.io
快乐编码。
Use ntk for your desktop application, which work on top of tkinter to give you more functional and good looking ui in less codding.
install ntk by
pip install ntk
proper Documentation in here: ntk.readthedocs.io
Happy codding.
安装python版本
3.6+
然后打开文本编辑器
或IDE
编写示例代码如下:
Install python version
3.6+
and open you text editor
or ide
write sample code like this:
即使您成功导入了 python3 和 python3-tk,它仍然可能无法工作。我将脚本的第一行更改为:
#! /usr/bin/python
到:
#! /usr/bin/python3
终于成功了!
Even after you've successfully imported python3 and python3-tk, it still might not work. I changed the first line of my script from:
#! /usr/bin/python
to:
#! /usr/bin/python3
It finally worked!
对于 Ubuntu 用户和一般 Linux 用户来说,阅读我发现的提示并不是一个坏主意 此处:
0:不要从系统中清除Python,否则很可能费尽心思重新安装你会错过的东西,包括 ubuntu 桌面。
1:更新本地软件存储库
2:安装每个人都需要的东西:
3:将 Dead Snake 添加到存储库列表中:
4:再次更新整个内容,因为我们安装了东西并添加了新的存储库:
5:安装 Python 版本 您想要的:
6:检查您想要的版本是否已安装:
最后,您可能需要重新启动您的个人电脑。
For Ubuntu users and Linux users in general, it's not a bad idea to read the tips I've found here:
0: Do NOT purge Python from your system otherwise it's very likely you go through hell to re-install the things you will miss, including the ubuntu-desktop.
1: Update your local software repository
2: Install the stuff everybody needs:
3: Add the Dead Snake to your repository list:
4: Update the entire thing again because we installed stuff and added a new repo:
5: Install the Python version you want:
6: Check that the version you want was installed:
Finally, you might want to restart your PC.
您可以简单地通过包管理器(pip)安装它。如果您尚未安装 pip,可以下载最新版本。
并下载 tkinter
pip install tkinter
如果您可能使用多个版本的 python,您可以将其安装在您当前使用的特定版本中。如果是 3.10,则为:
pip3.10 install tkinter
You can simply install it by your package manager(pip). If you haven't installed pip yet you can download the latest version of it.
And to download tkinter
pip install tkinter
And if you are maybe using a few versions of python, you can install it in the specific version you are currently using. if it's 3.10, it is,
pip3.10 install tkinter