tkinter實現打開文件對話框並獲取文件絕對路徑

# 首先,導入模塊
import tkinter.filedialog


# 此處省略父容器的定義 ...

# 第2步,定義按鈕並指定觸發函數self.Command1_Cmd
self.style.configure('Command1.TButton',font=('宋體',9))
self.Command1 = Button(self.Frame1, text='打開文件', command=self.Command1_Cmd, style='Command1.TButton')
self.Command1.place(relx=0.836, rely=0.107, relwidth=0.093, relheight=0.111)

# 第3步,定義按鈕觸發函數,event=None不能省略
def Command1_Cmd(self, event=None):
      # tkinter提供的askopenfilename函數可以實現打開文件對話框的效果,其返回值爲所選文件的絕對路徑
      filename = tkinter.filedialog.askopenfilename()
      print(filename)

 

 

邊學習邊長大,每天向前咕蛹咕蛹

發佈了16 篇原創文章 · 獲贊 7 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章