How to use Python tkSimpleDialog.askstring

I want to use the answer from the askstring prompt to set a variable. Unfortunately, I have a dilemma: I got into a circle asking a question, or the window refuses to draw, because the variable (urltoopen) does not matter. Code in its current form:

urltoopen = tkSimpleDialog.askstring('Address', 'Where do we get the pictures from?')
usock = urllib2.urlopen(urltoopen)
data = usock.read()    
usock.close()                     
+3
source share
2 answers

tkSimpleDialog.askstring returns Noneif the user clicks the Cancel button or closes the window (instead of pressing OK or using the Enter key); you have to check it (what do you want to do if the user wants to cancel? Of course, do not call urlopenanyway ...).

, ; , " " is None, ?

+3

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

. ( ).

0

Source: https://habr.com/ru/post/1727309/


All Articles