开发者

How to use Python tkSimpleDialog.askstring

开发者 https://www.devze.com 2022-12-15 16:25 出处:网络
I want to use the response from an ask开发者_运维问答string prompt to set a variable. Unfortunately,

I want to use the response from an ask开发者_运维问答string 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()                     


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?



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).

0

精彩评论

暂无评论...
验证码 换一张
取 消