如何在不同 python tkinter 按钮事件操作之间使用变量?

How to use variables between different python tkinter button event actions?

我正在编写一个 python 程序来获取输入文件和输出目录的用户输入,然后使用 subprocess 模块获取输入文件的 dd 图像。我写了下面的代码。我无法在其他一些 def 部分中使用我在一个 def 中使用的变量。请帮我解决这个问题,因为我是 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()

请给我一些建议,以便我可以使用 open_dir() 和 get_input() 中的 'var' 和 'o_var' 变量,并在 dev_img().

我想我知道你想问什么。您如何 'inherit' 将一个函数的输出变量传递给另一个函数?为此,您需要使用 classes。 Python 完全是 object-oriented,因此它的工作方式允许对象相互交互。

看看这个 link:http://www.diveintopython.net/object_oriented_framework/defining_classes.html

本教程将允许您创建一个 class,它允许您在您定义的任何函数中使用您的 var 和 var_o。

希望这对您有所帮助。