image.png (35.53 KB, 下载次数: 0)
下载附件
2023-6-2 06:39 上传
image.png (29.37 KB, 下载次数: 0)
下载附件
2023-6-2 06:40 上传
实现不同功能的计算需求。
原代码如下,欢迎提宝贵意见。
[color=]from
tkinter
[color=]import
*
[color=]import
random
[color=]def
[color=]jiajian
():
#
加减法函数,生成加数,和,被减数,减数,差
joj=random.randint(
[color=]1
[color=],
[color=]2
)
a = random.randint(
[color=]1
[color=],
[color=]999
)
b = random.randint(
[color=]1
[color=],
[color=]999
)
[color=]if
joj==
[color=]1
:
#
随机产生
h=a+b
[color=]return
[a
[color=],
[color=]'+'
[color=],
b
[color=],
h]
[color=]else
:
[color=]if
a > b:
h=a-b
[color=]return
[a
[color=],
[color=]'-'
[color=],
b
[color=],
h]
[color=]else
:
h=b-a
[color=]return
[b
[color=],
[color=]'-'
[color=],
a
[color=],
h]
[color=]def
[color=]cc
(i=
[color=]1
):
#
乘除运算,返回乘数,积。
i
表示难度,
1
为个位数,
2
为
2
位数,
3
为
3
位数
,j
,
1
是乘,
2
是除
jc = random.randint(
[color=]1
[color=],
[color=]2
)
[color=]if
i==
[color=]1
:
#
难度
1
a=random.randint(
[color=]2
[color=],
[color=]9
)
b = random.randint(
[color=]2
[color=],
[color=]9
)
j=a*b
[color=]if
jc==
[color=]1
:
#
乘法
[color=]return
[a
[color=],
[color=]'*'
[color=],
b
[color=],
j]
#1
为乘法,
2
为除法
[color=]else
:
[color=]return
[j
[color=],
[color=]'/'
[color=],
a
[color=],
b]
#1
为乘法,
2
为除法
[color=]elif
i==
[color=]2
:
#
难度
2
a = random.randint(
[color=]11
[color=],
[color=]99
)
b = random.randint(
[color=]11
[color=],
[color=]99
)
j = a * b
[color=]if
jc ==
[color=]1
:
#
乘法
[color=]return
[a
[color=],
[color=]'*'
[color=],
b
[color=],
j]
# 1
为乘法,
2
为除法
[color=]else
:
[color=]return
[j
[color=],
[color=]'/'
[color=],
a
[color=],
b]
# 1
为乘法,
2
为除法
[color=]else
:
#
难度
3
a = random.randint(
[color=]101
[color=],
[color=]999
)
b = random.randint(
[color=]101
[color=],
[color=]999
)
j = a * b
[color=]if
jc ==
[color=]1
:
#
乘法
[color=]return
[a
[color=],
[color=]'*'
[color=],
b
[color=],
j]
# 1
为乘法,
2
为除法
[color=]else
:
[color=]return
[j
[color=],
[color=]'/'
[color=],
a
[color=],
b]
# 1
为乘法,
2
为除法
[color=]def
[color=]yz
():
#
这是按钮的验证函数。
[color=]global
lista
[color=]if
ent1.get()==
[color=]str
(lista[
[color=]3
]):
ndsel()
lab1.config(
[color=]text
=
[color=]str
(lista[
[color=]0
])+lista[
[color=]1
]+
[color=]str
(lista[
[color=]2
])+
[color=]'='
)
lab2.config(
[color=]text
=
[color=]'
[color=]上一题解题正确,已更新题目
[color=]'
)
lab2.config(
[color=]fg
=
[color=]'green'
)
ent1.delete(
[color=]0
[color=],
END)
ent1.focus()
[color=]else
:
#
回答错误,清除文本框,标签
2
更新
ent1.delete(
[color=]0
[color=],
END)
ent1.focus()
lab2.config(
[color=]text
=
[color=]'
[color=]解答错误请重新输入
[color=]'
)
lab2.config(
[color=]fg
=
[color=]'red'
)
[color=]def
[color=]ndsel
():
#
单选按钮难度设定
[color=]global
lista
a=nd.get()
[color=]if
a ==
[color=]1
:
lista=jiajian()
lab1.config(
[color=]text
=
[color=]str
(lista[
[color=]0
]) + lista[
[color=]1
] +
[color=]str
(lista[
[color=]2
]) +
[color=]'='
)
lab2.config(
[color=]text
=
[color=]'
[color=]题目已变更
[color=]'
)
lab2.config(
[color=]fg
=
[color=]'green'
)
ent1.delete(
[color=]0
[color=],
END)
ent1.focus()
[color=]else
:
lista=cc(a-
[color=]1
)
lab1.config(
[color=]text
=
[color=]str
(lista[
[color=]0
]) + lista[
[color=]1
] +
[color=]str
(lista[
[color=]2
]) +
[color=]'='
)
lab2.config(
[color=]text
=
[color=]'
[color=]题目已变更
[color=]'
)
lab2.config(
[color=]fg
=
[color=]'green'
)
ent1.delete(
[color=]0
[color=],
END)
ent1.focus()
lista=jiajian()
root=Tk()
root.title(
[color=]"
[color=]小学加减乘除练习
[color=]"
)
root.geometry(
[color=]'600x400'
)
labf1=LabelFrame(root
[color=],
[color=]text
=
[color=]'
[color=]模式选择
[color=]'
[color=],
[color=]width
=
[color=]500
[color=],
[color=]height
=
[color=]150
)
labf1.pack()
nd=IntVar()
nd.set(
[color=]1
)
rb1=Radiobutton(labf1
[color=],
[color=]text
=
[color=]'
[color=]加减法运算
[color=]'
[color=],
[color=]variable
=nd
[color=],
[color=]value
=
[color=]1
[color=],
[color=]command
=ndsel)
rb2=Radiobutton(labf1
[color=],
[color=]text
=
[color=]'
[color=]十以内乘除法
[color=]'
[color=],
[color=]variable
=nd
[color=],
[color=]value
=
[color=]2
[color=],
[color=]command
=ndsel)
rb3=Radiobutton(labf1
[color=],
[color=]text
=
[color=]'
[color=]百以内乘除法
[color=]'
[color=],
[color=]variable
=nd
[color=],
[color=]value
=
[color=]3
[color=],
[color=]command
=ndsel)
rb4=Radiobutton(labf1
[color=],
[color=]text
=
[color=]'
[color=]千以内乘除法
[color=]'
[color=],
[color=]variable
=nd
[color=],
[color=]value
=
[color=]4
[color=],
[color=]command
=ndsel)
rb1.pack(
[color=]side
=LEFT)
rb2.pack(
[color=]side
=LEFT)
rb3.pack(
[color=]side
=LEFT)
rb4.pack(
[color=]side
=LEFT)
lab1=Label(root
[color=],
[color=]text
=
[color=]str
(lista[
[color=]0
])+lista[
[color=]1
]+
[color=]str
(lista[
[color=]2
])+
[color=]'='
[color=],
[color=]font
=(
[color=]'
[color=]宋体
[color=]'
[color=],
[color=]28
))
lab1.pack()
ent1=Entry(root
[color=],
[color=]font
=(
[color=]'
[color=]宋体
[color=]'
[color=],
[color=]28
)
[color=],
[color=]width
=
[color=]10
)
ent1.pack()
but1=Button(root
[color=],
[color=]text
=
[color=]'
[color=]确定
[color=]'
[color=],
[color=]font
=(
[color=]'
[color=]宋体
[color=]'
[color=],
[color=]28
)
[color=],
[color=]command
=yz)
but1.pack()
lab2=Label(root
[color=],
[color=]text
=
[color=]'
[color=]请在文本框内输入正确答案,点击确定键验算
[color=]'
[color=],
[color=]pady
=
[color=]5
)
lab2.pack()
'''f=open('a.txt','w',encoding='utf8')
for i in range(10000):
listb=jiajian()
[color=] f.write(str(listb[0])+listb[1]+str(listb[2])+'='+"
[color=]\t\t\t
")
f.close()'''
root.mainloop()