python 搜索一个指定txt 的字符串 并输出其下面两行
发布网友
发布时间:2022-04-24 17:52
我来回答
共1个回答
热心网友
时间:2022-04-18 16:01
from tkinter import *
from tkinter import ttk
from tkinter import font
import os
import arrange
# 课程的字典
crslst = {}
crslst_in_num = {}
# GUI
root = Tk()
# frame2
# 右侧的内容
frame2 = ttk.Frame(root, borderwidth=5, relief="sunken")
frame2.grid(column=1, row=0)
# 将数字转换为汉字的字典
numtoch = {1: '一', 2: '二', 3: '三', 4: '四', 5: '五'}
# 设置课表周围周一至周五和1至13的标签
for num in list(range(1, 6)):
ttk.Label(frame2, text='周' + numtoch[num]).grid(column=num, row=0)
emptylist = []
for num in list(range(1, 14)):
ttk.Label(frame2, text='第' + str(num) + '节').grid(column=0, row=num)
# 课表主体
schd1 = Listbox(frame2, height=13)
schd1.grid(column=1, row=1, rowspan=13, sticky=(N, S, E, W))
schd2 = Listbox(frame2, height=13)
schd2.grid(column=2, row=1, rowspan=13, sticky=(N, S, E, W))
schd3 = Listbox(frame2, height=13)
schd3.grid(column=3, row=1, rowspan=13, sticky=(N, S, E, W))
schd4 = Listbox(frame2, height=13)
schd4.grid(column=4, row=1, rowspan=13, sticky=(N, S, E, W))
schd5 = Listbox(frame2, height=13)
schd5.grid(column=5, row=1, rowspan=13, sticky=(N, S, E, W))
# 设置字体
ft = font.Font(family='Microsoft YaHei', size=12, weight='normal')
# 为了便捷将五天的课表作为一list
daylist = (schd1, schd2, schd3, schd4, schd5)
# 设置课表
for day in daylist:
day['selectmode'] = 'extended'
day['exportselection'] = False
for num in range(1, 14):
day.insert(0, '无课')
day['font'] = ft
day['width'] = 10
# 课表下四个按钮和一个combobox
# 改变颜色
def chcolor():
print("\033[1;42m\033[0m")
butdont = ttk.Button(frame2, text='不上课', command=chcolor).grid(column=3, row=15)
# 清除选择
butusel = ttk.Button(frame2, text='清除选择').grid(column=4, row=15)
Label(frame2, text='选择课表').grid(column=0, row=15, columnspan=2, sticky=W)
outputlist = ttk.Combobox(frame2, textvariable=['待选', 'ok'])
outputlist.grid(column=1, row=15, columnspan=2, sticky=E)
# frame1
# 左侧的内容
frame1 = ttk.Frame(root, borderwidth=5, relief="sunken")
frame1.grid(column=0, row=0)
# 设置上方的label
Label(frame1, text='输入课程信息').grid(column=0, row=0, columnspan=2)
# 输入课程号的Label和entry
Label(frame1, text='课程号:').grid(column=0, row=1, sticky=W)
coursenum = StringVar()
inputcoursenum = ttk.Entry(frame1, textvariable=coursenum, width=13)
inputcoursenum.grid(column=1, row=1, sticky=E)
# 已选课程的listbox
Label(frame1, text='已选课程').grid(column=0, row=3, sticky=W)
courselist = Listbox(frame1, height=8, width=13, exportselection=False)
courselist.grid(column=0, row=4, columnspan=2, rowspan=8, sticky=W)
def isrtcrs(): # 加入课程
k = 0
num = 0
print(num, courselist.size())
while num < courselist.size():
if inputcoursenum.get() == courselist.get(num):
k = 1
num += 1
if k == 0:
courselist.insert(0, inputcoursenum.get())
crslst[inputcoursenum.get()] = []
crslst_in_num[inputcoursenum.get()] = []
print(crslst)
print(crslst_in_num)
return
def delcrs(): # 删除课程
# print(courselist.curselection())
# print(courselist.get(courselist.curselection()))
crslst_in_num.pop(courselist.get(courselist.curselection()))
crslst.pop(courselist.get(courselist.curselection()))
courselist.delete(courselist.curselection())
print(crslst)
print(crslst_in_num)
return
def shwtm(*args): # 当选中一门课程,时间listbox改为对应的时间
print('here')
timelist.delete(0, END)
tmlst = crslst[courselist.get(courselist.curselection())]
if len(tmlst) != 0:
for time in tmlst:
timelist.insert(END, time)
return
# courselist的选中事件
courselist.bind('<<ListboxSelect>>', shwtm)
# 加入课程和删除课程的按钮
addcourse = ttk.Button(frame1, text='加入课程', command=isrtcrs).grid(column=0, row=2)
deletcourse = ttk.Button(frame1, text='删除课程', command=delcrs).grid(column=0, row=12)
# 已选课程的时间listbox
Label(frame1, text='时间列表').grid(column=1, row=3, sticky=E)
timelist = Listbox(frame1, height=8, width=13, exportselection=False)
timelist.grid(column=1, row=4, columnspan=2, rowspan=8, sticky=E)
def isrttim(): # 加入时间
num = 1
time = []
time_in_num = []
strofod = ''
for day in daylist:
if len(day.curselection()) != 0:
strofod = numtoch[num]
temp = day.curselection()
for cls in temp:
strofod = strofod + ' ' + str(cls + 1)
time_in_num.append((num - 1) * 13 + (cls + 1))
num += 1
if len(strofod) != 0:
time.append(strofod)
strofod = []
if len(time) != 0:
timelist.insert(END, time)
crslst[courselist.get(courselist.curselection())].append(time)
crslst_in_num[courselist.get(courselist.curselection())].append(time_in_num)
print(crslst)
print(crslst_in_num)
return
def deltim(): # 删除时间
print(timelist.curselection())
for sel in timelist.curselection():
crslst[courselist.get(courselist.curselection())].pop(sel)
crslst_in_num[courselist.get(courselist.curselection())].pop(sel)
timelist.delete(timelist.curselection())
print(crslst)
print(crslst_in_num)
return
# 加入时间和删除时间的按钮
addtime = ttk.Button(frame1, text='加入时间', command=isrttim).grid(column=1, row=2)
delelttime = ttk.Button(frame1, text='删除时间', command=deltim).grid(column=1, row=12)
def creatcourselist(): # 建立课程列文本文档
with open('slist.txt', 'w') as slist:
slist.writelines(str(len(crslst_in_num)) + '\n') # 课程数
for crs, tlst in crslst_in_num.items():
slist.writelines(crs + '\n') # 课程名
slist.writelines(str(len(tlst)) + '\n') # 可选时间数
for time in tlst:
slist.writelines(str(len(time)) + '\n') # 某一可选时间的节数
for n in time:
slist.writelines(str(n) + '\n') # 第几节
return
def showoutput(): # 在右侧的选择课表combobox里显示可选课表
filename_list = []
for filename_e in os.listdir('output'):
filename_list.append(filename_e)
outputlist['values'] = filename_list
return
def arrg(): # 排课
print('here')
creatcourselist()
for filename_e in os.listdir('output'):
os.remove('output/' + filename_e)
arrange.arrangement_mod()
showoutput()
return
# 当用户选择某一可选课表时,展示课表
def showcrslist(*args):
print(outputlist.get())
for filename_e in os.listdir('output'): # 寻找所选项对应的文件
if filename_e == outputlist.get():
with open('output/' + filename_e, 'r') as f_r: # 打开对应的文件
nr = f_r.readlines() # 按行读取内容
k = 0
for day in daylist:
day.delete(0, END) # 删除课表的原有内容
for num in range(1, 14):
# print(nr[k])
# 加入新的内容
if nr[k] == '0\n':
day.insert(END, '')
else:
day.insert(END, nr[k].strip())
k += 1
break # 完成之后退出循环
return
# 可选课表combobox选择事件
outputlist.bind('<<ComboboxSelected>>', showcrslist)
# 排课的按钮
run = ttk.Button(frame1, text='排课', command=arrg).grid(column=0, row=13, columnspan=2)
# frame_search
frame_s = ttk.Frame(root, borderwidth=5, relief='sunken')
frame_s.grid(column=2, row=0)
ttk.Label(frame_s, text='搜索课程').grid(column=0, row=0, columnspan=2)
ttk.Label(frame_s, text='课程号:').grid(column=0, row=1, sticky=E)
crs_num = StringVar()
crs_search = ttk.Entry(frame_s, textvariable=crs_num, width=12).grid(column=1, row=1, sticky=E)
search_crs = ttk.Button(frame_s, text='搜索').grid(column=0, row=2)
adp = ttk.Button(frame_s, text='加入').grid(column=1, row=2)
ttk.Label(frame_s, text='课程号:').grid(column=0, row=3, columnspan=2, sticky=W)
ttk.Label(frame_s, text='课程名:').grid(column=0, row=4, columnspan=2, sticky=W)
ttk.Label(frame_s, text='上课时间:').grid(column=0, row=5, columnspan=2, sticky=W)
root.mainloop()