标签:
1 #!/usr/bin/env python 2 3 import Tkinter 4 5 top = Tkinter.Tk() 6 label = Tkinter.Label(top, text=‘Hello World!‘) 7 label.pack() 8 Tkinter.mainloop()
1 #!/usr/bin/env python 2 3 import Tkinter 4 5 top = Tkinter.Tk() 6 quit = Tkinter.Button(top, text=‘Hello World!‘,7 command=top.quit) 8 quit.pack() 9 Tkinter.mainloop()
1 #!/usr/bin/env python 2 3 import Tkinter 4 top = Tkinter.Tk() 5 6 hello = Tkinter.Label(top, text=‘Hello World!‘) 7 hello.pack() 8 9 quit = Tkinter.Button(top, text=‘QUIT‘, 10 command=top.quit, bg=‘red‘, fg=‘white‘) 11 quit.pack(fill=Tkinter.X, expand=1) 12 13 Tkinter.mainloop()
标签、按钮和进度条组件
1 #!/usr/bin/env python 2 3 from Tkinter import * 4 5 def resize(ev=None): 6 label.config(font=‘Helvetica -%d bold‘ % 7 scale.get()) 8 9 top = Tk() 10 top.geometry(‘250x150‘) 11 12 label = Label(top, text=‘Hello World!‘, 13 font=‘Helvetica -12 bold‘) 14 label.pack(fill=Y, expand=1) 15 16 scale = Scale(top, from_=10, to=40, 17 orient=HORIZONTAL, command=resize) 18 scale.set(12) 19 scale.pack(fill=X, expand=1) 20 21 quit = Button(top, text=‘QUIT‘, 22 command=top.quit, activeforeground=‘white‘, 23 activebackground=‘red‘) 24 quit.pack() 25 26 mainloop()
1 #!/usr/bin/env python 2 3 import os 4 from time import sleep 5 from Tkinter import * 6 7 class DirList(object): 8 9 def __init__(self, initdir=None): 10 self.top = Tk() 11 self.label = Label(self.top, 12 text=‘Directory Lister v1.1‘) 13 self.label.pack() 14 15 self.cwd = StringVar(self.top) 16 17 self.dirl = Label(self.top, fg=‘blue‘, 18 font=(‘Helvetica‘, 12, ‘bold‘)) 19 self.dirl.pack() 20 21 self.dirfm = Frame(self.top) 22 self.dirsb = Scrollbar(self.dirfm) 23 self.dirsb.pack(side=RIGHT, fill=Y) 24 self.dirs = Listbox(self.dirfm, height=15, 25 width=50, yscrollcommand=self.dirsb.set) 26 self.dirs.bind(‘<Double-1>‘, self.setDirAndGo) 27 self.dirsb.config(command=self.dirs.yview) 28 self.dirs.pack(side=LEFT, fill=BOTH) 29 self.dirfm.pack() 30 31 self.dirn = Entry(self.top, width=50, 32 textvariable=self.cwd) 33 self.dirn.bind(‘<Return>‘, self.doLS) 34 self.dirn.pack() 35 36 self.bfm = Frame(self.top) 37 self.clr = Button(self.bfm, text=‘Clear‘, 38 command=self.clrDir, 39 activeforeground=‘white‘, 40 activebackground=‘blue‘) 41 self.ls = Button(self.bfm, 42 text=‘List Directory‘, 43 command=self.doLS, 44 activeforeground=‘white‘, 45 activebackground=‘green‘) 46 self.quit = Button(self.bfm, text=‘Quit‘, 47 command=self.top.quit, 48 activeforeground=‘white‘, 49 activebackground=‘red‘) 50 self.clr.pack(side=LEFT) 51 self.ls.pack(side=LEFT) 52 self.quit.pack(side=LEFT) 53 self.bfm.pack() 54 55 if initdir: 56 self.cwd.set(os.curdir) 57 self.doLS() 58 59 def clrDir(self, ev=None): 60 self.cwd.set(‘‘) 61 62 def setDirAndGo(self, ev=None): 63 self.last = self.cwd.get() 64 self.dirs.config(selectbackground=‘red‘) 65 check = self.dirs.get(self.dirs.curselection()) 66 if not check: 67 check = os.curdir 68 self.cwd.set(check) 69 self.doLS() 70 71 def doLS(self, ev=None): 72 error = ‘‘ 73 tdir = self.cwd.get() 74 if not tdir: tdir = os.curdir 75 76 if not os.path.exists(tdir): 77 error = tdir + ‘:no such file‘ 78 elif not os.path.isdir(tdir): 79 error = tdir + ‘: not a directory‘ 80 81 if error: 82 self.cwd.set(error) 83 self.top.update() 84 #sleep(2) # sleep ?? 85 if not (hasattr(self, ‘last‘) 86 and self.last): 87 self.last = os.curdir 88 self.cwd.set(self.last) 89 self.dirs.config( 90 selectbackground=‘LightSkyBlue‘) 91 self.top.update() 92 return 93 94 self.cwd.set( 95 ‘FETCHING DIRECTORY CONTENTS...‘) 96 self.top.update() 97 dirlist = os.listdir(tdir) 98 dirlist.sort() 99 os.chdir(tdir) 100 self.dirl.config(text=os.getcwd()) 101 self.dirs.delete(0, END) 102 self.dirs.insert(END, os.curdir) 103 self.dirs.insert(END, os.pardir) 104 for eachFile in dirlist: 105 self.dirs.insert(END, eachFile) 106 print eachFile 107 self.cwd.set(os.curdir) 108 self.dirs.config(109 selectbackground=‘LightSkyBlue‘) 110 111 def main(): 112 d = DirList(os.curdir) 113 mainloop() 114 115 if __name__ == ‘__main__‘: 116 main()
标签:
原文地址:http://www.cnblogs.com/BugQiang/p/4743109.html