-1

I'm writing a python program to take user input for input file and output directory and then take a dd image of the input file using the subprocess module. I have written the following code. I am not able to use the variables i use in one def, in some other def part. Please help me out with this as i'm new to Python.

def open_dir():
    o_file = subprocess.call(["zenity","--file-selection","--directory","--title=Select Destination Directory"])
    o_var = StringVar() 
    o_var.set(o_file)   

def quit_root():
    root.destroy()


def get_input():
    msg1 = subprocess.call(["df","-h"])
    msg2 = StringVar()
    msg2.set(msg1)
    msg = Message(labelframe, textvariable=msg1)
    msg.pack()  
    in_file=subprocess.check_output(["zenity","--entry"])
    var=StringVar()
    var.set(in_file)


def dev_img():
    global var
    global o_var
    input_file=var
    output_file=o_var+"device.img"
    out = subprocess.check_output(["dd","if="+input_file,"|pv|","of="+output_file,"bs=1024"], stderr=subprocess.STDOUT)
    var1 = StringVar()
    var1.set(out)
    label1 = Message(labelframe, textvariable=var1)
    label1.pack()

Please suggest me some way so that i can use the 'var' and 'o_var' variables from open_dir() and get_input() and use those varibales in dev_img().

1 Answers1

0

I think I know what you are trying to ask. How do you 'inherit' the output variables from one function to a different function? For this, you need to use classes. Python is fully object-oriented so it works in a way that allows objects to interact with each other.

Check out this link: http://www.diveintopython.net/object_oriented_framework/defining_classes.html

This tutorial will allow you to create a class which will allow you to use your var and var_o in any functions you define.

Hope this helps.

Joey
  • 914
  • 4
  • 16
  • 37