安装 Python 版 tkinter

发布于 2024-10-14 03:55:37 字数 287 浏览 2 评论 0原文

我正在尝试导入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(24

攀登最高峰 2024-10-21 03:55:37

在本地安装 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.)

风吹短裙飘 2024-10-21 03:55:37

实际上,您只需要使用以下命令来安装 python3 的 tkinter:

sudo apt-get install python3-tk

另外,对于 Fedora 用户,请使用以下命令:

sudo dnf install python3-tkinter

Actually, you just need to use the following to install the tkinter for python3:

sudo apt-get install python3-tk

In addition, for Fedora users, use the following command:

sudo dnf install python3-tkinter
暗恋未遂 2024-10-21 03:55:37

如果像我一样,由于 IS 中的好朋友,您没有网络上的 root 权限,并且您正在本地安装,则上述方法可能会遇到一些问题。

我在 Google 上花了很长时间 - 但最终,这很容易。

http://www.tcl.tk/software/tcltk/ 下载 tcl 和 tk download.html 并在本地安装它们。

要在 Linux 上本地安装(我是在我的主目录中安装),请提取 tcl 和 tk 的 .tar.gz 文件。然后打开 ./unix 目录中的自述文件。我运行了

cd ~/tcl8.5.11/unix
./configure --prefix=/home/cnel711 --exec-prefix=/home/cnel711
make
make install

cd ~/tk8.5.11/unix
./configure --prefix=/home/cnel711 --exec-prefix=/home/cnel711 --with-tcl=/home/cnel711/tcl8.5.11/unix
make
make install

它可能看起来很痛苦,但是文件很小并且安装速度非常快。

然后在 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

cd ~/tcl8.5.11/unix
./configure --prefix=/home/cnel711 --exec-prefix=/home/cnel711
make
make install

cd ~/tk8.5.11/unix
./configure --prefix=/home/cnel711 --exec-prefix=/home/cnel711 --with-tcl=/home/cnel711/tcl8.5.11/unix
make
make install

It may seem a pain, but the files are tiny and installation is very fast.

Then re-run python setup.py build and python 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.

旧伤还要旧人安 2024-10-21 03:55:37

如果您使用的是 Python 3,可能是因为您输入的是 Tkinter 而不是 tkinter

If you are using Python 3 it might be because you are typing Tkinter not tkinter

╭⌒浅淡时光〆 2024-10-21 03:55:37

对于 Arch Linux 用户来说,它有点像

sudo pacman -S tk

For Arch Linux users, it goes a bit like

sudo pacman -S tk
甜心小果奶 2024-10-21 03:55:37

您将需要该包及其依赖项。

既然您提到了 synaptic,那么您必须使用基于 Debian 的系统。获得所需内容的一种方法:

sudo apt-get install python-tk

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:

sudo apt-get install python-tk
゛清羽墨安 2024-10-21 03:55:37

对于Python 2.7:

正如此处所说,

您不需要下载 Tkinter - 它是所有 Python 发行版不可或缺的一部分(不支持 Tcl/Tk 的平台的二进制发行版除外)。

就我而言,在 Windows 上,重新安装 Python 发行版很有帮助。很久以前,我没有选中“Tcl/Tk”安装功能。重新安装后,一切正常,我可以导入_tkinter导入Tkinter

For Python 2.7:

As it says here,

