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

Tkinter的simpledialog在Spyder中正常运行但在Shell中触发AttributeError的问题求助

Tkinter的simpledialog在Spyder中正常运行但在Shell中触发AttributeError的问题求助

我写了一个简单的Python脚本用来弹出一个对话框,在Spyder里运行完全正常,但在Shell里执行相同命令的时候却出问题了。

我用的是Linux Mint系统,Spyder是通过Anaconda的Miniconda安装的。

我的代码如下:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tkinter as tk
tkwin = tk.Tk()
tkwin.withdraw()
mytext = tk.simpledialog.askstring("A Simple Dialog Box", "Enter some text:", parent=tkwin)

在Spyder里能正常弹出对话框,但在Shell里我进入Python交互环境输入同样的命令时,却收到了这样的错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'tkinter' has no attribute 'simpledialog'

我确认过Spyder和Shell里用的是同一个Python版本。
有意思的是,在Spyder里执行print(dir(tk))能看到simpledialog,但在Shell里执行同样的命令却看不到。我在Spyder控制台里输入tk,得到的结果是:

<module 'tkinter' from '/home/keith/Apps/miniconda3/lib/python3.12/tkinter/__init__.py'>

在Shell里输入tk得到的结果也是一样的。这到底是怎么回事?该怎么解决呢?

谢谢大家!
Keith

备注:内容来源于stack exchange,提问作者keithIHS

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 16:24:31