2024/05/21
学习时长:3小时
代码函数:350左右
今天主要完成了python的实验三的两个算盘的设计部分
第一部分,画一个算盘
import tkinter as tk from PIL import Image, ImageTk, ImageOps #创建主窗口 top_oval = [int for i in range(5)] # 定义列表存储5个上珠 below_oval = [[int for i in range(5)] for i in range(4)] # 定义列表存储4*5个下珠 chushi = [[0 for j in range(5)] for i in range(4)] # 所有下珠的初始坐标 num = [[0 for i in range(5)] for j in range(4)] # 五个下珠分别对应的数值 num2 = [0 for i in range(5)] # 五个上珠分别对应的数值 root = tk.Tk() root.geometry('1300x700') root.title("电子算盘") tank = tk.Canvas(root, width=1300, height=700, bg='ivory') # 创建画板 tank.pack() # 显示画板 tank.create_rectangle(30, 30, 700, 150, width=3) # 左上侧方框 tank.create_rectangle(30, 150, 700, 600, width=3) # 左下侧方框 for i in range(5): # 生成串算珠的线 line_shu = tank.create_line(140 + i*110, 30 , 140 + i*110, 600, width=3) for i in range(5): # 生成5个上珠 top_oval[i] = tank.create_oval(100 + 110*i, 100, 180 + 110*i, 140, fill='orange', tags=f"top{i}") for i in range(4): # 生成4*5个下珠 for j in range(5): below_oval[i][j] = tank.create_oval(100 + 110*j, 400+50*i, 180 + 110*j, 440+50*i, fill='yellow', tags=f"below{i}{j}") chushi[i][j] = tank.coords(below_oval[i][j]) def get_empty(): empty = [[0 for j in range(5)] for i in range(4)] for i in range(4): for j in range(5): if tank.coords(below_oval[i][j]) != chushi[i][j]: empty[i][j] = 1 print(empty) return empty def bind(): def handler_adaptor(handler, fun, row, col): """事件处理函数的适配器,相当于中介,可以帮助tag_bind函数传递参数""" return lambda event, handler=handler, fun=fun, col=col, row=row: handler(event=event, fun=fun, row=row, col=col) def handler_adaptor2(handler2, fun, row): """事件处理函数的适配器,相当于中介,可以帮助tag_bind函数传递参数""" return lambda event, handler2=handler2, fun=fun, row=row: handler2(event=event, fun=fun, row=row) def handler(event, fun, row, col): """下珠上划""" content = fun # 这个就是被点击的算珠id empty = get_empty() if row == 0: if float(tank.coords(content)[1]) - 200 >= 140: tank.move(content, 0, -200) else: if empty[row - 1][col] == 1: if float(tank.coords(content)[1]) - 200 >= 110 + 50 * (row + 1): tank.move(content, 0, -200) def handler2(event, fun, row): """上珠上划""" content = fun # 这个就是被点击的算珠id if float(tank.coords(content)[1]) - 50 >= 40: tank.move(content, 0, -50) def handler3(event, fun, row, col): """下珠下划""" content = fun # 这个就是被点击的算珠id empty = get_empty() if row == 3: if float(tank.coords(content)[1]) + 200 <= 650: tank.move(content, 0, 200) else: if empty[row][col] == 1: tank.move(content, 0, 200) def handler4(event, fun, row): """上珠下划""" content = fun # 这个就是被点击的算珠id if float(tank.coords(content)[1]) + 50 <= 100: tank.move(content, 0, 50) for i in range(5): tank.tag_bind(top_oval[i], "<Button-1>", handler_adaptor2(handler2, fun=top_oval[i], row=i)) tank.tag_bind(top_oval[i], "<Button-3>", handler_adaptor2(handler4, fun=top_oval[i], row=i)) for i in range(4): for j in range(5): tank.tag_bind(below_oval[i][j], "<Button-1>", handler_adaptor(handler, fun=below_oval[i][j], row=i, col=j)) tank.tag_bind(below_oval[i][j], "<Button-3>", handler_adaptor(handler3, fun=below_oval[i][j], row=i, col=j)) bind() # 绘制图像 image = Image.open("experiment3\myselfw.jpg").convert("L") width, height = image.size new_img = Image.new("L", image.size, color=255) pixels = image.load() for x in range(0, height, 8): for y in range(0, width, 8): if x < width and y < height: if pixels[x, y] < 140: # 如果像素值小于128,则绘制一个黑色点 tank.create_rectangle((x-1)/2+750, y/2+100, (x+1)/2+750, (y+1)/2+100, fill='black') root.mainloop()
由于这个不好完成下一阶段,所以有重新更改了一下
from tkinter import * import tkinter.messagebox as msg from random import randint def initWindow(): canvas.create_rectangle(30, 30, 700, 150, width=3) # 左上侧方框 canvas.create_rectangle(30, 150, 700, 600, width=3) # 左下侧方框 for i in range(5): # 生成串算珠的线 line_shu = canvas.create_line(140 + i*110, 30 , 140 + i*110, 600, width=3) for i in range(5): # 生成5个上珠 top_oval[i] = canvas.create_oval(100 + 110*i, 100, 180 + 110*i, 140, fill='orange', tags=f"top{i}") for i in range(4): # 生成4*5个下珠 for j in range(5): below_oval[i][j] = canvas.create_oval(100 + 110*j, 400+50*i, 180 + 110*j, 440+50*i, fill='yellow', tags=f"below{i}{j}") chushi[i][j] = canvas.coords(below_oval[i][j]) global start_button, label_sum, username, entry_username, username_text, window_showusername global label_time, window_time, window_question global var_username, var_question, var_msg, window_answer, window_msg sum = Label(window, width=20, height=2, bg='ivory', textvariable=var, font=('Arial', 14)) label_sum = canvas.create_window(1000, 180, window=sum, anchor=NE) canvas.itemconfigure(label_sum, state="hidden") button = Button(window, width=30, height=4, bg='ivory', text="开始测试吧!", command=adjust) start_button = canvas.create_window(1010, 400, window=button, anchor=NE) text = Label(window, text="用户名:", width=10, height=2,bg='ivory') username_text = canvas.create_window(810, 300, window=text, anchor=CENTER) username = Entry(window, font=('Arial', 14),width=13) entry_username = canvas.create_window(920, 300, window=username, anchor=CENTER) label_showusername = Label(window, textvariable=var_username, width=15, height=2, bg='ivory', font=('Arial', 14)) window_showusername = canvas.create_window(1020, 100, window=label_showusername, anchor=NE) canvas.itemconfigure(window_showusername, state="hidden") label_time = Label(window, width=10, height=2, bg='ivory', font=('Arial', 14)) window_time = canvas.create_window(850, 100, window=label_time, anchor=NE) canvas.itemconfigure(window_time, state="hidden") label_question = Label(window, width=30, height=2,bg='ivory', font=('Arial', 14), textvariable=var_question) window_question = canvas.create_window(880, 280, window=label_question, anchor=CENTER) canvas.itemconfigure(window_question, state="hidden") answer_button = Button(window, width=10, height=2, bg='ivory', text="提交答案", command=judge, font=('Arial', 14)) window_answer = canvas.create_window(880, 500, window=answer_button, anchor=CENTER) canvas.itemconfigure(window_answer, state="hidden") msg_label = Label(window, width=30, height=6, bg='ivory', font=('Arial', 14), textvariable=var_msg) window_msg = canvas.create_window(880, 360, window=msg_label, anchor=CENTER) canvas.itemconfigure(window_msg, state="hidden") def run_time(target): def counting(): global time time += 1 global li print(li) if li != 5: target.config(text=f"所用时间:{str(time)}s") else: target.config(text="游戏结束") target.after(1000, counting) # 间隔1000毫秒再次执行counting函数 counting() def adjust(): global ques, li, true, false, all_username myusername = username.get() all_username = myusername if myusername != "": """对画布中的部件进行一些调整""" for i in range(5): ques[i] = get_question() canvas.itemconfigure(label_sum, state="normal") canvas.itemconfigure(window_showusername, state="normal") canvas.itemconfigure(window_time, state="normal") canvas.itemconfigure(window_question, state="normal") canvas.itemconfigure(window_answer, state="normal") canvas.itemconfigure(window_msg, state="normal") var_question.set(f"问题:{ques[0][0]}") run_time(label_time) var_username.set(f"用户名:{myusername}") sum = get_sum() var.set(f"当前数值:{sum}") var_msg.set(f"总题数:5\n已经做了0题\n已做对0题\n已做错0题") canvas.itemconfigure(start_button, state="hidden") canvas.itemconfigure(entry_username, state="hidden") canvas.itemconfigure(username_text, state="hidden") else: msg.showinfo("错误", "用户名不能为空白") def judge(): def next(): var_question.set(f"问题:{ques[li][0]}") global li, false, true sum = get_sum() myanw[li][0] = ques[li][0] myanw[li][1] = sum if sum == int(ques[li][1]): true += 1 msg.showinfo("答案正确", f"恭喜你做对了") else: false += 1 msg.showinfo("答案错误", f"做错了!正确答案是{ques[li][1]}") li += 1 var_msg.set(f"总题数:5\n已经做了{li}题\n已做对{true}题\n已做错{false}题") if li != 5: next() else: msg.showinfo("游戏结束", f"以下是你的战绩\n总题数:5\n总用时:{time}s\n总成绩:{true * 20}分\n做对了{true}题\n做错了{false}题") msg.showinfo("游戏结束", f"{all_username},你好\n以下是你的答题明细\n总题数:5\n1.{myanw[0][0]}={myanw[0][1]}\n" f"2.{myanw[1][0]}={myanw[1][1]}\n3.{myanw[2][0]}={myanw[2][1]}\n" f"4.{myanw[3][0]}={myanw[3][1]}\n5.{myanw[4][0]}={myanw[4][1]}\n") exit() def get_question(): answer, num1, num2 = 0, 0, 0 operator = "" temp = randint(1, 2) if temp == 1: while 1: num1 = randint(0, 99999) num2 = randint(0, 99999) if num1 + num2 <= 99999: break answer = num1 + num2 operator = "+" elif temp == 2: while 1: num1 = randint(0, 99999) num2 = randint(0, 99999) if num1 - num2 > 0: break answer = num1 - num2 operator = "-" equation = str(num1) + operator + str(num2) return equation, answer def get_sum(): sum = 0 temp = 5 for i in range(5): temp -= 1 if num2[i] == 1: sum += 10 ** temp * 5 temp = 5 for j in range(5): temp -= 1 for i in range(4): if num[i][j] == 1: sum += 10 ** temp return sum def bind(): def handler_adaptor(handler, fun, row, col): """事件处理函数的适配器,相当于中介,可以帮助tag_bind函数传递参数""" return lambda event, handler=handler, fun=fun, col=col, row=row: handler(event=event, fun=fun, row=row, col=col) def handler_adaptor2(handler2, fun, row): """事件处理函数的适配器,相当于中介,可以帮助tag_bind函数传递参数""" return lambda event, handler2=handler2, fun=fun, row=row: handler2(event=event, fun=fun, row=row) def handler(event, fun, row, col): """下珠上划""" content = fun # 这个就是被点击的算珠id if row == 0: if float(canvas.coords(content)[1]) - 200 >= 140 and num[row][col] == 0: canvas.move(content, 0, -200) num[row][col] = 1 else: if num[row - 1][col] == 1: if float(canvas.coords(content)[1]) - 200 >= 110 + 50 * (row + 1) and num[row][col] == 0: canvas.move(content, 0, -200) num[row][col] = 1 sum = get_sum() var.set(f"当前数值:{sum}") def handler2(event, fun, row): """上珠上划""" content = fun # 这个就是被点击的算珠id if float(canvas.coords(content)[1]) - 50 >= 40: canvas.move(content, 0, -50) num2[row] = 1 sum = get_sum() var.set(f"当前数值:{sum}") def handler3(event, fun, row, col): """下珠下划""" content = fun # 这个就是被点击的算珠id if row == 3: if num[row][col] == 1: canvas.move(content, 0, 200) num[row][col] = 0 else: if num[row + 1][col] == 0 and num[row][col] == 1: canvas.move(content, 0, 200) num[row][col] = 0 sum = get_sum() var.set(f"当前数值:{sum}") def handler4(event, fun, row): """上珠下划""" content = fun # 这个就是被点击的算珠id if float(canvas.coords(content)[1]) + 50 <= 100: canvas.move(content, 0, 50) num2[row] = 0 sum = get_sum() var.set(f"当前数值:{sum}") for i in range(5): canvas.tag_bind(top_oval[i], "<Button-1>", handler_adaptor2(handler2, fun=top_oval[i], row=i)) canvas.tag_bind(top_oval[i], "<Button-3>", handler_adaptor2(handler4, fun=top_oval[i], row=i)) for i in range(4): for j in range(5): canvas.tag_bind(below_oval[i][j], "<Button-1>", handler_adaptor(handler, fun=below_oval[i][j], row=i, col=j)) canvas.tag_bind(below_oval[i][j], "<Button-3>", handler_adaptor(handler3, fun=below_oval[i][j], row=i, col=j)) if __name__ == "__main__": window = Tk() window.title("电子算盘") window.geometry("1300x800") canvas = Canvas(window, width="1300", height="800", bg='ivory') canvas.pack() ques = [0 for i in range(5)] myanw = [[0, 0]for i in range(5)] li = 0 time, true, false = 0, 0, 0 start_button, label_sum, username, entry_username = 0, 0, 0, 0 username_text, window_showusername, window_time = 0, 0, 0 label_time, window_question, window_answer = 0, 0, 0 all_username = "" window_msg = 0 var = StringVar() var_username = StringVar() var_question = StringVar() var_msg = StringVar() top_oval = [int for i in range(5)] # 定义列表存储5个上珠 below_oval = [[int for i in range(5)] for i in range(4)] # 定义列表存储4*5个下珠 chushi = [[0 for j in range(5)] for i in range(4)] # 所有下珠的初始坐标 num = [[0 for i in range(5)] for j in range(4)] # 4*5个下珠分别是否被拨动 num2 = [0 for i in range(5)] # 五个上珠分别是否被拨动 initWindow() bind() window.mainloop()