想了比较久用python做一个GUI的demo,还是用tkinter做一个小demo,版本python3.7下可行,代码如下:
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 from functools import partial
5 import os
6 from time import sleep
7 from tkinter import *
8
9 class Application:
10
11 def __init__(self,initdir = None):
12 self.initdir = initdir
13
14 def ui_process(self):
15 root = Tk()
16 # 设置窗口位置
17 root.title("gui demo5 ")
18 self.root = root
19 #self.center_window(root, 600, 565)
20 #root.resizable(0, 0) # 框体大小可调性,分别表示x,y方向的可变性
21 self.label = Label(self.root,text='Directory Lister v1.1')
22 self.label.pack()
23
24 self.cwd = StringVar(self.root)
25 #print('==='+str(self.cwd))
26
27 self.dirl = Label(self.root,fg='blue',font=('Helvetica',12,'bold'))
28 self.dirl.pack()
29
30 self.dirfm = Frame(self.root)
31 self.dirsb = Scrollbar(self.dirfm)
32 self.dirsb.pack(side=RIGHT,fill=Y)
33 self.dirs = Listbox(self.dirfm,height=15,width=50,yscrollcommand=self.dirsb.set)
34 self.dirs.bind('<Double-1>',self.setDirAndGo)
35 self.dirsb.config(command=self.dirs.yview)
36 self.dirs.pack(side=LEFT,fill=BOTH)
37 self.dirfm.pack()
38
39 self.dirn = Entry(self.root,width=50,textvariable=self.cwd)
40 self.dirn.bind('<Return>',self.doLS)
41 self.dirn.pack()
42
43 self.bfm = Frame(self.root)
44 self.clr = Button(self.bfm,text='Clear',command=self.clrDir,activeforeground='white',activebackground='blue')
45 self.ls = Button(self.bfm,text='List Directory',command=self.doLS,activeforeground='white',activebackground='green')
46 self.quit = Button(self.bfm,text='QUIT',command=self.root.quit,activeforeground='white',activebackground='red')
47 self.clr.pack(side=LEFT)
48 self.ls.pack(side=LEFT)
49 self.quit.pack(side=LEFT)
50 self.bfm.pack()
51
52 if self.initdir:
53 self.cwd.set(os.curdir)
54 self.doLS()
55
56 root.mainloop()
57
58 def center_window(self, root, w, h):
59 """
60 窗口居于屏幕中央
61 :param root: root
62 :param w: 窗口宽度
63 :param h: 窗口高度
64 :return:
65 """
66 # 获取屏幕 宽、高
67 ws = root.winfo_screenwidth()
68 hs = root.winfo_screenheight()
69
70 # 计算 x, y 位置
71 x = (ws/2) - (w/2)
72 y = (hs/2) - (h/2)
73 root.geometry('%dx%d+%d+%d' % (w, h, x, y))
74
75 def doLS(self,ev=None):
76 error =''
77 tdir = self.cwd.get()
78 if not tdir:
79 tdir = os.curdir
80
81 if not os.path.exists(tdir):
82 error = tdir + ':no such file.'
83 elif not os.path.isdir(tdir):
84 error = tdir + ':no a directory.'
85
86 if error:
87 self.cwd.set(error)
88 self.root.update()
89 sleep(2)
90 if not (hasattr(self,'last') \
91 and self.last ):
92 self.last = os.curdir
93 self.cwd.set(self.last)
94 self.dirs.config(selectbackground='LightSkyBlue')
95 self.root.update()
96 return
97
98 self.cwd.set('fetching directory contents...')
99 self.root.update()
100 dirlist = os.listdir(tdir)
101 dirlist.sort()
102 os.chdir(tdir)
103
104 self.dirl.config(text=os.getcwd())
105 self.dirs.delete(0,END)
106 self.dirs.insert(END,os.curdir)
107 self.dirs.insert(END,os.pardir)
108 for eachFile in dirlist:
109 self.dirs.insert(END,eachFile)
110 self.cwd.set(os.getcwd())
111 self.dirs.config(selectbackground='LightSkyBlue')
112
113 def setDirAndGo(self,ev=None):
114 print(str(self.cwd))
115 self.last = self.cwd.get()
116 self.dirs.config(selectbackground='red')
117 check = self.dirs.get(self.dirs.curselection())
118 if not check:
119 check = os.curdir
120 self.cwd.set(check)
121 self.doLS()
122
123 def clrDir(self,ev=None):
124 self.cwd.set('')
125
126
127 def main():
128 app = Application()
129 app.ui_process()
130
131
132 if __name__ == "__main__":
133 main()
通过demo学习了entry,button,Listbox等控件的使用,学习了StringVar的使用。
来源:https://www.cnblogs.com/hipphappy/p/10805437.html