当前位置:   article > 正文

头歌实验代码_表格样式头歌代码

表格样式头歌代码

python3 /home/

第一关 

  1. from tkinter import *
  2. frame = Tk()
  3. frame.title('我的界面')
  4. frame.geometry('400x200') # x是英文字母,表示乘号
  5. frame.mainloop()

第二关

  1. from tkinter import *
  2. def onBtn(): # 定义响应函数
  3. frame2=Tk()
  4. label=Label(frame2,text='Hello World!')
  5. label.place(x=60,y=50)
  6. frame2.mainloop()
  7. frame = Tk()
  8. frame.title('我的界面')
  9. frame.geometry('400x200')
  10. btn=Button(frame,
  11. text='一个按钮',
  12. font=('Kaiti',14),
  13. command=onBtn) # 指定响应函数
  14. btn.place(x=150,y=50)
  15. frame.mainloop()

 

 第三关

  1. from tkinter import *
  2. def onBtn(txt):
  3. frame2=Tk()
  4. frame2.geometry('200x50')
  5. label=Label(frame2,text=txt)
  6. label.place(x=1,y=1)
  7. frame2.mainloop()
  8. frame=Tk()
  9. frame.geometry('200x50')
  10. text=Entry(frame,width=20)
  11. text.place(x=5,y=5)
  12. btn=Button(frame,text='按钮',
  13. command=lambda:onBtn(txt=text.get()))
  14. btn.place(x=150,y=1)
  15. frame.mainloop()

第四关

  1. from tkinter import *
  2. from tkinter import ttk
  3. frame=Tk()
  4. frame.title('我的界面')
  5. frame.geometry('323x200')
  6. # 创建表格对象table
  7. table=ttk.Treeview(frame,show='headings',height=3)
  8. table.place(x=1,y=1)
  9. #给每列去个名字
  10. table['columns']=['id','name','age']
  11. #指定每列的宽度和对齐方式
  12. table.column('id',width=150,anchor='w')
  13. table.column('name',width=100,anchor='center')
  14. table.column('age',width=50,anchor='e')
  15. #指定每列的标题
  16. table.heading('id',text='学号',anchor='w')
  17. table.heading('name',text='姓名',anchor='center')
  18. table.heading('age',text='年龄',anchor='e')
  19. #往表格中添加4个学生的记录
  20. table.insert('',0,values=('200506021041','张三',16))
  21. table.insert('',0,values=('200506021042','李四',18))
  22. table.insert('',0,values=('200506021043','王五',17))
  23. table.insert('',0,values=('200506021044','赵六',18))
  24. #定义垂直滚动条
  25. sbar=ttk.Scrollbar(frame,
  26. orient="vertical",
  27. command=table.yview)
  28. sbar.place(x=300,y=1,height=100)
  29. table.configure(yscrollcommand=sbar.set)
  30. frame.mainloop()

第五关

  1. from tkinter import *
  2. from tkinter import ttk
  3. import psutil, os
  4. def BtoKMG(n):
  5. if n >= 1024 ** 3:
  6. return str(round(n / 1024 ** 3, 1)) + ' GB'
  7. elif n >= 1024 ** 2:
  8. return str(round(n / 1024 ** 2, 1)) + ' MB'
  9. else:
  10. return str(round(n / 1024 ** 1, 1)) + ' KB'
  11. def getMemInfo():
  12. m = psutil.virtual_memory()
  13. info = (BtoKMG(m.total), BtoKMG(m.used), BtoKMG(m.free))
  14. return info
  15. def getDiskInfo():
  16. info = []
  17. for part in psutil.disk_partitions():
  18. if not 'cdrom' in part.opts:
  19. usage = psutil.disk_usage(part.mountpoint)
  20. info.append((part.device,
  21. part.fstype,
  22. BtoKMG(usage.total),
  23. BtoKMG(usage.used),
  24. BtoKMG(usage.free),
  25. str(usage.percent) + '%'))
  26. return info
  27. # 存储信息界面frame1:包括3个表格table11~table133个标签label11~label13
  28. def onBtn01():
  29. frame1 = Tk()
  30. frame1.title('查看存储信息')
  31. frame1.geometry('660x280')
  32. #显示主存信息
  33. memInfo = getMemInfo()
  34. label11 = Label(frame1, text='主存信息')
  35. label11.place(x=296, y=10)
  36. table11 = ttk.Treeview(frame1, show='headings', height=1)
  37. table11['columns'] = ['total', 'used', 'free']
  38. table11.column('total', width=100, anchor='center')
  39. table11.column('used', width=100, anchor='center')
  40. table11.column('free', width=100, anchor='center')
  41. table11.heading('total', text='总容量')
  42. table11.heading('used', text='已用容量')
  43. table11.heading('free', text='剩余容量')
  44. table11.insert('', 0, values=memInfo)
  45. table11.place(x=175, y=38)
  46. diskInfo = getDiskInfo()
  47. label13 = Label(frame1, text='分区信息')
  48. label13.place(x=296, y=90)
  49. table13 = ttk.Treeview(frame1, show='headings', height=5)
  50. table13['columns'] = ['dev', 'type', 'total',
  51. 'used', 'free', 'rate']
  52. table13.column('dev', width=102, anchor='center')
  53. table13.column('type', width=102, anchor='center')
  54. table13.column('total', width=104, anchor='e')
  55. table13.column('used', width=104, anchor='e')
  56. table13.column('free', width=104, anchor='e')
  57. table13.column('rate', width=104, anchor='e')
  58. table13.heading('dev', text='分区')
  59. table13.heading('type', text='类型')
  60. table13.heading('total', text='总容量', anchor='e')
  61. table13.heading('used', text='已用容量', anchor='e')
  62. table13.heading('free', text='剩余容量', anchor='e')
  63. table13.heading('rate', text='已用比例', anchor='e')
  64. for i in range(len(diskInfo)):
  65. table13.insert('', i, values=diskInfo[i])
  66. table13.place(x=20, y=118)
  67. frame1.mainloop()
  68. def start():
  69. frame0 = Tk()
  70. frame0.title('我的资源管理器')
  71. frame0.geometry('300x100')
  72. btn01 = Button(frame0, text='查看存储信息',
  73. font=('Kaiti', 14), command=onBtn01)
  74. btn01.place(x=80, y=30)
  75. frame0.mainloop()
  76. start()

