小学一年级 100以内加减
本帖最后由 dahai1983 于 2024-8-12 17:06 编辑写个小学加减法计算生成,先记录一下,有时间再完善封装
第一次用tk,做界面
import random
from tkinter.filedialog import askopenfilename
from tkinter.constants import BOTH, END
from tkinter importttk
import xlsxwriter
import tkinter as tk
from pathlib import Path
class Win(tk.Tk):
def __init__(self):
super().__init__()
self.geometry("430x300+350+250")
self.title('小学生数学题生成器')
label = tk.Label(self, text="运算符号:")
label.place(x=0, y=12)
self.var_add = tk.IntVar(value=1)
self.var_sub = tk.IntVar(value=1)
self.var_mul = tk.IntVar()
self.var_div = tk.IntVar()
self.checkbox_add = tk.Checkbutton(self, text="加法", variable=self.var_add)
self.checkbox_sub = tk.Checkbutton(self, text="减法", variable=self.var_sub)
self.checkbox_mul = tk.Checkbutton(self, text="乘法", variable=self.var_mul)
self.checkbox_div = tk.Checkbutton(self, text="除法", variable=self.var_div)
self.checkbox_add.place(x=60, y=10)
self.checkbox_sub.place(x=120, y=10)
self.checkbox_mul.place(x=180, y=10)
self.checkbox_div.place(x=240, y=10)
label2 = tk.Label(self, text="数字范围:")
label2.place(x=0, y=32)
label3 = tk.Label(self, text="——")
label3.place(x=86, y=31)
self.entry_min = tk.Entry(self, width=5)
self.entry_min.insert(0, '0')
self.entry_max = tk.Entry(self, width=5)
self.entry_max.insert(0, '100')
self.entry_min.place(x=60, y=32)
self.entry_max.place(x=120, y=32)
label4 = tk.Label(self, text="出题数量:")
label4.place(x=0, y=55)
self.entry_sum = tk.Entry(self, width=5)
self.entry_sum.insert(0, '30')
self.entry_sum.place(x=60, y=55)
btn = tk.Button(self, text="开始出题", command=self.showtable)
btn_out = tk.Button(self, text="保存到文件", command=self.outexcel)
btn.place(x=320, y=10)
btn_out.place(x=320, y=40)
label4 = tk.Label(self, text="输出目录:")
label4.place(x=0, y=80)
path = Path(__file__).parent.resolve()
self.entry_path = tk.Entry(self, width=35)
self.entry_path.insert(0, str(path) + '\out.xls')
self.entry_path.place(x=60, y=80)
btn_path = tk.Button(self, text="重选目录", command=self.chanpath)
btn_path.place(x=320, y=75)
def chanpath(self):
self.entry_path.delete(0, 'end')
file = tk.filedialog.asksaveasfilename()
self.entry_path.insert(0, file)
pass
def outexcel(self):
xls = xlsxwriter.Workbook(self.entry_path.get())
bold = xls.add_format({'bold': 0, "color": "black", "font_size": 15})
worksheet = xls.add_worksheet('Sheet1')
for i in range(len(self.lst_out)):
worksheet.write_string('A' + str(i+1), self.lst_out, bold)
xls.close()
pass
def generate_arithmetic(self, var, min_max):
list = []
if var[0] == 1:
list.append('+')
if var[1] == 1:
list.append('-')
if var[2] == 1:
list.append('x')
if var[3] == 1:
list.append('÷')
op = random.choice(list)
num1 = random.randint(int(min_max[0]), int(min_max[1]))
num2 = random.randint(int(min_max[0]), int(min_max[1]))
question = f"{num1} {op} {num2} ="
return question
def getvar(self):
var = (self.var_add.get(), self.var_sub.get(), self.var_mul.get(), self.var_div.get())
range = (self.entry_min.get(), self.entry_max.get())
return var, self.entry_sum.get(), range
pass
def showtable(self):
var, sum, min_max = self.getvar()
self.lst_out = []
for i in range(int(sum)):
ques = self.generate_arithmetic(var, min_max)
self.lst_out.append(ques)
pass
scrollbar_y = tk.Scrollbar(self, orient="vertical")
treeview = ttk.Treeview(self, columns=("Column 1", "Column 2", "Column 3", "Column 4"), show="headings",
yscrollcommand=scrollbar_y.set)
scrollbar_y.config(command=treeview.yview)
treeview.column("Column 1", width=60)
treeview.column("Column 2", width=60)
treeview.column("Column 3", width=60)
treeview.column("Column 4", width=60)
for item in self.lst_out:
treeview.insert('', END, values=item)
treeview.place(x=10, y=120)
scrollbar_y.pack(side="right", fill="y")
pass
def main():
win = Win()
win.mainloop()
if __name__ == '__main__':
main()
试用下,谢谢分享 这可以,谢谢楼主。 有用,等着楼主完善 这可以,谢谢楼主。
谢谢楼主,正需要 感谢楼主 谢谢楼主。 生成了好多结果大于100的题并且写入到表格里了
楼主加油