Update work.py
Add button + -
This commit is contained in:
parent
af62c1eed7
commit
7a729b728d
108
work.py
108
work.py
@ -3,10 +3,21 @@ from tkinter import filedialog, messagebox
|
|||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from scipy.interpolate import make_interp_spline, BSpline
|
from scipy.interpolate import make_interp_spline
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import io
|
import io
|
||||||
|
|
||||||
|
# 更新输入框值的函数,支持整型和浮点型
|
||||||
|
def update_entry_value(entry, delta, is_integer=False):
|
||||||
|
try:
|
||||||
|
value = float(entry.get()) + delta
|
||||||
|
if is_integer:
|
||||||
|
value = int(round(value)) # 确保为整数
|
||||||
|
entry.delete(0, tk.END)
|
||||||
|
entry.insert(0, str(value))
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
def generate_and_display_shape():
|
def generate_and_display_shape():
|
||||||
try:
|
try:
|
||||||
min_vertices = int(min_entry.get())
|
min_vertices = int(min_entry.get())
|
||||||
@ -18,28 +29,19 @@ def generate_and_display_shape():
|
|||||||
messagebox.showerror("无效输入", "最小和最大顶点数必须至少为3,并且最小值不应超过最大值。")
|
messagebox.showerror("无效输入", "最小和最大顶点数必须至少为3,并且最小值不应超过最大值。")
|
||||||
return
|
return
|
||||||
|
|
||||||
# 清除之前的图形
|
|
||||||
ax.clear()
|
ax.clear()
|
||||||
|
|
||||||
# 生成随机顶点数量
|
|
||||||
num_vertices = np.random.randint(min_vertices, max_vertices + 1)
|
num_vertices = np.random.randint(min_vertices, max_vertices + 1)
|
||||||
|
|
||||||
# 生成随机角度
|
|
||||||
angles = np.sort(np.random.rand(num_vertices) * 2 * np.pi)
|
angles = np.sort(np.random.rand(num_vertices) * 2 * np.pi)
|
||||||
|
|
||||||
# 添加散布
|
|
||||||
angles += np.random.normal(0, scatter, num_vertices)
|
angles += np.random.normal(0, scatter, num_vertices)
|
||||||
angles = np.sort(angles % (2 * np.pi))
|
angles = np.sort(angles % (2 * np.pi))
|
||||||
|
|
||||||
# 计算每个顶点的坐标
|
|
||||||
x = np.cos(angles)
|
x = np.cos(angles)
|
||||||
y = np.sin(angles)
|
y = np.sin(angles)
|
||||||
|
|
||||||
# 将第一个顶点添加到最后以闭合图形
|
|
||||||
x = np.append(x, x[0])
|
x = np.append(x, x[0])
|
||||||
y = np.append(y, y[0])
|
y = np.append(y, y[0])
|
||||||
|
|
||||||
# 使用B样条插值平滑曲线
|
|
||||||
t = np.linspace(0, 1, num_vertices + 1)
|
t = np.linspace(0, 1, num_vertices + 1)
|
||||||
t_smooth = np.linspace(0, 1, 300)
|
t_smooth = np.linspace(0, 1, 300)
|
||||||
|
|
||||||
@ -49,13 +51,12 @@ def generate_and_display_shape():
|
|||||||
x_smooth = spl_x(t_smooth)
|
x_smooth = spl_x(t_smooth)
|
||||||
y_smooth = spl_y(t_smooth)
|
y_smooth = spl_y(t_smooth)
|
||||||
|
|
||||||
# 创建图形
|
|
||||||
global current_line
|
global current_line
|
||||||
if current_line:
|
if current_line:
|
||||||
current_line.remove()
|
current_line.remove()
|
||||||
current_line, = ax.plot(x_smooth, y_smooth, linestyle='-', color='gray', linewidth=line_width) # 显示轨迹
|
current_line, = ax.plot(x_smooth, y_smooth, linestyle='-', color='gray', linewidth=line_width)
|
||||||
ax.axis('equal') # 确保比例相等
|
ax.axis('equal')
|
||||||
ax.axis('off') # 隐藏坐标轴
|
ax.axis('off')
|
||||||
canvas.draw()
|
canvas.draw()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
messagebox.showerror("无效输入", "请输入有效的整数作为最小和最大顶点数,以及浮点数作为线的粗细和散布。")
|
messagebox.showerror("无效输入", "请输入有效的整数作为最小和最大顶点数,以及浮点数作为线的粗细和散布。")
|
||||||
@ -70,13 +71,10 @@ def adjust_line_width():
|
|||||||
messagebox.showerror("无效输入", "请输入有效的浮点数作为线的粗细。")
|
messagebox.showerror("无效输入", "请输入有效的浮点数作为线的粗细。")
|
||||||
|
|
||||||
def save_shape():
|
def save_shape():
|
||||||
# 获取当前图形
|
|
||||||
buf = io.BytesIO()
|
buf = io.BytesIO()
|
||||||
plt.savefig(buf, format='png', transparent=True)
|
plt.savefig(buf, format='png', transparent=True)
|
||||||
buf.seek(0)
|
buf.seek(0)
|
||||||
image = Image.open(buf).convert("RGBA")
|
image = Image.open(buf).convert("RGBA")
|
||||||
|
|
||||||
# 保存图像
|
|
||||||
file_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG 文件", "*.png"), ("所有文件", "*.*")])
|
file_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG 文件", "*.png"), ("所有文件", "*.*")])
|
||||||
if file_path:
|
if file_path:
|
||||||
image.save(file_path)
|
image.save(file_path)
|
||||||
@ -95,47 +93,79 @@ canvas_widget.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
|
|||||||
|
|
||||||
# 创建输入框和按钮的框架
|
# 创建输入框和按钮的框架
|
||||||
input_frame = tk.Frame(root)
|
input_frame = tk.Frame(root)
|
||||||
input_frame.pack(side=tk.TOP, padx=20, pady=20)
|
input_frame.pack(side=tk.TOP, padx=30, pady=30)
|
||||||
|
|
||||||
# 线的粗细输入框和标签
|
# 线的粗细控制
|
||||||
line_width_label = tk.Label(input_frame, text="线的粗细:")
|
line_width_label = tk.Label(input_frame, text="线的粗细:")
|
||||||
line_width_label.grid(row=0, column=0, padx=5, pady=5)
|
line_width_label.grid(row=0, column=0, padx=10, pady=10, sticky="e")
|
||||||
line_width_entry = tk.Entry(input_frame)
|
line_width_entry = tk.Entry(input_frame, width=8)
|
||||||
line_width_entry.insert(0, "1.0")
|
line_width_entry.insert(0, "1.0")
|
||||||
line_width_entry.grid(row=0, column=1, padx=5, pady=5)
|
line_width_entry.grid(row=0, column=1, padx=10, pady=10)
|
||||||
|
|
||||||
# 散布输入框和标签
|
# 线的粗细 +/- 按钮(只调整线宽)
|
||||||
|
tk.Button(input_frame, text="-", width=3,
|
||||||
|
command=lambda: [update_entry_value(line_width_entry, -0.1), adjust_line_width()]
|
||||||
|
).grid(row=0, column=0, sticky="w")
|
||||||
|
tk.Button(input_frame, text="+", width=3,
|
||||||
|
command=lambda: [update_entry_value(line_width_entry, 0.1), adjust_line_width()]
|
||||||
|
).grid(row=0, column=1, sticky="e")
|
||||||
|
|
||||||
|
# 散布控制
|
||||||
scatter_label = tk.Label(input_frame, text="散布:")
|
scatter_label = tk.Label(input_frame, text="散布:")
|
||||||
scatter_label.grid(row=0, column=2, padx=5, pady=5)
|
scatter_label.grid(row=0, column=2, padx=10, pady=10, sticky="e")
|
||||||
scatter_entry = tk.Entry(input_frame)
|
scatter_entry = tk.Entry(input_frame, width=8)
|
||||||
scatter_entry.insert(0, "0.1")
|
scatter_entry.insert(0, "0.1")
|
||||||
scatter_entry.grid(row=0, column=3, padx=5, pady=5)
|
scatter_entry.grid(row=0, column=3, padx=10, pady=10)
|
||||||
|
|
||||||
|
# 散布 +/- 按钮(刷新图形)
|
||||||
|
tk.Button(input_frame, text="-", width=3,
|
||||||
|
command=lambda: [update_entry_value(scatter_entry, -0.01), generate_and_display_shape()]
|
||||||
|
).grid(row=0, column=2, sticky="w")
|
||||||
|
tk.Button(input_frame, text="+", width=3,
|
||||||
|
command=lambda: [update_entry_value(scatter_entry, 0.01), generate_and_display_shape()]
|
||||||
|
).grid(row=0, column=3, sticky="e")
|
||||||
|
|
||||||
# 调整按钮
|
# 调整按钮
|
||||||
adjust_button = tk.Button(input_frame, text="调整", command=adjust_line_width, width=10, height=2)
|
adjust_button = tk.Button(input_frame, text="调整", command=adjust_line_width, width=10, height=2)
|
||||||
adjust_button.grid(row=0, column=4, padx=5, pady=5)
|
adjust_button.grid(row=0, column=4, padx=10, pady=10)
|
||||||
|
|
||||||
# 最小顶点数输入框和标签
|
# 最小顶点数控制
|
||||||
min_label = tk.Label(input_frame, text="最小顶点数 (>3):")
|
min_label = tk.Label(input_frame, text=" 最小顶点数 (>3):")
|
||||||
min_label.grid(row=1, column=0, padx=5, pady=5)
|
min_label.grid(row=1, column=0, padx=10, pady=10, sticky="e")
|
||||||
min_entry = tk.Entry(input_frame)
|
min_entry = tk.Entry(input_frame, width=8)
|
||||||
min_entry.insert(0, "10")
|
min_entry.insert(0, "10")
|
||||||
min_entry.grid(row=1, column=1, padx=5, pady=5)
|
min_entry.grid(row=1, column=1, padx=10, pady=10)
|
||||||
|
|
||||||
# 最大顶点数输入框和标签
|
# 最小顶点数 +/- 按钮(刷新图形)
|
||||||
max_label = tk.Label(input_frame, text="最大顶点数:")
|
tk.Button(input_frame, text="-", width=3,
|
||||||
max_label.grid(row=1, column=2, padx=5, pady=5)
|
command=lambda: [update_entry_value(min_entry, -1, is_integer=True), generate_and_display_shape()]
|
||||||
max_entry = tk.Entry(input_frame)
|
).grid(row=1, column=0, sticky="w")
|
||||||
|
tk.Button(input_frame, text="+", width=3,
|
||||||
|
command=lambda: [update_entry_value(min_entry, 1, is_integer=True), generate_and_display_shape()]
|
||||||
|
).grid(row=1, column=1, sticky="e")
|
||||||
|
|
||||||
|
# 最大顶点数控制
|
||||||
|
max_label = tk.Label(input_frame, text=" 最大顶点数:")
|
||||||
|
max_label.grid(row=1, column=2, padx=10, pady=10, sticky="e")
|
||||||
|
max_entry = tk.Entry(input_frame, width=8)
|
||||||
max_entry.insert(0, "15")
|
max_entry.insert(0, "15")
|
||||||
max_entry.grid(row=1, column=3, padx=5, pady=5)
|
max_entry.grid(row=1, column=3, padx=10, pady=10)
|
||||||
|
|
||||||
|
# 最大顶点数 +/- 按钮(刷新图形)
|
||||||
|
tk.Button(input_frame, text="-", width=3,
|
||||||
|
command=lambda: [update_entry_value(max_entry, -1, is_integer=True), generate_and_display_shape()]
|
||||||
|
).grid(row=1, column=2, sticky="w")
|
||||||
|
tk.Button(input_frame, text="+", width=3,
|
||||||
|
command=lambda: [update_entry_value(max_entry, 1, is_integer=True), generate_and_display_shape()]
|
||||||
|
).grid(row=1, column=3, sticky="e")
|
||||||
|
|
||||||
# 刷新按钮
|
# 刷新按钮
|
||||||
refresh_button = tk.Button(input_frame, text="刷新", command=generate_and_display_shape, width=10, height=2)
|
refresh_button = tk.Button(input_frame, text="刷新", command=generate_and_display_shape, width=10, height=2)
|
||||||
refresh_button.grid(row=1, column=4, padx=5, pady=5)
|
refresh_button.grid(row=1, column=4, padx=10, pady=10)
|
||||||
|
|
||||||
# 保存按钮
|
# 保存按钮
|
||||||
save_button = tk.Button(input_frame, text="保存", command=save_shape, width=10, height=2)
|
save_button = tk.Button(input_frame, text="保存", command=save_shape, width=10, height=2)
|
||||||
save_button.grid(row=1, column=5, padx=5, pady=5)
|
save_button.grid(row=1, column=5, padx=10, pady=10)
|
||||||
|
|
||||||
# 初始化图形
|
# 初始化图形
|
||||||
current_line = None
|
current_line = None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user