6.2
python算盘2
import tkinter as tk
from tkinter import messagebox
from random import randint
from PIL import Image, ImageDraw, ImageTk
# 创建主窗口
tk_root = tk.Tk()
tk_root.title("电子算盘")
canvas = tk.Canvas(tk_root, width=1000, height=600, bg='ivory') # 创建画布
canvas.pack() # 显示画布
canvas.create_rectangle(30, 30, 520, 190, width=3) # 左上侧方框
canvas.create_rectangle(30, 190, 520, 570, width=3) # 左下侧方框
button = tk.Button()
button1 = [button for _ in range(5)] # 5个上珠
button2 = [[button for _ in range(5)] for _ in range(4)] # 四行,每行五个下珠
num = [[0 for _ in range(5)] for _ in range(4)] # 五个下珠分别对应的数值
num2 = [0 for _ in range(5)] # 五个上珠分别对应的数值
counter = 0
sure = tk.Button() # 确定按钮
st = tk.Button() # 启动检测按钮
equation = tk.Label() # 算式
answer = tk.Label(width=50, height=7) # 答题情况
name = tk.Entry() # 用户名输入
true_result = tk.Label(width=50, height=4) # 上一题的正确答案
digit = tk.Label(tk_root, bg='yellow', fg='blue', height=5, width=25, font='宋体 10 bold') # 计时器
true = 0 # 已做对题数
false = 0 # 做错题数
score = 0 # 题目得分
result = 0 # 每道题的正确答案
topic = "" # 题目
def draw_ellipse(canvas, x, y, width, height, color):
canvas.create_oval(x, y, x + width, y + height, fill=color, outline=color)
def draw_portrait():
# 创建空白图像
image = Image.new("RGB", (200, 200), "white")
draw = ImageDraw.Draw(image)
# 绘制脸部轮廓
draw.ellipse((50, 50, 150, 150), outline="black")
# 绘制眼睛
draw.ellipse((70, 80, 90, 100), outline="black")
draw.ellipse((110, 80, 130, 100), outline="black")
# 绘制鼻子
draw.line((100, 100, 100, 130), fill="black")
# 绘制嘴巴
draw.arc((70, 120, 130, 150), start=20, end-160, fill="black")
# 将图像转换为Tkinter PhotoImage
photo = ImageTk.PhotoImage(image)
# 在画布上显示自画像
canvas.create_image(780, 120, anchor=tk.NW, image=photo)
canvas.photo = photo # 防止图片被垃圾回收
def run_counter(digit, second): # 计时器
def counting():
global counter
if second == 1:
counter += 1
else:
counter += 0
digit.config(text="计时器:" + str(counter))
digit.after(1000, counting)
counting()
def getNum(num, num2): # 计算算盘总和
sum_ = 0
for i in num:
for j in i:
sum_ += j
for i in num2:
sum_ += i
return sum_
def suanshi(): # 生成随机加减法测试题
answer = 0
operator = ""
num1 = 0
num2 = 0
p = randint(1, 2)
if p == 1:
while True:
num1 = randint(0, 99999)
num2 = randint(0, 99999)
if num1 + num2 <= 99999:
break
answer = num1 + num2
operator = "+"
elif p == 2:
while True:
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 button_click_back(events): # 鼠标右击点击事件触发
widget = events.widget
for i in range(5):
if widget == button1[i]:
button1[i].place(x=40 + 100 * i, y=50 + 70 * 1)
num2[i] = 0
label = tk.Label(tk_root, text="当前数字:" + str(getNum(num, num2)), width=30, height=4)
label.place(x=780, y=30)
for i in range(4):
for j in range(5):
if widget == button2[i][j]:
if i == 3:
button2[3][j].place(x=40 + 100 * j, y=210 + 70 * (i + 1))
num[3][j] = 0
label = tk.Label(tk_root, text="当前数字:" + str(getNum(num, num2)), width=30, height=4)
label.place(x=780, y=30)
if i == 2:
button2[2][j].place(x=40 + 100 * j, y=210 + 70 * (i + 1))
button2[3][j].place(x=40 + 100 * j, y=210 + 70 * (i + 2))
num[2][j] = 0
num[3][j] = 0
label = tk.Label(tk_root, text="当前数字:" + str(getNum(num, num2)), width=30, height=4)
label.place(x=780, y=30)
if i == 1:
button2[1][j].place(x=40 + 100 * j, y=210 + 70 * (i + 1))
button2[2][j].place(x=40 + 100 * j, y=210 + 70 * (i + 2))
button2[3][j].place(x=40 + 100 * j, y=210 + 70 * (i + 3))
num[1][j] = 0
num[2][j] = 0
num[3][j] = 0
label = tk.Label(tk_root, text="当前数字:" + str(getNum(num, num2)), width=30, height=4)
label.place(x=780, y=30)
if i == 0:
button2[0][j].place(x=40 + 100 * j, y=210 + 70 * (i + 1))
button2[1][j].place(x=40 + 100 * j, y=210 + 70 * (i + 2))
button2[2][j].place(x=40 + 100 * j, y=210 + 70 * (i + 3))
button2[3][j].place(x=40 + 100 * j, y=210 + 70 * (i + 4))
num[0][j] = 0
num[1][j] = 0
num[2][j] = 0
num[3][j] = 0
label = tk.Label(tk_root, text="当前数字:" + str(getNum(num, num2)), width=30, height=4)
label.place(x=780, y=30)
def button_click(events): # 鼠标左击点击事件触发
widget = events.widget
for i in range(5):
if widget == button1[i]:
button1[i].place(x=40 + 100 * i, y=50 + 70 * 0)
num2[i] = 10 ** (4 - i) * 5
label = tk.Label(tk_root, text="当前数字:" + str(getNum(num, num2)), width=30, height=4)
label.place(x=780, y=30)
for i in range(4):
for j in range(5):
if widget == button2[i][j]:
if i == 3:
button2[0][j].place(x=40 + 100 * j, y=210 + 70 * (i - 3))
button2[1][j].place(x=40 + 100 * j, y=210 + 70 * (i - 2))
button2[2][j].place(x=40 + 100 * j, y=210 + 70 * (i - 1))
button2[3][j].place(x=40 + 100 * j, y=210 + 70 * (i))
num[0][j] = 10 ** (4 - j) * 1
num[1][j] = 10 ** (4 - j) * 1
num[2][j] = 10 ** (4 - j) * 1
num[3][j] = 10 ** (4 - j) * 1
label = tk.Label(tk_root, text="当前数字:" + str(getNum(num, num2)), width=30, height=4)
label.place(x=780, y=30)
if i == 2:
button2[0][j].place(x=40 + 100 * j, y=210)
button2[1][j].place(x=40 + 100 * j, y=210 + 70 * 1)
button2[2][j].place(x=40 + 100 * j, y=210 + 70 * 2)
num[0][j] = 10 ** (4 - j) * 1
num[1][j] = 10 ** (4 - j) * 1
num[2][j] = 10 ** (4 - j) * 1
label = tk.Label(tk_root, text="当前数字:" + str(getNum(num, num2)), width=30, height=4)
label.place(x=780, y=30)
if i == 1:
button2[0][j].place(x=40 + 100 * j, y=210)
button2[1][j].place(x=40 + 100 * j, y=210 + 70 * 1)
num[0][j] = 10 ** (4 - j) * 1
num[1][j] = 10 ** (4 - j) * 1
label = tk.Label(tk_root, text="当前数字:" + str(getNum(num, num2)), width=30, height=4)
label.place(x=780, y=30)
else:
button2[i][j].place(x=40 + 100 * j, y=210 + 70 * i)
num[0][j] = 10 ** (4 - j) * 1
label = tk.Label(tk_root, text="当前数字:" + str(getNum(num, num2)), width=30, height=4)
label.place(x=780, y=30)
def start():
global name
global equation
global sure
global result
global digit
global topic
st.place_forget()
digit.place(x=540, y=30)
run_counter(digit, 1)
tk.Label(tk_root, text="用户名", bg="ivory").place(x=540, y=150)
name = tk.Entry(tk_root, show='', font=('Arial', 14))
name.place(x=580, y=150)
p = suanshi()
topic = p[0]
result = p[1]
equation = tk.Label(tk_root, text=topic, width=40, height=4)
equation.place(x=540, y=200)
sure = tk.Button(text="确定", command=judge, width=10, height=3)
sure.place(x=850, y=200)
def judge(): # 判断结果
global true
global false
global score
global topic
global result
global true_result
if true + false == 5:
messagebox.showinfo('温馨提示', '恭喜您已做完所有题目!!!')
answer["text"] = "用户名:" + name.get() + "\n已答题数:" + str(true + false) + "\n做对题数:" + str(true) + "\n做错题数:" \
+ str(false) + "\n测试时长:" + str(counter) + "s" + "\n测试成绩:" + str(score) + "\n答题完毕!!!"
answer.place(x=540, y=400)
else:
print(getNum(num, num2), result)
if getNum(num, num2) == result:
true += 1
score += 20
else:
false += 1
answer["text"] = "总题数:5\n已答题数:" + str(true + false) + "\n已做对题数:" + str(true) + "\n做错题数:" + str(
false) + "\n得分:" + str(
score)
answer.place(x=540, y=400)
p = suanshi()
true_result["text"] = "上一题题目:" + topic + "\n上一题正确答案:" + str(result)
equation["text"] = p[0]
result = p[1]
equation.place(x=540, y=200)
true_result.place(x=540, y=300)
# 生成5个上珠
for i in range(5):
button1[i] = tk.Button(tk_root)
button1[i].bind("<Button-1>", button_click)
button1[i].bind("<Button-3>", button_click_back)
button1[i]["bg"] = "ivory"
button1[i]["border"] = "0"
draw_ellipse(canvas, 40 + 100 * i, 50 + 70, 50, 70, "orange")
# 四行,每行生成5个下珠
for i in range(4):
for j in range(5):
button2[i][j] = tk.Button(tk_root)
button2[i][j].bind("<Button-1>", button_click)
button2[i][j].bind("<Button-3>", button_click_back)
button2[i][j]["bg"] = "ivory"
button2[i][j]["border"] = "0"
draw_ellipse(canvas, 40 + 100 * j, 210 + 70 * (i + 1), 50, 70, "black")
# 绘制右侧人物像
draw_portrait()
st = tk.Button(text="启动测试", command=start, width=50, height=10)
st.place(x=600, y=100)
tk_root.mainloop()
浙公网安备 33010602011771号