[Tutor] Return Variable from Function.

rsm rsm at imap.cc
Tue May 20 14:33:50 CEST 2014


Hello,

(im using python 2.7)

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,
but the script is not working good. I've been reading tutorials but i 
just  don't get how to do it and i would like to ask for some help >.<
( at the end, it's the part of the script that is not working, you can 
copy it to easily understand the problem)

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.

( i've removed the variable from the script posted below because i tried 
to do it with the global variables )

The problem that i have using the global variables is that the:

     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

is executed when the main_window_gui() window is closed. i don't 
understand why.
It should be executed when the def cancel_label_desition() is executed. 
because thePompt_desition variable changes to 1.

Also i would like to ask if is there any way to put a definition with 
arguments on the command of a button?

when i do this i get errors >.<

     i define message, proceed_label, cancel_label  then:

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


___

Thanks so much for helping me! I appreciate it a lot !!
___





import Tkinter, tkFileDialog, os, csv, shutil, time, getpass, pwd
from os import stat
from Tkinter import *


################## Global Variables ####################

Script_Location=os.path.dirname(os.path.realpath(__file__))
Script_Title="Fresh Install v1.1"

Prompt_Desition=0

#################################################

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

     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()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140520/c037a694/attachment-0001.html>


More information about the Tutor mailing list