2017-05-06 10 views
0

파일을 선택하고 레이블의 이름을 표시하려고합니다.Python 3 - Tcl/Tk 파일 이름을 filedialog에서 얻고 레이블을 변경하는 방법

def onOpen(): 
    photo_label = filedialog.askopenfilename() 
    pass 


#photo code 
photo = PhotoImage(file="smile.png") 
photo_label = Button(image=photo, command=onOpen).grid() 
#I am attempting to change text=photo_label to reflect the file name 
text = Label(text=photo_label) # included to show background color 
text.grid() 

답변

1

당신은 StringVar를 사용할 때마다 그 변수의 값이 변경되도록, 라벨의 textvariable 옵션에 전달할 수 있으며, 라벨의 텍스트가 너무 :

import tkinter as tk 
from tkinter import filedialog 

def onOpen(): 
    """ Ask the user to choose a file and change the update the value of photo_label""" 
    photo_label.set(filedialog.askopenfilename()) 

root = tk.Tk() 
# StringVar that will contain the file name 
photo_label = tk.StringVar(root) 

photo = tk.PhotoImage(file="smile.png") 
tk.Button(root, image=photo, command=onOpen).grid() 

text = tk.Label(root, textvariable=photo_label) 
text.grid() 

root.mainloop() 

참고 : grid()None을 반환하므로
photo_label = Button(image=photo, command=onOpen).grid()photo_label 값이 할당되었습니다.