在 PyCharm 中运行时,Tkinter 窗口显示为黑色
无论我如何指定背景颜色,Tkinter 背景在运行脚本时都会显示为黑色。
我在 macOS 12.2.1 上使用 PyCharm CE 2021.3.2。
Python解释器 = Python 3.8,有5个包(如下):
- Pillow 9.0.1
- future 0.18.2
- pip 22.0.3
- setuptools 57.0.0
- Wheel 0.36.2
窗口如下所示: 黑色、空白的 Tkinter 窗口
我尝试过:
import tkinter as tk
window = tk.Tk()
window.title("Test")
window.geometry("600x400")
window.mainloop()
尝试使用 window.configure( bg="white")
以及 window['bg'] = "white"
和 window['background'] = "white"
为 no有用。
Tkinter background appears black upon running script no matter how I attribute the background colour.
I'm using PyCharm CE 2021.3.2 on macOS 12.2.1.
Python Interpreter = Python 3.8 with 5 packages (as follows):
- Pillow 9.0.1
- future 0.18.2
- pip 22.0.3
- setuptools 57.0.0
- wheel 0.36.2
Window looks like this:
Black, blank Tkinter window
I've tried:
import tkinter as tk
window = tk.Tk()
window.title("Test")
window.geometry("600x400")
window.mainloop()
Tried changing with window.configure(bg="white")
as well as window['bg'] = "white"
and window['background'] = "white"
to no avail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道是否还有其他人仍然坚持这个问题,这篇文章解决了我的问题。
基本上,如果
tcl-tk
尚未安装,那么在安装 python 时,pyenv
会获取错误版本的tkinter (tcl-tk)
提前酿造
。以下是命令:
下面的命令与上述帖子完全相同,但我修改了每条评论,以使其对新手更友好。
卸载两者,
$brew uninstall tcl-tk
用 homebrew 卸载 tcl-tk$pyenv uninstall 3.10.4
卸载 pyenv 中当前的 python。最后一部分是你的Python的当前版本,可以使用python --version
检查,然后以正确的顺序重新安装,
$brew install tcl-tk
重新安装tcl-tk ,不需要指定版本$ pyenv install 3.10.4
重新安装 Python,并将pyenv
$ pyenv global 3.10.4
设置为全局 Python版本这个问题应该消失。
一些旁注:
pyenv
或其他工具来为python设置虚拟环境,进一步的阅读可以参见此处。Idk if anyone else is still stuck with this, this post solved my problem.
Basically it's the
pyenv
grabbing the wrong version oftkinter (tcl-tk)
when installing python, iftcl-tk
hasn't been installed withbrew
beforehand.Here are the commands:
The commands below are exactly the same as the aforementioned post, but I've modified each comment to try to make them more newbie-friendly.
Uninstall both,
$ brew uninstall tcl-tk
uninstall tcl-tk with homebrew$ pyenv uninstall 3.10.4
uninstall current python in pyenv. the last part is the current version of your python, which can be checked withpython --version
then reinstall with correct order,
$ brew install tcl-tk
reinstall tcl-tk, version shouldn't need to be specified$ pyenv install 3.10.4
reinstall Python withpyenv
$ pyenv global 3.10.4
set as global Python versionthis problem should go away.
Some side notes:
pyenv
or other tools to setup a virtual environment for python, further readings can be seen here.感谢 @typedecker
问题是 Python 3.8 和 Monterey 更新。
修复:
首先安装 Python 3.10,然后按照以下教程操作:
创建 Python 3.10 虚拟环境
然后只需选择在 PyCharms 中新创建的虚拟环境并运行。
Thanks to @typedecker
Issue was with Python 3.8 and the Monterey update.
Fix:
First install Python 3.10 then follow this tutorial:
Creating Python 3.10 Virtual Env
Then simply select the newly created virtual env in PyCharms and run.
这对我在蒙特利的 pycharm 有用。
安装了python3.10
然后进入:
首选项
This worked for me in pycharm on Monterey.
Installed python3.10
Then go to:
Preferences