更新UI,以及根据文档修改代码

master
bettleChen 2 years ago
parent d83179620e
commit 10663b57f8

@ -0,0 +1,11 @@
import numpy as np
def x1_1(): # 随机给出1000个点坐标,赋值给xyMap,<class 'numpy.ndarray'>
shape = [1000, 2]
xyMap = np.random.randint(-200, 200, size=shape)
return xyMap
if __name__ == '__main__':
print(x1_1())

@ -0,0 +1,27 @@
import numpy as np
def x1_2(problemIndex): # 程序给出任一函数 y=f(x)计算x=1到1000的y值赋值给xyMap
# shape = [1000,2] #x值为1到1000y为其对应函数值
xyMap = np.random.rand(1000, 2)
if problemIndex == 1:
for x in range(1, 1001):
xyMap[x - 1][0] = x # xyMap序号为0-999
xyMap[x - 1][1] = np.sin(x)
return xyMap
elif problemIndex == 2:
for x in range(1, 1001):
xyMap[x - 1][0] = x
xyMap[x - 1][1] = np.cos(x)
return xyMap
elif problemIndex == 3:
for x in range(1, 1001):
xyMap[x - 1][0] = x
xyMap[x - 1][1] = np.tan(x)
return xyMap
elif problemIndex == 4:
for x in range(1, 1001):
xyMap[x - 1][0] = x
xyMap[x - 1][1] = x * 2 + 2
return xyMap
if __name__ == '__main__':
print(x1_2(4)[:50])

@ -0,0 +1,43 @@
import numpy as np
import math
def x1_3(problemIndex): # 程序给出任一函数y=f(x)计算x=-500到+500的y值赋值给xyMap
c = 0 # 此处x赋值为-500到-1到1到500到1000个
shape = [1000, 2] # y赋值为其对应的函数
xyMap = np.random.rand(1000, 2)
if problemIndex == 1:
for x in range(-500, 500):
if (x == 0):
continue
xyMap[c][0] = x
xyMap[c][1] = math.sin(x)
c += 1
return xyMap
elif problemIndex == 2:
for x in range(-500, 500):
if (x == 0):
continue
xyMap[c][0] = x
xyMap[c][1] = math.cos(x)
c += 1
return xyMap
elif problemIndex == 3:
for x in range(-500, 500):
if (x == 0):
continue
xyMap[c][0] = x
xyMap[c][1] = math.tan(x)
c += 1
return xyMap
elif problemIndex == 4:
for x in range(-500, 501):
if (x == 0):
continue
xyMap[c][0] = x
xyMap[c][1] = x * 2 + 2
c += 1
return xyMap
if __name__ == '__main__':
print(x1_3(1))

@ -0,0 +1,31 @@
import numpy as np
import math
def x1_4(ProblemIndex):
step = 3 # 步长
y = []
i, c = 0, 0
number = 1000 # 个数
while (True):
if (c == number):
break
else:
y.append(i)
i += step
c += 1
y = np.array(y)
xyMap = np.random.rand(1000, 2)
if (ProblemIndex == 1):
for i in range(len(y)):
xyMap[i][0] = y[i]
xyMap[i][1] = 2 * y[i] + 1
return xyMap
elif (ProblemIndex == 2):
for i in range(len(y)):
xyMap[i][0] = y[i]
xyMap[i][1] = y[i] / 3
return xyMap
if __name__ == '__main__':
print(x1_4(1))

