You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS Sonoma更新后Tkinter无法运行及安全编码警告问题求助

macOS Sonoma下Tkinter无法运行及安全编码警告的解决方法

临时绕过警告(快速启动程序)

要是只想先让程序跑起来,不用纠结警告,直接在代码开头加这段,禁用Tkinter的状态恢复功能:

import tkinter as tk
# 关闭状态恢复以绕过安全编码警告
root = tk.Tk()
root.withdraw()
root.option_add('*tearOff', False)
# 后续写你的程序逻辑
root.mainloop()

彻底消除安全编码警告(针对打包应用)

如果你的程序打包成了.app格式(比如用PyInstaller),修改Info.plist文件添加安全编码支持:

  • 找到.app文件,右键选「显示包内容」
  • 打开Contents/Info.plist,插入以下键值对:
<key>NSApplicationSupportsSecureRestorableState</key>
<true/>
  • 保存后重新启动应用

更换Python环境解决兼容性问题

系统自带的Python版本在Sonoma下和Tkinter适配性不佳,建议换用官方维护的Python版本:

  • 下载最新稳定版Python(3.10及以上),安装后自带适配Sonoma的Tkinter
  • 或者用conda创建独立环境:
conda create -n tk_env python=3.11
conda activate tk_env
pip install tk

测试Tkinter可用性

运行以下代码验证Tkinter是否正常工作:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("Sonoma Tkinter测试")
label = ttk.Label(root, text="Tkinter运行正常!")
label.pack(padx=20, pady=20)
root.mainloop()

内容的提问来源于stack exchange,提问作者Srushti Hirve

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 15:14:58