gpt4 book ai didi

python - Mac 上的 Tkinter 显示为黑屏

转载 作者:行者123 更新时间:2023-12-05 03:21:01 40 4
gpt4 key购买 nike

这是我的代码:

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

但运行后唯一出现在窗口上的是黑屏

you can see the code and the window in this image if it helps

最佳答案

经过大量挖掘,我找到了一个解决方案(有一些注意事项)-你需要同时安装 homebrewpyenv 才能工作。这个想法是用一个实际有效的最新安装替换旧的已弃用的 tkinter 安装*

Note that this will wipe out any packages you’ve installed with pip - back those up first!

运行以下命令

  1. brew uninstall tcl-tk 卸载旧的 tk,如果有的话

  2. pyenv uninstall 3.10.5 ...或者您当前的全局 Python 版本是什么

  3. brew install tcl-tk 获取全新安装的 tk

  4. pyenv install 3.10.5 重新安装 Python 3.10.5(或任何一个)

  5. pyenv global 3.10.5 设置你的全局 Python 版本(匹配你刚才安装的版本)

在使用 pyenv 安装 Python 之前,您需要通过 homebrew 安装 tk 因为 pyenv 会自动尝试使用安装 Python 时可以找到的任何 tk 包。

最后的想法

  • 如果您还没有安装homebrewhere are good instructions

  • 如果您没有pyenv,只需运行brew install pyenv

  • 您可能需要在 VSCode 中再次选择您喜欢的 Python 解释器

*这对我有用 - YMMV

关于python - Mac 上的 Tkinter 显示为黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73056296/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com