python3 /home/
第一关
from tkinter import *
frame = Tk()
frame.title('我的界面')
frame.geometry('400x200') # x是英文字母,表示乘号
frame.mainloop()
第二关
from tkinter import *
def onBtn(): # 定义响应函数
frame2=Tk()
label=Label(frame2,text='Hello World!')
label.place(x=60,y=50)
frame2.mainloop()
frame = Tk()
frame.title('我的界面')
frame.geometry('400x200')
btn=Button(frame,
text='一个按钮',
font=('Kaiti',14),
command=onBtn) # 指定响应函数
btn.place(x=150,y=50)
frame.mainloop()
第三关
from tkinter import *
def onBtn(txt):
frame2=Tk()
frame2.geometry('200x50')
label=Label(frame2,text=txt)
label.place(x=1,y=1)
frame2.mainloop()
frame=Tk()
frame.geometry('200x50')
text=Entry(frame,width=20)
text.place(x=5,y=5)
btn=Button(frame,text='按钮',
command=lambda:onBtn(txt=text.get()))
btn.place(x=150,y=1)
frame.mainloop()
第四关
from tkinter import *
from tkinter import ttk
frame=Tk()
frame.title('我的界面')
frame.geometry('323x200')
# 创建表格对象table
table=ttk.Treeview(frame,show='headings',height=3)
table.place(x=1,y=1)
#给每列去个名字
table['columns']=['id','name','age']
#指定每列的宽度和对齐方式
table.column('id',width=150,anchor='w')
table.column('name',width=100,anchor='center')
table.column('age',width=50,anchor='e')
#指定每列的标题
table.heading('id',text='学号',anchor='w')
table.heading('name',text='姓名',anchor='center')
table.heading('age',text='年龄',anchor='e')
#往表格中添加4个学生的记录
table.insert('',0,values=('200506021041','张三',16))
table.insert('',0,values=('200506021042','李四',18))
table.insert('',0,values=('200506021043','王五',17))
table.insert('',0,values=('200506021044','赵六',18))
#定义垂直滚动条
sbar=ttk.Scrollbar(frame,
orient="vertical",
command=table.yview)
sbar.place(x=300,y=1,height=100)
table.configure(yscrollcommand=sbar.set)
frame.mainloop()
第五关
from tkinter import *
from tkinter import ttk
import psutil, os
def BtoKMG(n):
if n >= 1024 ** 3:
return str(round(n / 1024 ** 3, 1)) + ' GB'
elif n >= 1024 ** 2:
return str(round(n / 1024 ** 2, 1)) + ' MB'
else:
return str(round(n / 1024 ** 1, 1)) + ' KB'
def getMemInfo():
m = psutil.virtual_memory()
info = (BtoKMG(m.total), BtoKMG(m.used), BtoKMG(m.free))
return info
def getDiskInfo():
info = []
for part in psutil.disk_partitions():
if not 'cdrom' in part.opts:
usage = psutil.disk_usage(part.mountpoint)
info.append((part.device,
part.fstype,
BtoKMG(usage.total),
BtoKMG(usage.used),
BtoKMG(usage.free),
str(usage.percent) + '%'))
return info
# 存储信息界面frame1:包括3个表格table11~table13、3个标签label11~label13
def onBtn01():
frame1 = Tk()
frame1.title('查看存储信息')
frame1.geometry('660x280')
#显示主存信息
memInfo = getMemInfo()
label11 = Label(frame1, text='主存信息')
label11.place(x=296, y=10)
table11 = ttk.Treeview(frame1, show='headings', height=1)
table11['columns'] = ['total', 'used', 'free']
table11.column('total', width=100, anchor='center')
table11.column('used', width=100, anchor='center')
table11.column('free', width=100, anchor='center')
table11.heading('total', text='总容量')
table11.heading('used', text='已用容量')
table11.heading('free', text='剩余容量')
table11.insert('', 0, values=memInfo)
table11.place(x=175, y=38)
diskInfo = getDiskInfo()
label13 = Label(frame1, text='分区信息')
label13.place(x=296, y=90)
table13 = ttk.Treeview(frame1, show='headings', height=5)
table13['columns'] = ['dev', 'type', 'total',
'used', 'free', 'rate']
table13.column('dev', width=102, anchor='center')
table13.column('type', width=102, anchor='center')
table13.column('total', width=104, anchor='e')
table13.column('used', width=104, anchor='e')
table13.column('free', width=104, anchor='e')
table13.column('rate', width=104, anchor='e')
table13.heading('dev', text='分区')
table13.heading('type', text='类型')
table13.heading('total', text='总容量', anchor='e')
table13.heading('used', text='已用容量', anchor='e')
table13.heading('free', text='剩余容量', anchor='e')
table13.heading('rate', text='已用比例', anchor='e')
for i in range(len(diskInfo)):
table13.insert('', i, values=diskInfo[i])
table13.place(x=20, y=118)
frame1.mainloop()
def start():
frame0 = Tk()
frame0.title('我的资源管理器')
frame0.geometry('300x100')
btn01 = Button(frame0, text='查看存储信息',
font=('Kaiti', 14), command=onBtn01)
btn01.place(x=80, y=30)
frame0.mainloop()
start()
第六关
from tkinter import *
from tkinter import ttk
import os
#判断filepath对应的txt文件中是否包含keyword
def isContain(filepath, keyword):
flag = False #flag用于标识是否包含,最开始为False,即不包含
txt = open(filepath)
while True:
line = txt.readline()
if line == '':
break
if keyword.lower() in line.lower(): #若包含
flag = True #flag变为True
break #已经确定包含,不用再处理后面的内容,跳出循环
txt.close()
return flag
#用于查找dirpath下面所有包含keyword的txt文件
def find(dirpath, keyword):
files = [] #用于存储满足条件的txt文件路径
L = os.listdir(dirpath)
for filename in L:
path = os.path.join(dirpath, filename)
if os.path.isfile(path): #如果是文件
try:
if isContain(path, keyword): #如果包含keyword
files.append(path) #把这个文件的路径追加到files
except:
pass
else: #若为文件夹
files = files+find(path, keyword) #用find函数处理该文件夹,把查找的结果(即这个子文件夹下面所有满足条件的txt)放到files中
return files
#显示结果界面
def onBtn01(path, keyword):
files = find(path, keyword)
files.sort()
frame1 = Tk()
frame1.title('查找结果')
frame1.geometry('600x430')
table11 = ttk.Treeview(frame1, show='headings', height=20)
table11['columns'] = ['id', 'name', 'path']
table11.column('id', width=50, anchor='center')
table11.column('name', width=150, anchor='w')
table11.column('path', width=378, anchor='w')
table11.heading('id', text='编号', anchor='center')
table11.heading('name', text='文件名', anchor='center')
table11.heading('path', text='文件路径', anchor='center')
for i in range(len(files)):
path = files[i]
name = os.path.split(path)[1]
table11.insert('', i, values=(i+1, name, path))
table11.place(x=1, y=1)
sbar11 = ttk.Scrollbar(frame1, orient="vertical",
command=table11.yview)
sbar11.place(x=580, y=2, height=425)
table11.configure(yscrollcommand=sbar11.set)
frame1.mainloop()
#主界面
def start():
frame0 = Tk()
frame0.title('查找文件')
frame0.geometry('510x95')
label01 = Label(frame0, text='位置')
label01.place(x=15, y=10)
text01 = Entry(frame0, width=64)
text01.place(x=50, y=10)
label02 = Label(frame0, text='关键字')
label02.place(x=3, y=35)
text02 = Entry(frame0, width=64)
text02.place(x=50, y=35)
btn01=Button(frame0, text='查找', width=10,
command=lambda:onBtn01(path=text01.get(),keyword=text02.get()))
btn01.place(x=422, y=60)
frame0.mainloop()
start()