如何使用 Python tkSimpleDialog.askstring

发布于 2024-08-16 23:58:35 字数 299 浏览 5 评论 0原文

我想使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小女人ら 2024-08-23 23:58:35

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 call urlopen anyway...).

Apart from that, you're using the function correctly; I imagine that by "has no value" you mean is None, right?

我三岁 2024-08-23 23:58:35

root = Tk()   


try:
        urltoopen = tkSimpleDialog.askstring('Ask Address', 'Where do we get the pictures from?')
        usock = urllib2.urlopen(urltoopen)                                                       
        data = usock.read()                                                                      
        usock.close()                                                                            
        a = data                                                                                 
except:                                                                                          
        sys.exit()    

工作正常。但它确实需要错误处理(正如亚历克斯提到的)。


root = Tk()   


try:
        urltoopen = tkSimpleDialog.askstring('Ask Address', 'Where do we get the pictures from?')
        usock = urllib2.urlopen(urltoopen)                                                       
        data = usock.read()                                                                      
        usock.close()                                                                            
        a = data                                                                                 
except:                                                                                          
        sys.exit()    

works fine. But it does need error handling (as mentioned by Alex).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文