首页 > 解决方案 > TKinter 属性 Simpledialog 错误

问题描述

我目前正在尝试使用 Python 3.6 中的 tkinter 模块(通过 Spyder)编写一些基本的用户输入框。我可以确认模块已加载,并且出现了选择 simpledialog 的选项,但我不断收到以下错误:

AttributeError:模块“tkinter”没有属性“simpledialog”

tkinter simpledialog 的图像

我试图寻找其他选项,但其他用户输入选项似乎不适用于我的 Python 界面。它要么崩溃,要么数据结构不正确。

有趣的是,在过去,我在 Python 中做过类似的事情,没有出现任何错误,但这一直是这个特定的编程部分。

import tkinter as tk
import pyodbc as py
py.pooling = False

## INPUT YOUR USER ID AND PASSWORD AND DECLARE YOUR CONNECTION
## THE DIALOG BOXES MAY POP OPEN ON ANOTHER SCREEN
## THE PASSWORD INPUT IS MASKED AND WILL NOT SHOW IN THE
## VARIABLE EXPLORER

ID = tk.simpledialog.askstring("Please input your username.","Username: ")
PW = tk.simpledialog.askstring("Please input your password.",
                               "Password: ", show='*')
CONN = tk.simpledialog.askstring("Please input your connection.",
                                 "Connection: ")

我的预期结果是会出现一个弹出窗口,并且我将能够获取我需要的用户信息,以保持与我正在使用的服务器的稳定连接。

提前感谢您的建议!

标签: pythontkinterpython-3.6

解决方案


simpledialog不在tkinter但在tkinter.simpledialog,你必须导入它

import tkinter as tk
import tkinter.simpledialog

root = tk.Tk() # create main window
#root.iconify() # minimize main window 
root.withdraw() # hide main window 

answer = tkinter.simpledialog.askstring("Question", 'Your name:')
print(answer)

#root.destroy()  # should work without it
#root.mainloop() # should work without it

查看tkinter 模块


推荐阅读