头歌实验代码

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
头歌计算机是一款基于数据表示的计算机实验软件,它能够通过实验logisim代码来实现各种数据表示的功能。 在头歌计算机中,我们可以使用logisim代码来对不同的数据进行表示。logisim代码是一种基于逻辑电路的描述语言,通过电路元件的连接和布线方式来实现特定的功能。对于数据表示来说,logisim代码可以用来描述不同的数值系统、数值转换、编码方式以及数据存储等方面。 比如,我们可以使用logisim代码来实现二进制、十进制、八进制和十六进制这些不同的数值系统之间的转换。通过设计逻辑电路,我们可以通过logisim代码将任意一个数值系统的数值转换成其他数值系统的表示。 此外,logisim代码还可以用来实现不同的编码方式,比如ASCII编码和BCD编码等。通过logisim代码,我们可以设计相应的逻辑电路来将字符和数字之间进行转换,并实现数据的传输和存储。 在头歌计算机中,我们还可以使用logisim代码来设计数据存储器。通过logisim代码,我们可以实现不同存储器的设计,比如寄存器、缓存以及主存等。通过逻辑电路的设计,我们可以实现数据的读写操作,并实现数据的存储和处理。 总之,通过实验logisim代码,我们可以在头歌计算机中实现各种数据表示的功能,包括不同数值系统之间的转换,不同编码方式的实现,以及数据存储器的设计和应用。这样,我们可以更加深入地理解和掌握计算机中的数据表示原理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值