2016-10-25 4 views
0

내 회사의 사용자가 일부 개인 설정으로 이메일의 기본 서명을 변경할 수 있도록하기 위해 노력하고 있습니다.askopenfilename tkinter 함수 및 전역

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) 

그것은 나 파일을 선택할 수 있으며이 변수에 저장하는 것 같다

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

그것은이 호출 :이 버튼을 클릭하면 왜

그래서 이해가 안 돼요 내가 여기에 변수를 사용할 때, 나중에 :

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

을 제공합니다
NameError: name 'image_path' is not defined 

내가 뭘 잘못하고 있니?

답변

2

전역 범위에서 변수를 만든 다음 함수 내에 global을 사용해야합니다.

image_path = "" 

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

정말 고맙습니다! – andrepogg