# 首先,导入模块
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)

 

欢迎访问我的个人博客:机器学习之路

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