本文实例讲述了Python GUI编程。分享给大家供大家参考,具体如下:
进口操作系统 从进口睡眠时间 从tkinter进口* 从tkinter。弹出窗口导入showinfo 类目录(对象): def __init__(自我,initdir=None): 自我。顶级=Tk () 自我。标签=标签(主=自我。,文本='目录李斯特V1.0”) self.label.pack () 自我。慢性消耗?StringVar(主=self.top) 自我。抖动=标签(自我。前,fg=袄渡弊痔?(Helvetica, 14日,“大胆”)) self.dirl.pack () 自我。dirfm=框架(主=self.top) 自我。dirsb=滚动条(主=self.dirfm) self.dirsb.pack(=正确,填补=Y) #=Y,垂直填充空间排列 自我。dirs=列表框(主=自我。dirfm、身高=15,宽度=50,yscrollcommand=self.dirsb.set) self.dirs.bind (' & lt; Double-1> ', func=self.setDirAndGo) # & lt; Double-1>,双击显示路径列表 self.dirsb.config(命令=self.dirs.yview) self.dirs。包(=左,填补=) self.dirfm.pack () 自我。dirn=条目(主=自我。,宽度=50,textvariable=self.cwd) self.dirn.bind (' & lt; Return> ', func=self.doLS) self.dirn.pack () 自我。bfm=框架(主=self.top) 自我。聪明=按钮(主=自我。bfm、文本='清除”,命令=自我。clrDir activeforeground=住? activebackground=袄丁? 自我。ls=按钮(主=自我。bfm、文本='显示列表”,命令=自我。元ds activeforeground=住? activebackground=奥躺? 自我。辞职=按钮(主=自我。bfm、文本='退出”,命令=self.top。辞职,activeforeground=住? activebackground='红色') self.cleer.pack(左端=) self.ls.pack(左端=) self.quit.pack(左端=) self.bfm.pack () 如果initdir: self.cwd.set (os.curdir) self.doLS () def setDirAndGo(自我,ev=None): 自我。去年=self.cwd.get () self.dirs.config (selectbackground='红色') 赤=self.dirs.get (self.dirs.curselection ()) 如果不是赤: 赤=os.curdir self.cwd.set(赤) self.doLS () def元ds(自我,ev=None): 错误=" tdir=self.cwd.get () 如果不是tdir: tdir=os.curdir 如果不是os.path.exists (tdir): 错误=tdir +”:未找到文件,请检查路径!” elif不是os.path.isdir (tdir): 错误=tdir +”:不是一个路径!” 如果错误: # self.cwd.set(错误) showinfo (title='提示”,消息=错误) self.top.update () #睡眠(2) 如果不是(hasattr(自我,“最后”)和self.last): 自我。去年=os.curdir self.cwd.set (self.last) self.dirs.config (selectbackground=癓ightSkyBlue”) self.top.update () 返回 如果不是os.path.isdir (tdir): self.cwd.set (") 其他: self.cwd.set(“获取目录内容中……) self.top.update () 目录=os.listdir (tdir) dirlist.sort () os.chdir (tdir) self.dirl.config(文本=os.getcwd ()) self.dirs.delete(0,结束) self.dirs。插入(结束,os.curdir) self.dirs。插入(结束,os.pardir) eachfile的目录: self.dirs。插入(结束,eachfile) self.cwd.set (os.curdir) self.dirs.config (selectbackground=癓ightSkyBlue”) def clrDir(自我,ev=None): self.cwd.set (") if __name__==癬_main__”: dir=目录(os.curdir) mainloop () >之前效果如下:
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python套接字编程技巧总结》,《Python函数使用技巧总结》,《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
Python GUI编程完整示例