创建标出横纵坐标值列表的代码
x_ticks_list = np.linspace(-2*np.pi, 2*np.pi, 16)
x_ticks_list = [float(round(i, 2)) for i in x_ticks_list]
x_ticks_list_set = []
for i in x_ticks_list:
x_ticks_list_set.append(f'${i}$')
y_ticks_list = np.linspace(-10, 10, 21)
y_ticks_list = [float(round(i, 2)) for i in y_ticks_list]
y_ticks_list_set = []
for i in y_ticks_list:
y_ticks_list_set.append(f'${i}$')
GUI设计的代码
class Mainpage_draw_function(tk.Frame):
def __init__(self, master, width, height, **kwargs):
self.root = master # 使用master作为父控件的引用
self.width = width
self.height = height
super().__init__(self.root) # 调用父类构造函数
# 设置框架的大小(可选,因为宽度和高度通常由布局管理器控制)
# 但如果确实需要固定大小,可以使用下面的方法
# 注意:这通常不是推荐的做法,因为它会使界面不够灵活
# self.config(width=self.width, height=self.height)
# 初始化UI(包括控件的创建和布局)
self.main_main()
# 主页面设计
def main_main(self):
# 画布框架
self.canvas_frame = tk.Frame(self, width=self.width, height=400, bd=2, relief="solid")
self.canvas_frame.pack(anchor="center")
self.canvas_frame.grid_propagate(0)
# 实现画图的画布
# 创建一个matplotlib的Figure对象
self.fig = Figure(figsize=(self.width, 4), dpi=100)
# 创建一个subplot
self.ax = self.fig.add_subplot(111)
# 创建一个FigureCanvasTkAgg对象,这是一个tkinter的widget
self.canvas = FigureCanvasTkAgg(self.fig, master=self.canvas_frame)
# 将widget添加到画布框架中
self.widget = self.canvas.get_tk_widget()
self.widget.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
# 设计横纵坐标值
# self.ax.set_xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi])
# self.ax.set_xticklabels([r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
# self.ax.set_yticks([-1, 0, +1])
# self.ax.set_yticklabels([r'$-1$', r'$0$', r'$+1$'])
self.ax.set_xticks(x_ticks_list)
self.ax.set_xticklabels(x_ticks_list_set)
self.ax.set_yticks(y_ticks_list)
self.ax.set_yticklabels(y_ticks_list_set)
# 获取输入控制图像的数值输入框frame
self.entry_value_frame = tk.Frame(self, width=self.width, height=50, bd=1, relief="solid")
self.entry_value_frame.pack(anchor="center", pady=10)
self.entry_value_frame.pack_propagate(0)
# 控制获取输入控制图像的数值输入框居中显示的框架
self.entry_value_frame_litle = tk.Frame(self.entry_value_frame)
self.entry_value_frame_litle.pack(anchor="center", pady=10)
self.entry_value_label = tk.Label(self.entry_value_frame_litle, width=(self.width//2), height=25)
self.entry_value_label.pack()
# 获取输入控制图像的数值输入框
self.entry_value = tk.Entry(self.entry_value_label)
self.entry_value.pack()
# 存放按钮标签内容的列表
self.control_button_title = ["sinx图像", "cosx图像", "tanx图像"]
#存放按钮不同id的字典
self.control_draw_sinx_button = {}
# 控制按钮框架
self.control_button_frame = tk.Frame(self, width=self.width, height=100, bd=1, relief="solid")
self.control_button_frame.pack(anchor="center", pady=10)
self.control_button_frame.pack_propagate(0)
# 控制按钮框架内集体控制按钮居中显示的框架
self.control_button_frame_litle = tk.Frame(self.control_button_frame)
self.control_button_frame_litle.pack(anchor="center", pady=10)
#在控制按钮框架中添加按钮组件
for num, i in enumerate(self.control_button_title):
self.control_draw_sinx_button[i] = tk.Button(self.control_button_frame_litle, width=15, height=2, text=i)
# self.control_draw_sinx_button[i].config(activebackground="green", activeforeground="white", command=lambda i=i :self.draw_function(fun_need=i))
self.control_draw_sinx_button[i].config(activebackground="green", activeforeground="white", command=lambda i=i :self.show_cau_type(c_value=i))
self.control_draw_sinx_button[i].grid(column=int(num), row=0, padx=4)
实现人机交互界面的显示获取用户输入要画入的三角函数参数的子页面
def show_cau_type(self, c_value):
self.entry_cau_message = tk.Toplevel(self.root)
self.entry_cau_message.title("输入函数参数")
# 获取屏幕的宽度和高度
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
# 计算窗口左上角的位置,使其居中
x = (screen_width // 2) - (self.width // 4)
y = (screen_height // 2) - (self.height // 4)
#实例化设计窗口大小和显示位置
self.entry_cau_message.geometry(f"{self.width//2}x{self.height//2}+{int(x)}+{int(y)}")
#控制内容居中显示的框
get_user_e = tk.Frame(self.entry_cau_message, width=(self.width // 2), height=(self.height // 2), bd=2, relief="solid")
get_user_e.pack(fill="both", expand=True)
#所用函数内容存放列表
c_value_dict = {"sinx图像": "sin", "cosx图像": "cos", "tanx图像": "tan"}
# 获取要画函数的系数
self.coefficient = tk.Entry(get_user_e, width=10)
tri_f = tk.Label(get_user_e, text=f"{c_value_dict[c_value]}(x+")
# 获取变量
self.variable = tk.Entry(get_user_e)
tri_f1 = tk.Label(get_user_e, text=")+")
# 获取常数
self.constant = tk.Entry(get_user_e)
#布局模块
mo_list = [self.coefficient, tri_f, self.variable, tri_f1, self.constant]
for num, v in enumerate(mo_list):
v.grid(column=int(num), row=0, padx=4, columnspan=1, rowspan=1)
# 控制画出数学函数按钮
start_draw = tk.Button(get_user_e, text="输入完成", command=lambda x=c_value: self.draw_function(x))
start_draw.grid(column=0, row=1, padx=4, columnspan=1, rowspan=1)
实现在画布中画图的自定义函数
def draw_function(self, fun_need):
# 存放获取的输入内容列表
print("获取用户输入的内容")
print(self.coefficient.get(), self.variable.get(), self.constant.get())
try:
self.function_uesr_entry_list = [int(self.coefficient.get()), int(self.variable.get()), int(self.constant.get())]
except:
self.function_uesr_entry_list = [1, 0, 0]
#删除self.entry_cau_message窗口
self.entry_cau_message.destroy()
# 清除当前轴的内容
self.ax.clear()
# 创建一个subplot
math_value = self.entry_value.get()
print(f"ok{fun_need}and{math_value}")
# 存放可以绘画的图像的字典
x, y = view.show_x_list(fun_need, self.function_uesr_entry_list)
# 设计横纵坐标值
self.ax.set_xticks(x_ticks_list)
self.ax.set_xticklabels(x_ticks_list_set)
self.ax.set_yticks(y_ticks_list)
self.ax.set_yticklabels(y_ticks_list_set)
# 判断并且画出对应的图像
can_draw_dict = {"sinx图像": np.sin(x), "cosx图像": np.cos(x), "tanx图像": np.tan(x)}
for cau in can_draw_dict.keys():
print(f"ok1{cau == fun_need}")
if fun_need == cau:
print(f"ok2{fun_need}")
self.ax.plot(x, y)
self.canvas.draw()
return 0
view.py文件实现功能的函数
# 返回图像对应Y轴数值列表
def show_x_list(judge, user_entry_list):
space = 256
# 所有x坐标值
x = np.linspace(-2 * np.pi, 2 * np.pi, 1000)
#所有y坐标值
can_draw_dict = {"sinx图像": np.sin(x+user_entry_list[1])*user_entry_list[0]+user_entry_list[2], "cosx图像": np.cos(x+user_entry_list[1])*user_entry_list[0]+user_entry_list[2], "tanx图像": np.tan(x+user_entry_list[1])*user_entry_list[0]+user_entry_list[2]}
print(can_draw_dict.keys())
y = can_draw_dict[judge]
y = np.where(np.abs(y) < 10, y, np.nan)
# # 将数组中的值改为保留两位小数
# for n, num in enumerate(x):
# # 注意:这里我们直接修改了x数组
# x[:] = np.round(x, decimals=2)
return x, y
小结
应用还在开发阶段,后面会继续更新新的界面设计和内容,感谢支持。