@ -1,7 +1,7 @@
import math
from collections import OrderedDict
from time import sleep
from tkinter import *
from ttkbootstrap import *
import tkinter.font as tkFont
from tkinter import messagebox
from tkinter.ttk import Treeview
@ -46,8 +46,8 @@ class Graph(Canvas):
绘制坐标轴
"""
self.delete("all")
self.create_line(0, self.origin[1], self.width, self.origin[1], fill='white', arrow=LAST) # 一象限xy轴
self.create_line(self.origin[0], 0, self.origin[0], self.height, fill='white', arrow=FIRST)
self.create_line(0, self.origin[1], self.width, self.origin[1], fill='black', arrow=LAST) # 一象限xy轴
self.create_line(self.origin[0], 0, self.origin[0], self.height, fill='black', arrow=FIRST)
def draw_scale(self):
"""
@ -256,32 +256,32 @@ class FunctionDisplay(Frame):
def create_form(self):
bottom_frame = Frame(self.master, width=self.attr["width"], height=self.attr["height"] * 0.3)
self.func_input = Entry(bottom_frame, font=self.font_style, bg="#c0d9d9", width=40)
self.func_input = Entry(bottom_frame, font=self.font_style, width=40)
self.func_input.grid(row=0, column=0, columnspan=4, padx=30)
self.add_func_button = Button(bottom_frame, text="用户命名并新增基本函数", command=self.add_function, font=("Lucida Grande", 20))
self.add_func_button = Button(bottom_frame, text="用户命名并新增基本函数", command=self.add_function)
self.add_func_button.grid(row=0, column=4, padx=10)
Label(bottom_frame, text="x步长", font=("Lucida Grande", 20)).grid(row=1, column=0, padx=30)
self.x_step = Entry(bottom_frame, font=self.font_style, bg="#c0d9d9", width=10)
self.x_step = Entry(bottom_frame, font=self.font_style, width=10)
self.x_step.grid(row=1, column=1, padx=10, pady=10)
Label(bottom_frame, text="x个数", font=("Lucida Grande", 20)).grid(row=1, column=2, padx=30)
self.x_count = Entry(bottom_frame, font=self.font_style, bg="#c0d9d9", width=10)
self.x_count = Entry(bottom_frame, font=self.font_style, width=10)
self.x_count.grid(row=1, column=3, padx=10, pady=10)
Label(bottom_frame, text="坐标轴", font=("Lucida Grande", 20)).grid(row=2, column=0, padx=30)
self.quadrant = IntVar()
self.quadrant.set(4)
Radiobutton(bottom_frame, text="四象限", font=("Lucida Grande", 20), indicatoron=False, command=self.update_quadrant, value=4, variable=self.quadrant).grid(row=2, column=1, padx=10, pady=10)
Radiobutton(bottom_frame, text="一象限", font=("Lucida Grande", 20), indicatoron=False, command=self.update_quadrant, value=1, variable=self.quadrant).grid(row=2, column=2, padx=10, pady=10)
Radiobutton(bottom_frame, text="四象限", command=self.update_quadrant, value=4, variable=self.quadrant).grid(row=2, column=1, padx=10, pady=10)
Radiobutton(bottom_frame, text="一象限", command=self.update_quadrant, value=1, variable=self.quadrant).grid(row=2, column=2, padx=10, pady=10)
Label(bottom_frame, text="x放大倍数", font=("Lucida Grande", 20)).grid(row=3, column=0, padx=30)
self.x_scale = Entry(bottom_frame, font=self.font_style, bg="#c0d9d9", width=10)
self.x_scale = Entry(bottom_frame, font=self.font_style, width=10)
self.x_scale.grid(row=3, column=1, padx=10, pady=10)
Label(bottom_frame, text="y放大倍数", font=("Lucida Grande", 20)).grid(row=3, column=2, padx=30)
self.y_scale = Entry(bottom_frame, font=self.font_style, bg="#c0d9d9", width=10)
self.y_scale = Entry(bottom_frame, font=self.font_style, width=10)
self.y_scale.grid(row=3, column=3, padx=10, pady=10)
Button(bottom_frame, text="输出", font=("Lucida Grande", 20),
command=self.print_function, height=3, width=5).grid(row=1, rowspan=3, column=4, sticky="w")
Button(bottom_frame, text="输出",
command=self.print_function, width=5).grid(row=1, rowspan=3, column=4, sticky="w")
bottom_frame.place(x=0, y=self.attr["height"] * 0.65 + 20)
def show_user_function(self):
@ -345,7 +345,7 @@ class FunctionDisplay(Frame):
if __name__ == '__main__':
root = Tk()
root = Window()
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
root_attr = {
@ -357,4 +357,6 @@ if __name__ == '__main__':
root.geometry(alignstr)
root.resizable(width=False, height=False)
app = FunctionDisplay(root, root_attr)
ttk.Style().configure("TButton", font="-size 18")
ttk.Style().configure("TRadiobutton", font="-size 18")
root.mainloop()

@ -17,9 +17,9 @@ def trapezoidal_rule(a, b, n):
integral = (h / 2) * (y[0] + 2 * sum(y[1:n]) + y[n]) # 梯形法计算积分值
return integral
a = 0 # 积分区间的起始点
b = 1 # 积分区间的结束点
n = 100 # 划分的子区间数
integral = trapezoidal_rule(a, b, n)
print("数值积分的近似值:", integral)
if __name__ == '__main__':
a = 0 # 积分区间的起始点
b = 1 # 积分区间的结束点
n = 100 # 划分的子区间数
integral = trapezoidal_rule(a, b, n)
print("数值积分的近似值:", integral)

@ -17,8 +17,9 @@ def central_difference(f, x, h):
def f(x):
return x**2
x = 2 # 求导点的横坐标
h = 0.01 # 步长
if __name__ == '__main__':
x = 2 # 求导点的横坐标
h = 0.01 # 步长
df = central_difference(f, x, h)
print("导数的近似值:", df)
df = central_difference(f, x, h)
print("导数的近似值:", df)

@ -32,6 +32,8 @@ def f(x):
def f_prime(x):
return 2 * x
x0 = 1 # 初始猜测值
solution = newton_method(f, f_prime, x0)
print("解的近似值:", solution)
if __name__ == '__main__':
x0 = 1 # 初始猜测值
solution = newton_method(f, f_prime, x0)
print("解的近似值:", solution)

@ -1,7 +1,7 @@
import math
from collections import OrderedDict
from time import sleep
from tkinter import *
from ttkbootstrap import *
import tkinter.font as tkFont
from tkinter import messagebox
from tkinter.ttk import Treeview
@ -46,8 +46,8 @@ class Graph(Canvas):
绘制坐标轴
"""
self.delete("all")
self.create_line(0, self.origin[1], self.width, self.origin[1], fill='white', arrow=LAST) # 一象限xy轴
self.create_line(self.origin[0], 0, self.origin[0], self.height, fill='white', arrow=FIRST)
self.create_line(0, self.origin[1], self.width, self.origin[1], fill='black', arrow=LAST) # 一象限xy轴
self.create_line(self.origin[0], 0, self.origin[0], self.height, fill='black', arrow=FIRST)
def draw_scale(self):
"""
@ -76,6 +76,9 @@ class Graph(Canvas):
self.origin = (self.width / 2, self.height / 2)
self.draw_axis()
self.draw_scale()
def get_xy(self):
return -math.ceil((self.origin[0] / self.bili_x)) + 1, math.ceil((self.width - self.origin[0]) / self.bili_x)
def draw_graph(self, func, draw_precision=0.1, count=1000, bili_x=20, bili_y=40, c='blue'):
'xmin,xmax 自变量的取值范围; c 图像颜色'
@ -84,7 +87,7 @@ class Graph(Canvas):
self.draw_axis()
self.draw_scale()
w1, w2 = self.bili_x, self.bili_y # w1,w2为自变量和函数值在横纵轴上的放大倍数
xmin, xmax = -math.ceil((self.origin[0] / self.bili_x)) + 1, math.ceil((self.width - self.origin[0]) / self.bili_x)
xmin, xmax = self.get_xy()
co2 = []
try:
for x in arange(xmin, xmax, draw_precision): # draw_precision----画图精度
@ -271,32 +274,32 @@ class FunctionDisplay(Frame):
def create_form(self):
bottom_frame = Frame(self.master, width=self.attr["width"], height=self.attr["height"] * 0.3)
self.func_input = Entry(bottom_frame, font=self.font_style, bg="#c0d9d9", width=40)
self.func_input = Entry(bottom_frame, font=self.font_style, width=40)
self.func_input.grid(row=0, column=0, columnspan=4, padx=30)
self.add_func_button = Button(bottom_frame, text="用户命名并新增基本函数", command=self.add_function, font=("Lucida Grande", 20))
self.add_func_button = Button(bottom_frame, text="用户命名并新增基本函数", command=self.add_function)
self.add_func_button.grid(row=0, column=4, padx=10)
Label(bottom_frame, text="x步长", font=("Lucida Grande", 20)).grid(row=1, column=0, padx=30)
self.x_step = Entry(bottom_frame, font=self.font_style, bg="#c0d9d9", width=10)
self.x_step = Entry(bottom_frame, font=self.font_style, width=10)
self.x_step.grid(row=1, column=1, padx=10, pady=10)
Label(bottom_frame, text="x个数", font=("Lucida Grande", 20)).grid(row=1, column=2, padx=30)
self.x_count = Entry(bottom_frame, font=self.font_style, bg="#c0d9d9", width=10)
self.x_count = Entry(bottom_frame, font=self.font_style, width=10)
self.x_count.grid(row=1, column=3, padx=10, pady=10)
Label(bottom_frame, text="坐标轴", font=("Lucida Grande", 20)).grid(row=2, column=0, padx=30)
self.quadrant = IntVar()
self.quadrant.set(4)
Radiobutton(bottom_frame, text="四象限", font=("Lucida Grande", 20), indicatoron=False, command=self.update_quadrant, value=4, variable=self.quadrant).grid(row=2, column=1, padx=10, pady=10)
Radiobutton(bottom_frame, text="一象限", font=("Lucida Grande", 20), indicatoron=False, command=self.update_quadrant, value=1, variable=self.quadrant).grid(row=2, column=2, padx=10, pady=10)
Radiobutton(bottom_frame, text="四象限", command=self.update_quadrant, value=4, variable=self.quadrant).grid(row=2, column=1, padx=10, pady=10)
Radiobutton(bottom_frame, text="一象限", command=self.update_quadrant, value=1, variable=self.quadrant).grid(row=2, column=2, padx=10, pady=10)
Label(bottom_frame, text="x放大倍数", font=("Lucida Grande", 20)).grid(row=3, column=0, padx=30)
self.x_scale = Entry(bottom_frame, font=self.font_style, bg="#c0d9d9", width=10)
self.x_scale = Entry(bottom_frame, font=self.font_style, width=10)
self.x_scale.grid(row=3, column=1, padx=10, pady=10)
Label(bottom_frame, text="y放大倍数", font=("Lucida Grande", 20)).grid(row=3, column=2, padx=30)
self.y_scale = Entry(bottom_frame, font=self.font_style, bg="#c0d9d9", width=10)
self.y_scale = Entry(bottom_frame, font=self.font_style, width=10)
self.y_scale.grid(row=3, column=3, padx=10, pady=10)
Button(bottom_frame, text="输出", font=("Lucida Grande", 20),
command=self.print_function, height=3, width=5).grid(row=1, rowspan=3, column=4, sticky="w")
Button(bottom_frame, text="输出",
command=self.print_function, width=5).grid(row=1, rowspan=3, column=4, sticky="w")
bottom_frame.place(x=0, y=self.attr["height"] * 0.65 + 20)
def show_user_function(self):
@ -380,7 +383,7 @@ class FunctionDisplay(Frame):
if __name__ == '__main__':
root = Tk()
root = Window()
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
root_attr = {
@ -392,4 +395,6 @@ if __name__ == '__main__':
root.geometry(alignstr)
root.resizable(width=False, height=False)
app = FunctionDisplay(root, root_attr)
ttk.Style().configure("TButton", font="-size 18")
ttk.Style().configure("TRadiobutton", font="-size 18")
root.mainloop()

@ -1,9 +1,7 @@
import math
from time import sleep
from ttkbootstrap import *
import tkinter.font as tkFont
from tkinter import messagebox
from tkinter.ttk import Treeview
import numpy as np
from matplotlib.figure import Figure
@ -28,7 +26,6 @@ class Graph(Canvas):
"""
绘制函数图形
"""
def __init__(self, master=None, **kwargs):
super().__init__(master, **kwargs)
self.width = int(self.cget('width'))
@ -40,7 +37,6 @@ class Graph(Canvas):
self.draw_scale()
self.fig = FigureCanvasTkAgg()
def draw_axis(self):
"""
绘制坐标轴

@ -1 +1 @@
{"xy": "x*y", "f2": "sin(x) * cos(x)", "2sinx": "2*sin(x)", "2cosx": "2*cos(x)"}
{"xy": "x*y", "f2": "sin(x) * cos(x)", "2sinx": "2*sin(x)", "2cosx": "2*cos(x)", "2xy": "2*x*y", "SinPlusCos(x)": "sin(x) + cos(x)"}
Loading…
Cancel
Save