第六关

  1. from tkinter import *
  2. from tkinter import ttk
  3. import os
  4. #判断filepath对应的txt文件中是否包含keyword
  5. def isContain(filepath, keyword):
  6. flag = False #flag用于标识是否包含,最开始为False,即不包含
  7. txt = open(filepath)
  8. while True:
  9. line = txt.readline()
  10. if line == '':
  11. break
  12. if keyword.lower() in line.lower(): #若包含
  13. flag = True #flag变为True
  14. break #已经确定包含,不用再处理后面的内容,跳出循环
  15. txt.close()
  16. return flag
  17. #用于查找dirpath下面所有包含keyword的txt文件
  18. def find(dirpath, keyword):
  19. files = [] #用于存储满足条件的txt文件路径
  20. L = os.listdir(dirpath)
  21. for filename in L:
  22. path = os.path.join(dirpath, filename)
  23. if os.path.isfile(path): #如果是文件
  24. try:
  25. if isContain(path, keyword): #如果包含keyword
  26. files.append(path) #把这个文件的路径追加到files
  27. except:
  28. pass
  29. else: #若为文件夹
  30. files = files+find(path, keyword) #用find函数处理该文件夹,把查找的结果(即这个子文件夹下面所有满足条件的txt)放到files中
  31. return files
  32. #显示结果界面
  33. def onBtn01(path, keyword):
  34. files = find(path, keyword)
  35. files.sort()
  36. frame1 = Tk()
  37. frame1.title('查找结果')
  38. frame1.geometry('600x430')
  39. table11 = ttk.Treeview(frame1, show='headings', height=20)
  40. table11['columns'] = ['id', 'name', 'path']
  41. table11.column('id', width=50, anchor='center')
  42. table11.column('name', width=150, anchor='w')
  43. table11.column('path', width=378, anchor='w')
  44. table11.heading('id', text='编号', anchor='center')
  45. table11.heading('name', text='文件名', anchor='center')
  46. table11.heading('path', text='文件路径', anchor='center')
  47. for i in range(len(files)):
  48. path = files[i]
  49. name = os.path.split(path)[1]
  50. table11.insert('', i, values=(i+1, name, path))
  51. table11.place(x=1, y=1)
  52. sbar11 = ttk.Scrollbar(frame1, orient="vertical",
  53. command=table11.yview)
  54. sbar11.place(x=580, y=2, height=425)
  55. table11.configure(yscrollcommand=sbar11.set)
  56. frame1.mainloop()
  57. #主界面
  58. def start():
  59. frame0 = Tk()
  60. frame0.title('查找文件')
  61. frame0.geometry('510x95')
  62. label01 = Label(frame0, text='位置')
  63. label01.place(x=15, y=10)
  64. text01 = Entry(frame0, width=64)
  65. text01.place(x=50, y=10)
  66. label02 = Label(frame0, text='关键字')
  67. label02.place(x=3, y=35)
  68. text02 = Entry(frame0, width=64)
  69. text02.place(x=50, y=35)
  70. btn01=Button(frame0, text='查找', width=10,
  71. command=lambda:onBtn01(path=text01.get(),keyword=text02.get()))
  72. btn01.place(x=422, y=60)
  73. frame0.mainloop()
  74. start()

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号