要求
1.单击”出题“按钮时会在label控件中显示新出的题目;
2.单击”判分“按钮时会对用户在文本框中输入的结果进行判断;
3.将题目、用户输入的结果和判分结果一起显示到列表中,✓和✘用以上两个字符来表示。
源码
import tkinter as tk
import random as rd
from tkinter import scrolledtext
ls = ['+' , '-' , '*']
cnts = 0
def click_button1():
global ans_1
a = rd.randint(0,9)
b = rd.randint(0,9)
c = rd.choice(ls)
if(c == "*"):
ans_1 = a * b
str1 = "{}*{}=".format(a, b)
elif(c == "-"):
ans_1 = a - b
str1 = "{}-{}=".format(a, b)
else:
ans_1 = a + b
str1 = "{}+{}=".format(a, b)
result_num.set(str1)
def click_button2(txet):
ans_2 = Entry_ans_num.get()
ans_2 = int(ans_2)
if(ans_1 == ans_2):
text1.insert("end","{}={} √\n".format(result_num.get(),ans_2))
else:
text1.insert("end","{}={} ×\n".format(result_num.get(),ans_2))
root = tk.Tk()# 创建一个窗体
root.title("自动出题判分")# 创建一个标题
root.geometry('300x300+100+100')# 设置窗体大小
font = ('宋体' , 15)# 设置字体大小
#----------------------------------------------------------------------------------------------------------------------
result_num = tk.StringVar()# 设置 lable 数据
tk.Label(root , textvariable = result_num,font = font,height = 2).grid(row = 1 , column = 1)# 设置 Lable
#----------------------------------------------------------------------------------------------------------------------
Entry_ans_num = tk.StringVar()# 设置 Entry 数据
Entry_ans = tk.Entry(root,textvariable = Entry_ans_num).grid(row = 1, column = 3)#设置输入框
#----------------------------------------------------------------------------------------------------------------------
# 设置两个按钮
button_set = tk.Button(root,text = '出题' , width = 13 , font = font , relief = tk.FLAT , bg = '#FF69B4')
button_check = tk.Button(root,text = '判分' , width = 13 , font = font , relief = tk.FLAT , bg = '#FF69B4')
# 按钮位置
button_set.grid(row = 2 , column = 1 , padx = 4 , pady = 2)
button_check.grid(row = 2 , column = 3, padx = 4 , pady = 2)
#按钮功能
button_set.config(command = lambda : click_button1())
button_check.config(command = lambda : click_button2(text1))
#----------------------------------------------------------------------------------------------------------------------
# 设置文本框 , 注意这两个语句分开写 , 设置滑动文本框
text1 = scrolledtext.ScrolledText(root , width = 40 , height = 10)
text1.grid(row = 3, column = 1,columnspan = 3)
#----------------------------------------------------------------------------------------------------------------------
#显示
root.mainloop()
#----------------------------------------------------------------------------------------------------------------------
效果
个人感想:
不算是很方便 , 对初学者很不友好
本文含有隐藏内容,请 开通VIP 后查看