From 743aadf02a46d2c1a0ca8881f7082e915b8b21fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E7=8F=8D=E5=A6=AE?= <9328698+jane-chen@user.noreply.gitee.com> Date: Sat, 23 Apr 2022 03:11:56 +0000 Subject: [PATCH] =?UTF-8?q?update=2010-=E5=AD=A6=E6=A0=A1=E5=BE=AE?= =?UTF-8?q?=E8=AF=BE=E5=B9=B3=E5=8F=B0=E6=8E=A8=E8=8D=90=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E8=AE=BE=E8=AE=A1-=E6=8C=91=E6=88=98=E9=A1=B9=E7=9B=AE-?= =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E5=8D=95=E5=85=83=20=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E3=80=81=E9=93=BE=E8=A1=A8=E4=B8=8E=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?/README.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../README.md" | 237 ++++++++++++++++++ 1 file changed, 237 insertions(+) diff --git "a/10-\345\255\246\346\240\241\345\276\256\350\257\276\345\271\263\345\217\260\346\216\250\350\215\220\345\212\237\350\203\275\350\256\276\350\256\241-\346\214\221\346\210\230\351\241\271\347\233\256-\347\254\254\344\272\214\345\215\225\345\205\203 \346\225\260\347\273\204\343\200\201\351\223\276\350\241\250\344\270\216\345\255\227\347\254\246\344\270\262/README.md" "b/10-\345\255\246\346\240\241\345\276\256\350\257\276\345\271\263\345\217\260\346\216\250\350\215\220\345\212\237\350\203\275\350\256\276\350\256\241-\346\214\221\346\210\230\351\241\271\347\233\256-\347\254\254\344\272\214\345\215\225\345\205\203 \346\225\260\347\273\204\343\200\201\351\223\276\350\241\250\344\270\216\345\255\227\347\254\246\344\270\262/README.md" index 344cf20..161db65 100644 --- "a/10-\345\255\246\346\240\241\345\276\256\350\257\276\345\271\263\345\217\260\346\216\250\350\215\220\345\212\237\350\203\275\350\256\276\350\256\241-\346\214\221\346\210\230\351\241\271\347\233\256-\347\254\254\344\272\214\345\215\225\345\205\203 \346\225\260\347\273\204\343\200\201\351\223\276\350\241\250\344\270\216\345\255\227\347\254\246\344\270\262/README.md" +++ "b/10-\345\255\246\346\240\241\345\276\256\350\257\276\345\271\263\345\217\260\346\216\250\350\215\220\345\212\237\350\203\275\350\256\276\350\256\241-\346\214\221\346\210\230\351\241\271\347\233\256-\347\254\254\344\272\214\345\215\225\345\205\203 \346\225\260\347\273\204\343\200\201\351\223\276\350\241\250\344\270\216\345\255\227\347\254\246\344\270\262/README.md" @@ -26,6 +26,243 @@ 3. 项目程序实现 +import random +from tkinter import * +from tkinter import ttk +from PIL import Image , ImageTk + + +# 设置背景图片 +def get_image(filename , width , height) : + im = Image.open(filename).resize((width , height)) + return ImageTk.PhotoImage(im) + + +# 获取表格选中对象的值 +def tb_click(event) : + global user , subject , sj_name , item_text + for item in table.selection() : + item_text = table.item(item , "values") # item_text为元组类型 + user = item_text[1] + subject = item_text[2] + sj_name = item_text[3] + + +# 添加数据到表格 +def add_table(tree , data) : + tree.insert('' , END , values = data) # 添加数据到末尾 + + +# 删除表格中的值 +def del_table(tree) : + x = tree.get_children() + for item in x : + tree.delete(item) + + +# 设置表格字体 +# def set_tree_font(tree): +# items=tree.get_children() #获取所有的单元格 +# for item in items: +# tree.item(item,tags="oddrow") #对每一个单元格命名 +# tree.tag_configure("oddrow",font=("宋体",11)) +# tree.update() + + +# 让界面初始化 +def Initializing() : + str.set("") + del_table(table) + del_table(table2) + for index , data in enumerate(data1) : + if data[0] != "微课ID" : + add_table(table , data) + + +# 导入所有微课 +def wk_read() : + global data1 , maxid + csv_file = open("wk.csv" , "r") + flines = csv_file.read() # 以行的形式导入数据 + rows = flines.split("\n") # 生成列表 + data1 = [] + maxid = 0 + csv_file.close() + for row in rows : # 将点播记录按顺序存入列表data中,可视其为二维数组,其中数据类型均为字符串 + if row != "" : + split_row = row.split(",") + data1.append(split_row) + for index , data in enumerate(data1) : + if data[0] != "微课ID" : + add_table(table , data) + for i in range(1 , len(data1)) : + if maxid < int(data1[i][0]) : + maxid = int(data1[i][0]) + + +# 导入历史点播记录 +def import_data() : + global data2 + csv_file = open("wk_click.csv" , "r") + flines = csv_file.read() # 以行的形式导入数据 + rows = flines.split("\n") # 生成列表 + data2 = [] + csv_file.close() + for row in rows : # 将点播记录按顺序存入列表data中,可视其为二维数组,其中数据类型均为字符串 + if row != "" : + split_row = row.strip().split(",") + data2.append(split_row) + + +# 设置搜索规则,显示对应微课列表 +def search() : + del_table(table) + n = value.get() + if n == "" : + wk_read() + else : + for index , data in enumerate(data1) : + if n in data[1] or n in data[2] or \ + n[:2] in data[1] and n[2 :] in data[2] or \ + n[:2] in data[2] and n[2 :] in data[1] or \ + n[:4] in data[2] and n[4 :] in data[1] : + add_table(table , data) + ''' n in data[1]匹配年级是否一致 + n in data[2]匹配科目是否一致 + n[:2] in data[1] and n[2:] in data[2] 匹配年级+学科组合 + n[:2] in data[2] and n[2:] in data[1] 匹配两个字的学科+年级组合 + n[:4] in data[2] and n[4:] in data[1] 匹配四个字的学科+年级组合''' + + +# 设置微课选择及个性化推荐 +def choice() : + str.set(user + " " + subject + " " + sj_name) + del_table(table2) + m = random.randint(1 , 3) + if m == 1 : + user_type() + elif m == 2 : + wk_type() + else : + zh_type() + # set_tree_font(table2) # 调整table2的字体样式 + + +# 查找点播量前三的数据 +def top_3(num) : + for i in range(3) : + n = num.index(max(num)) # n记录每次列表中最大值的位置 + for index , data in enumerate(data1[1 :len(data1)]) : + if n == int(data[0]) : + add_table(table2 , data) + num[n] = 0 + break + + +# 推荐规则一,根据年级段匹配一致进行推荐 +def user_type() : + num = [0] * (maxid + 1) + for i in range(1 , len(data2)) : + if data2[i][2] == user : + t = int(data2[i][1]) + num[t] = num[t] + 1 + top_3(num) + + +# 推荐规则二,根据科目匹配一致进行推荐 +def wk_type() : + num = [0] * (maxid + 1) + for i in range(1 , len(data2)) : + if data2[i][3] == subject : + t = int(data2[i][1]) + num[t] = num[t] + 1 + top_3(num) + + +# 推荐规则三,根据年级段及科目都匹配一致进行推荐 +def zh_type() : + # list2.insert("end" , "\n") + num = [0] * (maxid + 1) # 存储各微课被点播次数 + for i in range(1 , len(data2)) : + if data2[i][2] == user and data2[i][3] == subject : + t = int(data2[i][1]) + num[t] = num[t] + 1 + top_3(num) + + +if __name__ == '__main__' : + top = Tk() + top.geometry("1000x430") + top.title("微课推荐系统") + # 设置画布,存放背景图片 + canvas = Canvas(top , width = 1000 , height = 430) + im = get_image("1.jpg" , 1000 , 430) + canvas.create_image(500 , 215 , image = im) + canvas.pack() + # 设置标签 + label1 = Label(top , text = "微课列表如下,请选择自己想要点播的微课:" , bg = "#FFF0F5" , font = ("楷体" , 14)) + label2 = Label(top , text = "温馨提醒:当搜索栏为空时,搜索结果为所有微课列表。" , fg = "#FF6347") + label3 = Label(top , text = "猜你喜欢:" , anchor = "nw" , bg = "#FFB6C1" , font = ("黑体" , 12)) + label4 = Label(top , text = "你选择的微课为:" , bg = "#B0E0E0" , font = ("黑体" , 12)) + label1.place(x = 50 , y = 75) + label2.place(x = 700 , y = 70) + label3.place(x = 500 , y = 200) + label4.place(x = 500 , y = 150) + # 设置按钮 + button1 = Button(top , text = "确定" , width = 10 , bg = "#B0E0E6" , font = ("黑体" , 14) , command = choice) + button2 = Button(top , text = "搜索" , width = 7 , bg = "#FEF5EE" , command = search) + button3 = Button(top , text = "初始化" , width = 9 , bg = "#B0E0E6" , font = ("黑体" , 14) , command = Initializing) + button1.place(x = 360 , y = 350) + button2.place(x = 900 , y = 30) + button3.place(x = 850 , y = 350) + # 创建并设置文本框 + value = StringVar() + entry = Entry(top , bg = "#F5F5F5" , textvariable = value) + entry.place(x = 750 , y = 40) + + str = StringVar() + entry2 = Entry(top , width = 30 , textvariable = str , bg = "#FFF5EE" , font = ("华文楷体" , 15 , "bold")) + entry2.place(x = 650 , y = 140 , height = 30) + frame1 = Frame(top) # 创建 frame框架,用来放置表格和滚动条 + frame1.place(x = 40 , y = 120) + sb1 = Scrollbar(frame1) # 创建滚动条 + columns = ["微课ID" , "用户类型" , "微课类型" , "微课名"] # 创建并设置表格 + table = ttk.Treeview(frame1 , height = 10 , columns = columns , show = "headings" , yscrollcommand = sb1.set) + table.column("微课ID" , width = 50 , anchor = "center") + table.column("用户类型" , width = 75 , anchor = "center") + table.column("微课类型" , width = 75 , anchor = "center") + table.column("微课名" , width = 220) + table.heading("微课ID" , text = "微课ID") + table.heading("用户类型" , text = "用户类型") + table.heading("微课类型" , text = "微课类型") + table.heading("微课名" , text = "微课名") + sb1.config(command = table.yview) # 关联滚动条 + table.bind("" , tb_click) # 绑定表格点击事件 + + sb1.pack(side = RIGHT , fill = Y) + table.pack(fill = BOTH , expand = True) + frame2 = Frame(top) + # columns2 = ["微课ID" , "用户类型" , "微课类型" , "微课名","点播次数"] # 创建并设置表格 + table2 = ttk.Treeview(frame2 , height = 4 , columns = columns , show = "headings") + table2.column("微课ID" , width = 50 , anchor = "center") + table2.column("用户类型" , width = 80 , anchor = "center") + table2.column("微课类型" , width = 80 , anchor = "center") + table2.column("微课名" , width = 240) + # table2.column("点播次数" , width = 50,anchor="center") + table2.heading("微课ID" , text = "微课ID") + table2.heading("用户类型" , text = "用户类型") + table2.heading("微课类型" , text = "微课类型") + table2.heading("微课名" , text = "微课名") + # table2.heading("点播次数" , text = "点播次数") + table2.pack(side = BOTTOM , fill = BOTH , expand = True) + frame2.place(x = 500 , y = 230) + # 设置列表框 + var1 = StringVar() + var2 = StringVar() + wk_read() # 导入微课列表 + import_data() # 导入历史点播记录 + top.mainloop() + #### 第三阶段:项目汇报阶段 -- Gitee