views:

534

answers:

3

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

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?

Alex Martelli
The window is not opening. Instead of starting the program returns <code>'NoneType' object has no attribute 'winfo_viewable'</code>. Should it be set up as an 'if' 'else' type situation as with some other tkSimple stuff? Could I see an example of tkSimpleDialog setting a string variable with the response it receives and then allowing the program to continue (the dialog ends)?
Donnied
Oops. I think I got it working. Thank you for the input. Now, I'll need to clear up the blank tkinter widget that appears and enable copy and paste.
Donnied
A: 

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

Donnied
A: 

The point is that for all the other tkSimpleDialog stuff (askinteger, etc) you don't need to define root. So something is broken somewhere in this code.

Tscott4