如何使用 Python tkSimpleDialog.askstring
我想使用askstring提示的响应来设置变量。很遗憾, 我陷入了困境,我陷入了询问问题的循环中,或者窗口拒绝绘制,因为变量 (urltoopen) 没有值。 代码如下:
urltoopen = tkSimpleDialog.askstring('Address', 'Where do we get the pictures from?')
usock = urllib2.urlopen(urltoopen)
data = usock.read()
usock.close()
I want to use the response from an askstring prompt to set a variable. Unfortunately,
I have the dilemma that I'm trapped in the loop asking the question or the window refuses to draw because the variable (urltoopen) has no value.
The code as it stands:
urltoopen = tkSimpleDialog.askstring('Address', 'Where do we get the pictures from?')
usock = urllib2.urlopen(urltoopen)
data = usock.read()
usock.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
tkSimpleDialog.askstring 返回
None 如果用户单击“取消”或关闭窗口(而不是单击“确定”或使用 Enter 键);您应该检查这一点(如果用户选择取消,您想做什么?无论如何,肯定不会调用 urlopen...)。
除此之外,您正确使用了该功能;我想“没有价值”你的意思是
没有
,对吗?tkSimpleDialog.askstring returns
None
if the user clicks Cancel or closes the window (instead of clicking Ok or using the Enter key); you should check for that (what do you want to do if the user chooses to cancel? surely not callurlopen
anyway...).Apart from that, you're using the function correctly; I imagine that by "has no value" you mean
is None
, right?工作正常。但它确实需要错误处理(正如亚历克斯提到的)。
works fine. But it does need error handling (as mentioned by Alex).