[Tutor] Return Variable from Function.

Alan Gauld alan.gauld at btinternet.com
Tue May 20 15:15:55 CEST 2014


On 20/05/14 13:33, rsm wrote:

> I've been having some problems to return the variable from a function so
> i decided to try to do it with with global variables,

That's usually a bad idea.

However, what you have not shown us is what you did
try that didn't work. (Unless thats what the snippet below is?)

In principle its easy you just type

return variable

and the function terminates at that point and returns the variable.

> what i would like create a function that creates a window for taking a
> choice and returns a variable in order to proceed using the choice of
> that variable.

However, that is usually a standard feature of any GUI toolkit so you 
should probably look a little closer at your widget set and seee if 
three are any standard dialogs available. You don;t say which GUI 
toolkit you are using but if it were Tkinter (and it looks like
it may be)  you'd do something like (for python3)

import tkinter.messagebox as mbox

or (for Python2)

import tkmessagebox

result = mbox.askyesno("Your title here", "Your yes/no question here")

if result:
    print ("You said yes!")
else: print("Sorry, no go")

With several other options available...

You can see more in my GUI topic of my
tutor...(look in Version 2)

Most GUIs will have similar facilities.

>      if Prompt_Desition==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"

You probably shouldn't mix print statements and GUI
input it tends to be look confusing. GUI output from text input is often 
OK but if the user is interacting with a GUI they tend not
to expect output in a terminal.


>
>          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"
>
>          Prompt_Desition=0
>
> is executed when the main_window_gui() window is closed. i don't
> understand why.

See below

>    b100 = Button(window1, text="Delete",command=prompt_desition(message,
> proceed_label, cancel_label))
>      b100.pack()

Here you are calling the function prompt_desition() rather than 
referencing it. Also the button command function must not take
any a.rguments They way rtound that is to do this:

 >    b100 = Button(window1, text="Delete",
                    command=lambda m=message,
                                   pl=proceed_label,
                                   cl=cancel_label :
                                             prompt_desition(m,pl,cl))


> def prompt_desition(message, proceed_label, cancel_label):
>      "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."

those should probably be triple quotes...

>      def cancel_label_desition():
>          prompt_window.destroy()
>
>      def proceed_label_desition():
>          global Prompt_Desition
>          Prompt_Desition=1
>          prompt_window.destroy()
>
>      prompt_window=Tk()
>      prompt_window.configure()
>      prompt_window.wm_title()
>      prompt_window.resizable(0,0)
>
>      lb1 = Label(prompt_window, text=message)
>      lb1.pack()
>
>      button_proceed_label = Button(prompt_window,
> text=proceed_label,command=proceed_label_desition)
>      button_proceed_label.pack()
>
>      button_cancel_label = Button(prompt_window,
> text=cancel_label,command=cancel_label_desition)
>      button_cancel_label.pack()
>
>      prompt_window.mainloop()
>
>
> def delete_program_data():
>      "Delete all the data stored by this program"
>
>      global Prompt_Desition
>
>      message="Do you want to delete all the data stored by this program?"
>      prompt_desition(message, "Proceed", "Cancel")
>
>      if Prompt_Desition==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"
>
>          Prompt_Desition=0
>
>
> def main_window_gui():
>
>      window1=Tk()
>      window1.wm_title(Script_Title)
>      window1.resizable(0,0) # dont resize
>
>
>      b100 = Button(window1, text="Delete",command=delete_program_data)
>      b100.pack()
>
>      window1.mainloop()
>
>
> main_window_gui()
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list