Python Askopen函数和全局函数

Python Askopen函数和全局函数,python,python-3.x,variables,Python,Python 3.x,Variables,我正在尝试做一些事情,让我的公司的用户改变他们的默认签名的电子邮件与一些个性化 所以我不明白为什么我点击这个按钮: Button(master, text='Insert Image',command=insert_image).grid(row=12,column=1,sticky=W,pady=4) 它称之为: global image_path def insert_image(): image_path = filedialog.askopenfilename(initi

我正在尝试做一些事情,让我的公司的用户改变他们的默认签名的电子邮件与一些个性化

所以我不明白为什么我点击这个按钮:

 Button(master, text='Insert  Image',command=insert_image).grid(row=12,column=1,sticky=W,pady=4)
它称之为:

global image_path

def insert_image():
    image_path = filedialog.askopenfilename(initialdir="X:\\", title="Select the image you want to add")
    image_path = 'X:\\' + str(image_path)
它让我选择文件,并似乎将其存储在变量中,但稍后,当我必须在此处使用变量时:

    if image_path != "":
        signature.write('<br><br><img src="{}" alt="prova"><br><br>\n'.format(image_path))

我做错了什么?

您需要在全局范围内创建变量,然后在函数内部使用

image_path = ""

def insert_image():
    global image_path
    image_path = filedialog.askopenfilename(...)
    ...

您需要在全局范围内创建变量,然后在函数内部使用

image_path = ""

def insert_image():
    global image_path
    image_path = filedialog.askopenfilename(...)
    ...