You don't need to download Tkinter - it's an integral part of all Python distributions (except binary distributions for platforms that don't support Tcl/Tk).

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 and import Tkinter.

往日 2024-10-21 03:55:37

如果您使用的是 RHEL、CentOS、Oracle Linux 等,您可以使用 yum 安装 tkinter 模块

yum install tkinter

If you're using RHEL, CentOS, Oracle Linux, etc. You can use yum to install tkinter module

yum install tkinter
命比纸薄 2024-10-21 03:55:37

对于 python3 用户,通过以下命令安装 python3-tk 包
<代码>
sudo apt-get 安装 python3-tk

for python3 user, install python3-tk package by following command

sudo apt-get install python3-tk

带上头具痛哭 2024-10-21 03:55:37

对于 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

不气馁 2024-10-21 03:55:37

我的情况下还需要安装 tk-devel

yum install -y tkinter tk-devel

安装这些并重建 python

tk-devel also needs to be installed in my case

yum install -y tkinter tk-devel

install these and rebuild python

不气馁 2024-10-21 03:55:37

Fedora 版本 25(二十五)

dnf install python3-tkinter

这对我有用。

Fedora release 25 (Twenty Five)

dnf install python3-tkinter

This worked for me.

太阳公公是暖光 2024-10-21 03:55:37

要在流行的 Linux 发行版上安装 Tkinter:

Debian/Ubuntu:

sudo apt install python3-tk -y  

Fedora:

sudo dnf install -y python3-tkinter

Arch:

sudo pacman -Syu tk --noconfirm 

REHL/CentOS6/CentOS7:

sudo yum install -y python3-tkinter

OpenSUSE:

sudo zypper in -y python-tk

To install the Tkinter on popular Linux distros:

Debian/Ubuntu:

sudo apt install python3-tk -y  

Fedora:

sudo dnf install -y python3-tkinter

Arch:

sudo pacman -Syu tk --noconfirm 

REHL/CentOS6/CentOS7:

sudo yum install -y python3-tkinter

OpenSUSE:

sudo zypper in -y python-tk
梦屿孤独相伴 2024-10-21 03:55:37

有 _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.

沒落の蓅哖 2024-10-21 03:55:37

如果您使用的是 Python 3,则必须按如下方式安装:

sudo apt-get update
sudo apt-get install python3-tk

Tkinter for Python 2 (python-tk) 与 不同Python 3 (python3-tk)。

If you're using Python 3 then you must install as follows:

sudo apt-get update
sudo apt-get install python3-tk

Tkinter for Python 2 (python-tk) is different from Python 3's (python3-tk).

茶色山野 2024-10-21 03:55:37

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

sudo apt-get install python-tk

最后,为 python 3

sudo apt-get install python3-tk

如何使用

现在,让我们通过这个小示例检查 Tkinter 是否运行良好,

打开终端并进入 python shell。
python

用于 python3 的
python3

如果 python 安装正确,您将得到 >>>迅速的。
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.
>>>

现在导入 Tkinter 模块。如果正确导入,它不会显示任何错误。注意:确保在 python2 中输入 Tkinter(不是 tkinter),在 python3 中输入 tkinter(不是 Tkinter)。

>>>import Tkinter

现在,只是为了检查您是否可以使用 Tkinter 创建一个空窗口。

>>>Tkinter.Tk()

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

sudo apt-get install python-tk

for python 3

sudo apt-get install python3-tk

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

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.
>>>

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.

>>>import Tkinter

Now, just to check you can create an empty window using Tkinter.

>>>Tkinter.Tk()
请爱~陌生人 2024-10-21 03:55:37

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.

New warning for "Anywhere"

傲影 2024-10-21 03:55:37

对于 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

朮生 2024-10-21 03:55:37

我认为你必须将 tkinter 安装到你的代码存储库目录中。

对于 liunx(Ubuntu 或 debian),打开终端。
通过在终端上运行此命令来安装 tkinter 软件包。

sudo apt-get install python-tk 

在终端上安装 python-tk 包后,创建一个新的 python 文件。将其命名为文件名.py。

在 filename.py 的第一行,将 tkinter 导入到 python 文件中。

import tkinter

要创建具有不同字段的 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.

sudo apt-get install python-tk 

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.

import tkinter

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.

雪若未夕 2024-10-21 03:55:37

使用 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.

东走西顾 2024-10-21 03:55:37

安装python版本3.6+
然后打开文本编辑器
或IDE
编写示例代码如下:

from tkinter import *

root = Tk()
root.title("Answer")

root.mainloop()

Install python version 3.6+
and open you text editor
or ide
write sample code like this:

from tkinter import *

root = Tk()
root.title("Answer")

root.mainloop()
_畞蕅 2024-10-21 03:55:37

即使您成功导入了 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!

注定孤独终老 2024-10-21 03:55:37

对于 Ubuntu 用户和一般 Linux 用户来说,阅读我发现的提示并不是一个坏主意 此处

0:不要从系统中清除Python,否则很可能费尽心思重新安装你会错过的东西,包括 ubuntu 桌面。

1:更新本地软件存储库

sudo apt update

2:安装每个人都需要的东西:

sudo apt install software-properties-common

3:将 Dead Snake 添加到存储库列表中:

sudo add-apt-repository ppa:deadsnakes/ppa

4:再次更新整个内容,因为我们安装了东西并添加了新的存储库:

sudo apt update

5:安装 Python 版本 您想要的:

sudo apt install python3.10

6:检查您想要的版本是否已安装:

python3 --version

最后,您可能需要重新启动您的个人电脑。

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

sudo apt update

2: Install the stuff everybody needs:

sudo apt install software-properties-common

3: Add the Dead Snake to your repository list:

sudo add-apt-repository ppa:deadsnakes/ppa

4: Update the entire thing again because we installed stuff and added a new repo:

sudo apt update

5: Install the Python version you want:

sudo apt install python3.10

6: Check that the version you want was installed:

python3 --version

Finally, you might want to restart your PC.

孤独岁月 2024-10-21 03:55:37

您可以简单地通过包管理器(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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文