目录
(一)源码
(二)代码拆解
(1)导入库
(2)round模块
(3)uniform
(4)randint
(5)mainloop()组件
源码
老样子,先上源码
from random import *
from tkinter import *
calculation_window = Tk()
calculation_window.title('计算题生成器')
Sx = 0
def CXSC():
A_a = randint(10, 100)
A_b = randint(10, 100)
A_c = A_a
A_d = 100-A_b
A_e = str(A_a) + '×' + str(A_b) + '+' + str(A_c) + '×' + str(A_d)
label1 = Label(calculation_window, text = A_e,font=('微软雅黑',15))
B_a = round(uniform(10, 100), 2)
B_b = round(uniform(10, 100), 2)
B_c = B_a
B_d = round(100-B_b, 2)
B_e = str(B_a) + '×' + str(B_b) + '+' + str(B_c) + '×' + str(B_d)
label2 = Label(calculation_window, text = B_e,font=('微软雅黑',15))
C_a = round(uniform(10, 100), 2)
C_b = round(uniform(10, 100), 2)
C_c = C_a
C_d = round(100-C_b, 2)
C_e = str(C_a) + '×' + str(C_b) + '+' + str(C_c) + '×' + str(C_d)
label3 = Label(calculation_window, text = C_e,font=('微软雅黑',15))
D_a = round(uniform(10, 100), 2)
D_b = round(uniform(10, 130), 2)
D_c = D_a
D_d = randint(100, 130) - round(D_b, 2)
while D_d < 0:
D_b = round(uniform(10, 130), 2)
D_d = round(randint(100, 130) - round(D_b, 2))
D_e = round(D_b + D_c - 100, 2)
while D_e < 0:
D_b = round(uniform(10, 130), 2)
D_d = randint(100, 130) - round(D_b, 2)
if D_d < 0:
D_b = round(uniform(10, 130), 2)
D_d = round(randint(100, 130) - round(D_b, 2))
D_e = round(D_b + D_c - 100, 2)
D_f = D_a
D_g = str(round(D_a, 2)) + '×' + str(round(D_b, 2)) + '+' + str(round(D_c, 2)) + '×' +str(round(D_d, 2)) + '-' + str(round(D_f, 2)) + '×' + str(round(D_e, 2))
label4 = Label(calculation_window, text = D_g,font=('微软雅黑',15))
E_a = round(uniform(10, 100), 2)
E_b = round(uniform(10, 130), 2)
E_c = E_a
E_d = randint(100, 130) - round(E_b, 2)
while E_d < 0:
E_b = round(uniform(10, 130), 2)
E_d = round(randint(100, 130) - round(E_b, 2))
E_e = round(E_b + E_c - 100, 2)
while E_e < 0:
E_b = round(uniform(10, 130), 2)
E_d = randint(100, 130) - round(D_b, 2)
if E_d < 0:
E_b = round(uniform(10, 130), 2)
E_d = round(randint(100, 130) - round(E_b, 2))
E_e = round(E_b + E_c - 100, 2)
E_f = E_a
E_g = str(round(E_a, 2)) + '×' + str(round(E_b, 2)) + '+' + str(round(E_c, 2)) + '×' + str(round(E_d, 2)) + '-' + str(round(E_f, 2)) + '×' + str(round(E_e, 2))
label5 = Label(calculation_window, text = E_g,font=('微软雅黑',15))
F_a = randint(10,100)
F_b = 100 // F_a
while F_a * F_b != 100:
F_a = randint(10,100)
F_b = 100 // F_a
F_c = round(uniform(10,200),1)
F_d = str(F_c) + '÷' + str(F_a) + '÷' + str(F_b)
label6 = Label(calculation_window, text = F_d,font=('微软雅黑',15))
CXSC_button = Button(calculation_window, text="继续生成",command=CXSC)
G_a = randint(10,100)
G_b = 100 // G_a
while G_a * G_b != 100:
G_a = randint(10,100)
G_b = 100 // G_a
G_c = round(uniform(10,200),1)
G_d = str(G_c) + '÷' + str(G_a) + '÷' + str(G_b)
label7 = Label(calculation_window, text = G_d,font=('微软雅黑',15))
CXSC_button = Button(calculation_window, text="继续生成",command=CXSC)
H_a = round(uniform(10, 100), 2)
H_b = round(uniform(10, 130), 2)
H_c = H_a
H_d = randint(100, 130) - round(H_b, 2)
while H_d < 0:
H_b = round(uniform(10, 130), 2)
H_d = round(randint(100, 130) - round(H_b, 2))
H_e = round(H_b + H_c - 100, 2)
while H_e < 0:
H_b = round(uniform(10, 130), 2)
H_d = randint(100, 130) - round(D_b, 2)
if H_d < 0:
H_b = round(uniform(10, 130), 2)
H_d = round(randint(100, 130) - round(H_b, 2))
H_e = round(H_b + H_c - 100, 2)
H_f = H_a
H_g = str(round(H_a, 2)) + '×' + str(round(H_b, 2)) + '+' + str(round(H_c, 2)) + '×' + str(round(H_d, 2)) + '-' + str(round(H_f, 2)) + '×' + str(round(H_e, 2))
H_i = randint(10,100)
H_j = 100 // H_i
while H_i * H_j != 100:
H_i = randint(10,100)
H_j = 100 // H_i
H_k = round(uniform(10,200),1)
H_k = str(H_k) + '÷' + str(H_i) + '÷' + str(H_j)
H_l = H_g + '-' + H_k
label8 = Label(calculation_window, text = H_l,font=('微软雅黑',15))
I_a = round(uniform(10, 100), 2)
I_b = round(uniform(10, 130), 2)
I_c = I_a
I_d = randint(100, 130) - round(I_b, 2)
while I_d < 0:
I_b = round(uniform(10, 130), 2)
I_d = round(randint(100, 130) - round(I_b, 2))
I_e = round(I_b + I_c - 100, 2)
while I_e < 0:
I_b = round(uniform(10, 130), 2)
I_d = randint(100, 130) - round(D_b, 2)
if I_d < 0:
I_b = round(uniform(10, 130), 2)
I_d = round(randint(100, 130) - round(H_b, 2))
I_e = round(I_b + I_c - 100, 2)
I_f = I_a
I_g = str(round(I_a, 2)) + '×' + str(round(I_b, 2)) + '+' + str(round(I_c, 2)) + '×' + str(round(I_d, 2)) + '-' + str(round(I_f, 2)) + '×' + str(round(I_e, 2))
I_i = randint(10,100)
I_j = 100 // I_i
while I_i * I_j != 100:
I_i = randint(10,100)
I_j = 100 // I_i
I_k = round(uniform(10,200),1)
I_k = str(I_k) + '÷' + str(I_i) + '÷' + str(I_j)
I_l = I_g + '-' + I_k
label9 = Label(calculation_window, text = I_l,font=('微软雅黑',15))
J_a = round(uniform(10, 100), 2)
J_b = round(uniform(10, 130), 2)
J_c = J_a
J_d = randint(100, 130) - round(J_b, 2)
while J_d < 0:
J_b = round(uniform(10, 130), 2)
J_d = round(randint(100, 130) - round(J_b, 2))
J_e = round(J_b + J_c - 100, 2)
while J_e < 0:
J_b = round(uniform(10, 130), 2)
J_d = randint(100, 130) - round(J_b, 2)
if J_d < 0:
J_b = round(uniform(10, 130), 2)
J_d = round(randint(100, 130) - round(J_b, 2))
J_e = round(J_b + J_c - 100, 2)
J_f = J_a
J_g = str(round(J_a, 2)) + '×' + str(round(J_b, 2)) + '+' + str(round(J_c, 2)) + '×' + str(round(J_d, 2)) + '-' + str(round(J_f, 2)) + '×' + str(round(J_e, 2))
J_i = randint(10,100)
J_j = 100 // J_i
while J_i * J_j != 100:
J_i = randint(10,100)
J_j = 100 // J_i
J_k = round(uniform(10,200),1)
J_k = str(J_k) + '÷' + str(J_i) + '÷' + str(J_j)
J_l = J_g + '-' + J_k
label10 = Label(calculation_window, text = J_l,font=('微软雅黑',15))
label1.pack()
label2.pack()
label3.pack()
label4.pack()
label5.pack()
label6.pack()
label7.pack()
label8.pack()
label9.pack()
label10.pack()
A_a = randint(10, 100)
A_b = randint(10, 100)
A_c = A_a
A_d = 100-A_b
A_e = str(A_a) + '×' + str(A_b) + '+' + str(A_c) + '×' + str(A_d)
label1 = Label(calculation_window, text = A_e,font=('微软雅黑',15))
B_a = round(uniform(10, 100), 2)
B_b = round(uniform(10, 100), 2)
B_c = B_a
B_d = round(100-B_b, 2)
B_e = str(B_a) + '×' + str(B_b) + '+' + str(B_c) + '×' + str(B_d)
label2 = Label(calculation_window, text = B_e,font=('微软雅黑',15))
C_a = round(uniform(10, 100), 2)
C_b = round(uniform(10, 100), 2)
C_c = C_a
C_d = round(100-C_b, 2)
C_e = str(C_a) + '×' + str(C_b) + '+' + str(C_c) + '×' + str(C_d)
label3 = Label(calculation_window, text = C_e,font=('微软雅黑',15))
D_a = round(uniform(10, 100), 2)
D_b = round(uniform(10, 130), 2)
D_c = D_a
D_d = randint(100, 130) - round(D_b, 2)
while D_d < 0:
D_b = round(uniform(10, 130), 2)
D_d = round(randint(100, 130) - round(D_b, 2))
D_e = round(D_b + D_c - 100, 2)
while D_e < 0:
D_b = round(uniform(10, 130), 2)
D_d = randint(100, 130) - round(D_b, 2)
if D_d < 0:
D_b = round(uniform(10, 130), 2)
D_d = round(randint(100, 130) - round(D_b, 2))
D_e = round(D_b + D_c - 100, 2)
D_f = D_a
D_g = str(round(D_a, 2)) + '×' + str(round(D_b, 2)) + '+' + str(round(D_c, 2)) + '×' + str(round(D_d, 2)) + '-' + str(round(D_f, 2)) + '×' + str(round(D_e, 2))
label4 = Label(calculation_window, text = D_g,font=('微软雅黑',15))
E_a = round(uniform(10, 100), 2)
E_b = round(uniform(10, 130), 2)
E_c = E_a
E_d = randint(100, 130) - round(E_b, 2)
while E_d < 0:
E_b = round(uniform(10, 130), 2)
E_d = round(randint(100, 130) - round(E_b, 2))
E_e = round(E_b + E_c - 100, 2)
while E_e < 0:
E_b = round(uniform(10, 130), 2)
E_d = randint(100, 130) - round(D_b, 2)
if E_d < 0:
E_b = round(uniform(10, 130), 2)
E_d = round(randint(100, 130) - round(E_b, 2))
E_e = round(E_b + E_c - 100, 2)
E_f = E_a
E_g = str(round(E_a, 2)) + '×' + str(round(E_b, 2)) + '+' + str(round(E_c, 2)) + '×' + str(round(E_d, 2)) + '-' + str(round(E_f, 2)) + '×' + str(round(E_e, 2))
label5 = Label(calculation_window, text = E_g,font=('微软雅黑',15))
F_a = randint(10,100)
F_b = 100 // F_a
while F_a * F_b != 100:
F_a = randint(10,100)
F_b = 100 // F_a
F_c = round(uniform(10,200),1)
F_d = str(F_c) + '÷' + str(F_a) + '÷' + str(F_b)
label6 = Label(calculation_window, text = F_d,font=('微软雅黑',15))
CXSC_button = Button(calculation_window, text="继续生成",command=CXSC)
G_a = randint(10,100)
G_b = 100 // G_a
while G_a * G_b != 100:
G_a = randint(10,100)
G_b = 100 // G_a
G_c = round(uniform(10,200),1)
G_d = str(G_c) + '÷' + str(G_a) + '÷' + str(G_b)
label7 = Label(calculation_window, text = G_d,font=('微软雅黑',15))
CXSC_button = Button(calculation_window, text="继续生成",command=CXSC)
H_a = round(uniform(10, 100), 2)
H_b = round(uniform(10, 130), 2)
H_c = H_a
H_d = randint(100, 130) - round(H_b, 2)
while H_d < 0:
H_b = round(uniform(10, 130), 2)
H_d = round(randint(100, 130) - round(H_b, 2))
H_e = round(H_b + H_c - 100, 2)
while H_e < 0:
H_b = round(uniform(10, 130), 2)
H_d = randint(100, 130) - round(D_b, 2)
if H_d < 0:
H_b = round(uniform(10, 130), 2)
H_d = round(randint(100, 130) - round(H_b, 2))
H_e = round(H_b + H_c - 100, 2)
H_f = H_a
H_g = str(round(H_a, 2)) + '×' + str(round(H_b, 2)) + '+' + str(round(H_c, 2)) + '×' + str(round(H_d, 2)) + '-' + str(round(H_f, 2)) + '×' + str(round(H_e, 2))
H_i = randint(10,100)
H_j = 100 // H_i
while H_i * H_j != 100:
H_i = randint(10,100)
H_j = 100 // H_i
H_k = round(uniform(10,200),1)
H_k = str(H_k) + '÷' + str(H_i) + '÷' + str(H_j)
H_l = H_g + '-' + H_k
label8 = Label(calculation_window, text = H_l,font=('微软雅黑',15))
I_a = round(uniform(10, 100), 2)
I_b = round(uniform(10, 130), 2)
I_c = I_a
I_d = randint(100, 130) - round(I_b, 2)
while I_d < 0:
I_b = round(uniform(10, 130), 2)
I_d = round(randint(100, 130) - round(I_b, 2))
I_e = round(I_b + I_c - 100, 2)
while I_e < 0:
I_b = round(uniform(10, 130), 2)
I_d = randint(100, 130) - round(D_b, 2)
if I_d < 0:
I_b = round(uniform(10, 130), 2)
I_d = round(randint(100, 130) - round(H_b, 2))
I_e = round(I_b + I_c - 100, 2)
I_f = I_a
I_g = str(round(I_a, 2)) + '×' + str(round(I_b, 2)) + '+' + str(round(I_c, 2)) + '×' + str(round(I_d, 2)) + '-' + str(round(I_f, 2)) + '×' + str(round(I_e, 2))
I_i = randint(10,100)
I_j = 100 // I_i
while I_i * I_j != 100:
I_i = randint(10,100)
I_j = 100 // I_i
I_k = round(uniform(10,200),1)
I_k = str(I_k) + '÷' + str(I_i) + '÷' + str(I_j)
I_l = I_g + '-' + I_k
label9 = Label(calculation_window, text = I_l,font=('微软雅黑',15))
J_a = round(uniform(10, 100), 2)
J_b = round(uniform(10, 130), 2)
J_c = J_a
J_d = randint(100, 130) - round(J_b, 2)
while J_d < 0:
J_b = round(uniform(10, 130), 2)
J_d = round(randint(100, 130) - round(J_b, 2))
J_e = round(J_b + J_c - 100, 2)
while J_e < 0:
J_b = round(uniform(10, 130), 2)
J_d = randint(100, 130) - round(J_b, 2)
if J_d < 0:
J_b = round(uniform(10, 130), 2)
J_d = round(randint(100, 130) - round(J_b, 2))
J_e = round(J_b + J_c - 100, 2)
J_f = J_a
J_g = str(round(J_a, 2)) + '×' + str(round(J_b, 2)) + '+' + str(round(J_c, 2)) + '×' + str(round(J_d, 2)) + '-' + str(round(J_f, 2)) + '×' + str(round(J_e, 2))
J_i = randint(10,100)
J_j = 100 // J_i
while J_i * J_j != 100:
J_i = randint(10,100)
J_j = 100 // J_i
J_k = round(uniform(10,200),1)
J_k = str(J_k) + '÷' + str(J_i) + '÷' + str(J_j)
J_l = J_g + '-' + J_k
label10 = Label(calculation_window, text = J_l,font=('微软雅黑',15))
CXSC_button = Button(calculation_window, text="继续生成",command=CXSC)
CXSC_button.pack()
label1.pack()
label2.pack()
label3.pack()
label4.pack()
label5.pack()
label6.pack()
label7.pack()
label8.pack()
label9.pack()
label10.pack()
calculation_window.mainloop()
拆解代码
from random import *
from tkinter import *
tkinter与random这两个都是python的内置库,tkinter一般用来写GUI程序,random是一个用来生产随机数的库。
round(uniform(10, 100), 2)
round是python的一个命令,其作用是给一个小数四舍五入,保留指定的位数。如“round(N, 3)”表示对N保留3位小数。
uniform(10, 100)
uniform是random的指令,其作用是生产一个指定范围的浮点数。
randint()
与uniform一样,它是random库的指令,但作用是生产一个指定范围的整数。
calculation_window.mainloop()
mainloop()是tkinter的消息循环组件,也是必须组件。