最小 tkSimpleDialog 示例中的错误 [英] Error in a minimal tkSimpleDialog example
问题描述
我在 Rosetta Code 尝试了一个 Python 代码示例 - a programming chrestomathy 站点,其中以尽可能多的不同编程语言呈现相同任务的解决方案.对于此任务,目标是从图形用户界面输入一个字符串和整数 75000.代码如下所示:
I was trying out a python code example at Rosetta Code - a programming chrestomathy site, where solutions to the same task are presented in as many different programming languages as possible. For this task, the goal is to input a string and the integer 75000, from graphical user interface. The code is shown below:
import tkSimpleDialog
number = tkSimpleDialog.askinteger("Integer", "Enter a Number")
string = tkSimpleDialog.askstring("String", "Enter a String")
但是,当我尝试运行代码时,出现以下错误:
However, when I try to run the code, I get the following error:
Traceback (most recent call last):
File "C:\Users\vix\Documents\.cache\GUIexample.py", line 3, in <module>
number = tkSimpleDialog.askinteger("Integer", "Enter a Number")
File "C:\Python27\lib\lib-tk\tkSimpleDialog.py", line 262, in askinteger
d = _QueryInteger(title, prompt, **kw)
File "C:\Python27\lib\lib-tk\tkSimpleDialog.py", line 189, in __init__
Dialog.__init__(self, parent, title)
File "C:\Python27\lib\lib-tk\tkSimpleDialog.py", line 53, in __init__
if parent.winfo_viewable():
AttributeError: 'NoneType' object has no attribute 'winfo_viewable'
问题出在哪里?
谢谢
推荐答案
错误消息告诉您对话框需要父窗口.
The error message is telling you that the dialog needs a parent window.
使用 Python 2.x,您可以使用以下命令创建根窗口:
With Python 2.x, you create the root window with:
import tkinter
from tkinter import simpledialog
root = tkinter.Tk()
如果不想隐藏根窗口,请使用:
To hide the root window if you don't want it, use:
root.withdraw()
有关详细信息,请参阅 Python Tkinter 文档.
See the Python Tkinter Docs for more info.
这篇关于最小 tkSimpleDialog 示例中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!