[Tutor] Return Variable from Function.

ALAN GAULD alan.gauld at btinternet.com
Tue May 20 17:04:26 CEST 2014



>Thanks so much for your answer Alan! it gave me something to work
    with :), unfortunately it stills not working :/
>
>I've made several comments in the code...
         


>def prompt_desition(message, proceed_label, cancel_label,return_value):
>    "Display a window with the selected message with two buttons
      in way to take a desition. If cancel_label: return_var=0 , if
      proceed_label: return_var=1."
>  
>    def cancel_label_desition(return_value):
>        prompt_window.destroy()
>        return_value=0
>        
>        print "no"
>        
>        return return_value
>              
>    def proceed_label_desition(return_value):
>        prompt_window.destroy()
>        return_value=1
>        
>        print "yes"
>        
>        return return_value
>
>These two functions should probably be left outside your prompt_desition function. 
There is no real need to define these every time this function is called. They don't change.
Also note that although they both define return_value and indeed return it that does 
not make the variable known to the outer function, it is still only local to the two functions.

    prompt_window=Tk()
>
>You already have a top level window (Tk) so you should either use it or instead create 
a dialog window using Toplevel() instead of Tk()

    button_proceed_label = Button(prompt_window, text=proceed_label,command=lambda :proceed_label_desition(return_value))
>
>The lambda calls the function with a return_value argument but return_value 
is not known at this point. It is not a defined name within this functions scope.

    button_cancel_label = Button(prompt_window, text=cancel_label,command=lambda :cancel_label_desition(return_value))
>
>
>Same here

    prompt_window.mainloop()
>
>
>
>You really only want a single mainloop() running in your GUI, and that's 
>the top level one defined in main()


>def delete_program_data():
>    "Delete all the data stored by this program"
>    message="Do you want to delete all the data stored by this
      program?"
>    prompt_desition(message, "Proceed", "Cancel", "choice")
>
>You shouldn't really mix event handler/command functions with functions you call directly. 
It can get awfully confusing. Sometimes it works but often it gets messy. Better to define 
the general function and then have an event handler call that indirectly.
(Or use a lambda such as you are almost doing...)

But the real issue here is that you are not storing the return value from the 
prompt_desition() function anywhere, so what is your choice (in the next line) 
based on?



     if choice==1:
>      
>        if os.path.exists(Script_Location+"/program-data"):
>            os.system("rm -r "+Script_Location+"/program-data")
>            print "Program-data deleted"
>        else:
>            print "No program-data"
>  
>        if os.path.exists(Script_Location+"/computer-data"):
>            os.system("rm -r "+Script_Location+"/computer-data")
>            print "Computer-Data Deleted"
>        else:
>            print "No computer-data"
>        
>
>  I think you need to rethink the structure of your GUI.
Lookingt at the code below it defines a window containing nothing 
but a single button. That button then calls another function which 
then builds another window. Can you think of any popular GUI 
application that works like that? I can't.

Try to envision what the GUI should look and feel like to the user, 
Then build your code around that. Think about the data displayed 
and the  actions that your user will do to perform work.

Then link those actions to the functions that perform it.
This design feels like you are trying to build a text oriented 
CLI using a GUI rather than design an event driven true GUI program.

def main_window_gui():
>
>    window1=Tk()
>    window1.wm_title()
>    window1.resizable(0,0) # dont resize
>    
>    b100 = Button(window1, text="Delete",
      command=delete_program_data)
>    b100.pack()
>
>    window1.mainloop()
>
>
>main_window_gui() 
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140520/130d4d6b/attachment-0001.html>


More information about the Tutor mailing list