新版本代码

main
陈玉辉 5 months ago
parent 3471b62af6
commit 4d57d0a53e

@ -0,0 +1,410 @@
# encoding: utf-8
from tkinter import *
from tkinter import messagebox
from PIL import Image, ImageTk
import math
from time import sleep
from dbUtil import DbUtil
class Message(Canvas):
# todo: 日志消息类
def __init__(self, master, **kwargs):
super().__init__(master, **kwargs)
self.message = []
self.master = master
self.scrollbar = Scrollbar(master, orient="vertical", command=self.yview)
self.scrollbar.pack(side="right", fill=BOTH)
self.config(yscrollcommand=self.scrollbar.set)
def show_message(self, message, color="white"):
# todo: 添加日志消息
self.message.append({"message": message, "color": color})
num = 0
self.delete("message")
for function in self.message:
self.create_text(20, 25 * num + 10, anchor="nw", text=function["message"], font=("", 15),
fill=function["color"], tags="message")
num += 1
self.update()
self.configure(scrollregion=self.bbox("all"))
self.scrollbar.set(1.0, 1.0)
self.yview_moveto(1.0)
class AddFractal(Toplevel):
def __init__(self, master, _type, fractal_id=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.transient(root)
self.master = master
self.type = _type
self.fractal_id = fractal_id
self.cv = Canvas(self, width=screen_width * 0.65, height=screen_height * 0.8)
self.width, self.height = int(screen_width * 0.65), int(screen_height * 0.8)
self.cv.place(x=0, y=0, anchor=NW)
self.label_width, self.label_height = 140, 40
self.top_window_back_img = ImageTk.PhotoImage(
Image.open("./images/大背景@3x.jpg").resize((int(screen_width * 0.65), int(screen_height * 0.8))))
self.save_img = ImageTk.PhotoImage(Image.open("./images/保存@3x.png").resize((100, self.label_height)))
self.update_img = ImageTk.PhotoImage(Image.open("./images/修改@3x.png").resize((100, self.label_height)))
self.cv.create_image(0, 0, image=self.top_window_back_img, anchor=NW)
self.var_data_dict = {}
if self.type == "update":
fractal = DbUtil.query_fractal_by_id(self.fractal_id).iloc[0]
self.is_default = fractal["is_default"]
self.init_window()
if self.type == "update":
self.load_fractal_data()
def load_fractal_data(self):
fractal = DbUtil.query_fractal_by_id(self.fractal_id).iloc[0]
self.fractal_name.insert(0, str(fractal["name"]))
self.fractal_e_name.insert(0, str(fractal["e_name"]))
self.basic_pattern.insert(0.0, "\n".join(str(fractal["basic_pattern"]).split("\n")[:-1]))
self.function_code.insert(0.0, "\n".join(str(fractal["function_code"]).split("\n")[:-1]))
self.basic_execute_function.insert(0, str(fractal["basic_pattern"]).split("\n")[-1])
self.function_code_execute_function.insert(0, str(fractal["function_code"]).split("\n")[-1])
self.var_tree.insert(0.0, "\n".join(str(fractal["vars"]).split("\n")[:-1]))
if self.is_default == 0:
self.fractal_name.config(state="readonly")
self.fractal_e_name.config(state="readonly")
self.basic_pattern.config(state="disabled")
self.basic_execute_function.config(state="readonly")
self.function_code_execute_function.config(state="readonly")
self.var_tree.config(state="disabled")
self.function_code.config(state="disabled")
def create_label(self, x, y, img, label_text, entry_width=200):
self.cv.create_image(x, y, image=img, anchor=NW)
self.cv.create_text(x + self.label_width / 2, y + self.label_height / 2, text=label_text, font=("黑体", 12),
fill="black")
entry = Entry(self.cv, font=("Arial", 18, "bold"))
entry.place(x=x + self.label_width, y=y, width=entry_width, height=40, anchor=NW)
return entry
def save(self):
fractal_name = self.fractal_name.get()
fractal_e_name = self.fractal_e_name.get()
basic_execute_function = self.basic_execute_function.get()
function_code_execute_function = self.function_code_execute_function.get()
function_code = self.function_code.get(0.0, "end")
basic_pattern = self.basic_pattern.get(0.0, "end")
vars = self.var_tree.get(0.0, "end")
if fractal_name.strip() == "" \
and fractal_e_name == "" \
and basic_execute_function.strip() == "" \
and basic_pattern.strip() == "" \
and function_code.strip() == "" \
and function_code_execute_function.strip() == "":
messagebox.showwarning("提示", "值不能为空!")
return
function_code = function_code + "\n" + function_code_execute_function
basic_pattern = basic_pattern + "\n" + basic_execute_function
if self.type == "update":
DbUtil.update_to_fractal(self.fractal_id, fractal_name, fractal_e_name, basic_pattern, function_code, vars)
self.master.master.message.show_message("更新成功!")
else:
id = DbUtil.insert_to_fractal(fractal_name, fractal_e_name, basic_pattern, function_code, vars)
self.master.master.message.show_message("添加成功!")
self.destroy()
self.master.master.flush()
def init_window(self):
self.label_img = ImageTk.PhotoImage(
Image.open("./images/文字背景@3x.png").resize((self.label_width, self.label_height)))
self.fractal_name = self.create_label(35, 30, self.label_img, "分形名称", int((self.width - 100) / 2 - 140))
self.fractal_e_name = self.create_label(35 + int((self.width - 100) / 2) + 30, 30, self.label_img,
"分形英文名称", int((self.width - 100) / 2 - 140))
self.basic_execute_function = self.create_label(35, 30 + (self.label_height + 15) * 1, self.label_img,
"基本图案调用函数", int((self.width - 100) / 2 - 140))
self.function_code_execute_function = self.create_label(35 + int((self.width - 100) / 2) + 30,
30 + (self.label_height + 15) * 1, self.label_img,
"分形树调用函数",
entry_width=int((self.width - 100) / 2 - 140))
if self.is_default != 0:
Button(self.cv, image=self.save_img if self.type == "add" else self.update_img, command=self.save, bd=0,
relief="solid", bg="#f7f7f7",
highlightthickness=0, ).place(x=(self.width - 100) / 2, y=self.height - 60, width=100,
height=self.label_height, anchor=NW)
self.basic_label_image = ImageTk.PhotoImage(
Image.open("./images/文字背景@3x.png").resize((int((self.width - 100) / 2), 30)))
# 分形树绘制表单
self.cv.create_image(35 + int((self.width - 100) / 2) + 30, 30 + (self.label_height + 15) * 2,
image=self.basic_label_image, anchor=NW)
self.cv.create_text(35 + int((self.width - 100) / 2) + 30 + int((self.width - 100) / 4),
30 + (self.label_height + 15) * 2 + 15, text="分形图案绘制函数", font=("黑体", 14))
self.function_code = Text(self.cv, font=("Arial", 14), bg="#bce7f7", highlightthickness=0,
highlightcolor="#bce7f7",
spacing1=5, spacing2=3, spacing3=3, wrap="none")
self.function_code.place(x=35 + int((self.width - 100) / 2) + 30, y=30 + (self.label_height + 15) * 2 + 30,
height=self.height - 250, width=int((self.width - 100) / 2))
# 基本图形绘制表单
self.cv.create_image(35, 30 + (self.label_height + 15) * 2, image=self.basic_label_image, anchor=NW)
self.cv.create_text(35 + int((self.width - 100) / 4), 30 + (self.label_height + 15) * 2 + 15,
text="基本图案绘制函数", font=("黑体", 14))
self.basic_pattern = Text(self.cv, font=("Arial", 14), bg="#bce7f7", highlightthickness=0,
spacing1=5, spacing2=3, spacing3=3, wrap="none", undo=True)
self.basic_pattern.place(x=35, y=30 + (self.label_height + 15) * 2 + 30, width=int((self.width - 100) / 2),
height=285)
# 初始变量表单
self.cv.create_image(35, 30 + (self.label_height + 15) * 2 + 30 + 285 + 15, image=self.basic_label_image,
anchor=NW)
self.cv.create_text(35 + int((self.width - 100) / 4), 30 + (self.label_height + 15) * 2 + 30 + 285 + 30,
text="初始变量",
font=("黑体", 14))
self.var_tree = Text(self.cv, font=("Arial", 14), bg="#bce7f7", highlightthickness=0, highlightcolor="#bce7f7",
spacing1=10, spacing2=5, spacing3=5, wrap="none")
self.var_tree.place(x=35, y=30 + (self.label_height + 15) * 2 + 30 + 280 + 45,
height=290, width=int((self.width - 100) / 2))
class SelectFractalFrame(Frame):
# todo: 右上方分形图案单选框类
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.db = DbUtil()
self.width = int(self.cget("width"))
self.height = int(self.cget("height"))
self.img = ImageTk.PhotoImage(Image.open("./images/上部背景@3x.png").resize(
(int(self.width), self.height)))
Label(self, width=self.width, height=self.height, image=self.img).place(x=0, y=0, anchor=NW)
Label(self, text="分形图案类型", font=("黑体", 14, "bold"), bg="#f7f7f7", ).place(x=10, y=10)
self.button_img = ImageTk.PhotoImage(Image.open("./images/新增分形图案类型@3x.png").resize((120, 36)))
Button(self, image=self.button_img, bd=0, relief="solid", bg="#f7f7f7",
highlightthickness=0, command=self.add_fractal_tree).place(x=280, y=8)
self.update_button_img = ImageTk.PhotoImage(Image.open("./images/修改分形图案@3x.png").resize((120, 36)))
Button(self, image=self.update_button_img, bd=0, relief="solid", bg="#f7f7f7",
highlightthickness=0, command=self.update_fractal_tree).place(x=150, y=8)
self.select_frame = Frame(self, width=self.width, height=self.height * 0.8, bg="#f7f7f7")
self.select_frame.place(x=0, y=self.height * 0.2)
self.create_select_menu()
def add_fractal_tree(self):
screen_width = root.winfo_screenwidth() # winfo方法来获取当前电脑屏幕大小
screen_height = root.winfo_screenheight()
x, y = int(screen_width - screen_width * 0.65) // 2, int(screen_height - screen_height * 0.8) // 2
top_window = AddFractal(self, "add", width=screen_width * 0.65, height=screen_height * 0.8)
top_window.geometry(f'{int(screen_width * 0.65)}x{int(screen_height * 0.8)}+{x}+{y}')
top_window.mainloop()
def update_fractal_tree(self):
screen_width = root.winfo_screenwidth() # winfo方法来获取当前电脑屏幕大小
screen_height = root.winfo_screenheight()
x, y = int(screen_width - screen_width * 0.65) // 2, int(screen_height - screen_height * 0.8) // 2
top_window = AddFractal(self, "update", width=screen_width * 0.65, height=screen_height * 0.8,
fractal_id=self.master.chose_fractal.get())
top_window.geometry(f'{int(screen_width * 0.65)}x{int(screen_height * 0.8)}+{x}+{y}')
top_window.mainloop()
def create_select_menu(self):
# todo: 创建分形图案单选框
data_frame = self.db.execute("select * from fractal")
yscro = Scrollbar(self.select_frame, orient=VERTICAL)
yscro.place(x=self.width - 20, y=0, height=self.height * 0.8, width=20, anchor='nw')
canvas = Canvas(self.select_frame, width=self.width - 20, height=self.height * 0.8, bg="#f7f7f7")
canvas.place(x=0, y=0, anchor='nw')
f2 = Frame(canvas, bg="#f7f7f7", width=self.width * 0.8, height=self.height * 0.8)
canvas.create_window((0, 0), window=f2, anchor=NW)
self.master.chose_fractal = IntVar()
self.master.chose_fractal.set(data_frame.loc[0:]["id"].tolist()[0])
for data in data_frame.itertuples(index=False):
f_id, name, e_name, basic_pattern, function_code = data[0], data[1], data[2], data[3], data[4]
Radiobutton(f2, text=f"{name} {e_name}", value=f_id,
command=lambda message=f"{name} {e_name}": self.master.update_select(message),
variable=self.master.chose_fractal, font=("", 18), bg="#f7f7f7").pack(ipadx=10, anchor="w")
canvas.update()
canvas.config(scrollregion=canvas.bbox("all"), yscrollcommand=yscro.set) # 设置画布的滚动区域
yscro.config(command=canvas.yview)
class FormFractalFrame(Frame):
# todo: 右下方变量填写类
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.db = DbUtil()
self.width = int(self.cget("width"))
self.height = int(self.cget("height"))
self.var_dict = {}
self.img = ImageTk.PhotoImage(Image.open("./images/上部背景@3x.png").resize(
(int(self.width), self.height)))
Label(self, width=self.width, height=self.height, image=self.img).place(x=0, y=0, anchor=NW)
Label(self, text="分形参数设置", font=("黑体", 14, "bold"), bg="#f7f7f7", ).place(x=10, y=10)
self.create_form()
self.button_img = ImageTk.PhotoImage(Image.open("./images/基本图案绘制@3x.png").resize((90, 36)))
Button(self, image=self.button_img, bd=0, relief="solid", bg="#f7f7f7",
highlightthickness=0, command=self.master.basic_tree_canvas.draw_basic_tree).place(x=10,
y=self.height - 50)
self.button2_img = ImageTk.PhotoImage(Image.open("./images/绘制参数更新@3x.png").resize((90, 36)))
Button(self, image=self.button2_img, bd=0, relief="solid", bg="#f7f7f7",
highlightthickness=0, command=self.update_fractal_var).place(x=100 + 5, y=self.height - 50)
self.button3_img = ImageTk.PhotoImage(Image.open("./images/绘制@3x.png").resize((70, 36)))
Button(self, image=self.button3_img, bd=0, relief="solid", bg="#f7f7f7",
highlightthickness=0,
command=lambda params=self.var_dict: self.master.fractal_tree_canvas.draw_fractal_tree(params)).place(
x=100 * 2 + 5, y=self.height - 50)
self.button4_img = ImageTk.PhotoImage(Image.open("./images/删除图案@3x.png").resize((90, 36)))
Button(self, image=self.button4_img, bd=0, relief="solid", bg="#f7f7f7",
highlightthickness=0, command=self.delete_fractal).place(x=95 * 3, y=self.height - 50)
def delete_fractal(self):
"""
删除分形树
:return:
"""
result = messagebox.askquestion("确认框", "您确定要执行此操作吗?")
if not result == "yes":
return
fractal_id = self.master.chose_fractal.get()
DbUtil.delete_fractal(fractal_id)
self.master.flush()
self.master.message.show_message("删除成功!")
def update_fractal_var(self):
# todo: 绘制参数更新
data_frame = self.db.query_fractal_by_id(self.master.chose_fractal.get()).iloc[0]
params = {
"self": self,
"var_text": self.var_text.get(0.0, END).strip()
}
self.var_text.delete(0.0, END)
exec(data_frame["vars"], params)
def create_form(self):
# todo: 分形图案变量生成
data_frame = self.db.query_fractal_by_id(self.master.chose_fractal.get()).iloc[0]
self.var_text = Text(self, font=("Arial", 14), bg="#f7f7f7", highlightthickness=0, highlightcolor="#f7f7f7",
spacing1=10, spacing2=5, spacing3=5, wrap="none")
self.var_text.place(x=0, y=self.height * 0.2, width=self.width, height=self.height * 0.6)
params = {
"self": self,
"var_text": self.var_text.get(0.0, END)
}
exec(data_frame["vars"], params)
class BasicTree(Canvas):
# 基本图案绘制画布类
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.db = DbUtil()
self.width = int(self.cget("width"))
self.height = int(self.cget("height"))
self.background_img = ImageTk.PhotoImage(
Image.open("./images/全览图@3x.png").resize((self.width + 20, self.height + 20)))
self.create_image(-5, -5, image=self.background_img, anchor=NW)
def draw_basic_tree(self):
# todo: 绘制基本图案
self.delete("all")
data_frame = self.db.query_fractal_by_id(self.master.chose_fractal.get())
params = {
"canvas": self
}
try:
exec(data_frame["basic_pattern"].tolist()[0], params)
self.master.message.show_message(f"基本图案绘制成功!")
except Exception as E:
self.master.message.show_message(f"{E}", color="red")
class FractalTreeCanvas(Canvas):
# todo: 绘制分形树画布
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.db = DbUtil()
self.width = int(self.cget("width"))
self.height = int(self.cget("height"))
def draw_fractal_tree(self, params):
try:
self.delete("all")
data_frame = self.db.query_fractal_by_id(self.master.chose_fractal.get())
params = {key: value for key, value in params.items()}
params["canvas"] = self
params["math"] = math
params["sleep"] = sleep
exec(data_frame["function_code"].tolist()[0], params)
self.master.message.show_message("分形树绘制成功!")
except Exception as E:
self.master.message.show_message(f"{E}", "red")
class FractalTree(Canvas):
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.db = DbUtil()
self.width = int(self.cget("width"))
self.height = int(self.cget("height"))
self.background_image = ImageTk.PhotoImage(
Image.open("./images/大背景@3x.jpg").resize((self.width, self.height)))
self.create_image(0, 0, image=self.background_image, anchor=NW)
self.chose_fractal = 1
self.function_code = ""
self.fractal_var = {}
self.init_window()
def update_select(self, message):
# todo: 切换分形图案
self.message.show_message(f"选择了{message},初始参数如下:")
self.form_fractal_frame.create_form()
def flush(self):
self.select_fractal_frame.create_select_menu()
self.form_fractal_frame.create_form()
def init_window(self):
"""
初始化窗口创建窗口并布局
:return:
"""
# 创建下方消息显示框
message_frame = Frame(self, width=self.width - 60, height=self.height * 0.2)
message_frame.place(x=15, y=self.height * 0.8 + 15, anchor=NW)
self.message = Message(message_frame, width=self.width - 60, height=self.height * 0.2 - 30)
self.message.pack(anchor=NW)
self.message.configure(bg="#85bbd1")
# 分形树画布
self.fractal_tree_canvas = FractalTreeCanvas(self, width=self.width * 0.73 - 10, height=self.height * 0.8 + 12,
highlightthickness=0, bg="#b5e6f5")
self.fractal_tree_canvas.place(x=0, y=0, anchor=NW)
# 基本图形绘制框
self.basic_tree_canvas = BasicTree(self, width=150, height=150, bg="#e0eaf5", bd=0)
self.basic_tree_canvas.place(x=15, y=self.height * 0.8 - 150, anchor=NW)
# 创建左上方分形图案选择框
self.select_fractal_frame = SelectFractalFrame(self, width=self.width * 0.27 - 10,
height=int((self.height * 0.8) / 2) - 10)
self.select_fractal_frame.place(x=self.width * 0.73, y=10, anchor=NW)
# 创建左下方输入框
self.form_fractal_frame = FormFractalFrame(self, width=self.width * 0.27 - 10,
height=int((self.height * 0.8) / 2))
self.form_fractal_frame.place(x=self.width * 0.73, y=self.height * 0.8 / 2 + 10, anchor=NW)
if __name__ == '__main__':
# 创建主窗口
root = Tk()
root.title('分形树')
screen_width = root.winfo_screenwidth() # winfo方法来获取当前电脑屏幕大小
screen_height = root.winfo_screenheight()
root_attr = {
"width": screen_width * 0.8,
"height": screen_height * 0.8,
}
size = '%dx%d+%d+%d' % (root_attr['width'], root_attr['height'], (screen_width - root_attr['width']) / 2,
(screen_height - root_attr['height']) / 2 - 30)
root.geometry(size)
tree = FractalTree(root, width=root_attr["width"], height=root_attr["height"])
tree.place(x=0, y=0, anchor=NW)
root.mainloop()

68
X1.py

@ -0,0 +1,68 @@
import math
# print(math.cos(math.radians(90)))
#
# start=(440,670)
# print(start[0]+200*math.cos(math.radians(90)))
# print(start[1] - 200 * math.sin(math.radians(90)))
import tkinter as tk
# 创建主窗口
root = tk.Tk()
root.title("绘制Y形")
# 设置画布
canvas = tk.Canvas(root, bg="white", width=1000, height=700)
canvas.pack()
def draw_shape():
# 绘制三角形
point1 = [(150, 20), (50, 200), (250, 200)]
canvas.create_polygon(point1, outline='black', fill='white', width=2)
# 绘制菱形
point2 = [(150, 240), (50, 360), (150, 480), (250, 360)]
canvas.create_polygon(point2, outline='black', fill='white', width=2)
# 绘制 Y形
canvas.create_line(340, 20, 420, 80, fill='blue3', width=2) # 画左臂
canvas.create_line(510, 20, 420, 80, fill='blue3', width=2) # 画右臂
canvas.create_line(420, 180, 420, 80, fill='blue3', width=2) # 画中心线
# 绘制圆形
canvas.create_oval(340, 240, 540, 440, width=2)
# 绘制直线
canvas.create_line(330, 470, 550, 470, fill='blue3', width=2)
# 绘制正方形
canvas.create_rectangle(650, 420, 800, 270, width=2)
# root=tk.Tk()
# canvas = tk.Canvas(root, bg="white", highlightthickness=1, highlightbackground="tomato")
# canvas.config(width=1000, height=700)
# canvas.place(x=50, y=50)
#
# canvas.pack()
draw_shape()
root.mainloop()
def draw_rect(x, y, w, a):
coord = (x, y, x - w * math.cos(math.radians(a)), y + w * math.sin(math.radians(a)), \
x - math.sqrt(2) * w * math.sin(math.radians(a + 45)),
y - math.sqrt(2) * w * math.cos(math.radians(a + 45)), \
x - w * math.sin(math.radians(a)), y - w * math.cos(math.radians(a)))
canvas.create_polygon(coord, fill='', outline='black')
root = tk.Tk()
root.geometry('1000x800')
canvas = tk.Canvas(root, bg="white", highlightthickness=1, highlightbackground="tomato")
canvas.config(width=1000, height=700)
canvas.pack()
a = 0
# 实现边长增加5旋转度数增加5
for i in range(5, 301, 5):
draw_rect(400, 400, i, a)
canvas.update()
a += 5
root.mainloop()

49
X2.py

@ -0,0 +1,49 @@
# FH的python代码
# 文本分析和图像识别
# 开发时间 2023/7/17 17:07
import tkinter as tk
import math
root = tk.Tk()
cv_small = tk.Canvas(root, width=220, height=180)
cv_small.pack()
cv_small.delete("all") # 清除画布
cv_small.create_line(50, 30, 110, 90, fill='red', width=2) # 左臂
cv_small.create_line(170, 30, 110, 90, fill='red', width=2) # 右臂
cv_small.create_line(110, 160, 110, 90, fill='red', width=2) # 中心线
root.mainloop()
def Y_tree(depth, percent, left_angle, right_angle, start_point, angle, length, canvas):
if depth == 0:
return
end_point = (start_point[0] + length * math.cos(math.radians(angle)),
start_point[1] - length * math.sin(math.radians(angle)))
canvas.create_line(start_point, end_point, fill='LightBlue4', width=2)
# canvas.update()
# canvas.after(1)
Y_tree(depth - 1, percent, left_angle, right_angle, end_point, angle + right_angle, length * percent,
canvas) # 画右枝
Y_tree(depth - 1, percent, left_angle, right_angle, end_point, angle - left_angle, length * percent,
canvas) # 画左枝
# print(start_point,end_point)
if __name__ == "__main__":
root = tk.Tk()
canvas = tk.Canvas(root, width=1200, height=1000)
canvas.pack()
start_point = (300, 600)
angle = 45
length = 200
depth = 12
percent = 0.7
left_angle = 30
right_angle = 30
Y_tree(depth, percent, left_angle, right_angle, start_point, angle, length, canvas)
root.mainloop()

123
X24.py

@ -0,0 +1,123 @@
# FH的python代码
# 文本分析和图像识别
# 开发时间 2023/7/17 15:52
# 创建主窗口
import fractaltree1
import tkinter as tk
import tkinter.font as tf
root = tk.Tk()
w = root.winfo_screenwidth()
h = root.winfo_screenheight()
root.geometry("%dx%d" % (w, h))
root.title("分形树 电脑的分辨率是%dx%d" % (root.winfo_screenwidth(), root.winfo_screenheight()))
# 创建新增分形图案类型所需的字典
str_params_dict = {}
pattern_dict = {}
tree_dict = {}
update_params_dict = {}
# 定义变量,用于存储用户选择的分形类型
shape_var = tk.StringVar()
# 设置字体
ft1 = tf.Font(family="黑体", size=21)
ft2 = tf.Font(family="黑体", size=21, weight=tf.BOLD)
ft3 = tf.Font(family="黑体", size=18)
ft4 = tf.Font(family="黑体", size=19)
# 创建一个标签
lab1 = tk.Label(root, text="分形图案类型", font=ft1)
lab1.place(x=20, y=10, width=180, height=50, anchor='nw')
def add_fractal():
# 创建一个按钮,用来新增分形图案类型
button_addPattern = tk.Button(root, text="新增分形图案类型", command=add_fractal, font=ft2)
button_addPattern.place(x=500, y=10, width=270, height=50, anchor='ne')
## 滚动框的设置
# 创建一个Canvas对象使用该对象作为滚动区域
canvas = tk.Canvas(root, width=445, height=160)
# 创建一个列表框对象Rlist并将其放入滚动区域中
Rlist = tk.Listbox(canvas, width=445, height=20, bg='white', highlightthickness=1, highlightbackground="LightBlue")
# 创建滚动条对象并与Canvas绑定实现滚动条功能
scrollbar = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=scrollbar.set)
canvas.place(x=20, y=75, anchor='nw')
scrollbar.place(x=470, y=75, width=30, height=160, anchor='nw')
# 创建一个Frame组件用于放置单选框
frame = tk.Frame(Rlist, bg='white')
frame.place(anchor='nw')
# 将列表框放入Canvas中并设置Canvas窗口的大小
box_id = canvas.create_window((0, 0), window=Rlist, anchor="nw")
Rlist.update_idletasks() # 更新视图
canvas.config(scrollregion=canvas.bbox("all")) # 设置画布的滚动区域
# 添加单选框到 Frame组件
rb_Y = tk.Radiobutton(frame, text="Y形分形 Y_Fractal", variable=shape_var, value='Y', bg="white", font=ft1)
rb_Y.pack(anchor='w')
rb_Triangle = tk.Radiobutton(frame, text="三角形分形 Triangle_Fractal", variable=shape_var, value='Triangle',bg="white", font=ft1)
rb_Triangle.pack(anchor='w')
rb_Rect = tk.Radiobutton(frame, text="矩形分形 Rect_Fractal", variable=shape_var, value='Rect', bg="white", font=ft1)
rb_Rect.pack(anchor='w')
from fractaltree1 import pd
import os
if os.path.exists('params.csv'):
df=pd.read_csv("params.csv")
for i in df["分型名称"]:
rb1=tk.Radiobutton(frame,text=i,variable=shape_var,value=df[df['分型名称']==i].分形英文.values[0], bg="white", font=ft1)
rb1.pack(anchor='w')
# 将默认选项设置为 Y 形分形
rb_Y.select()
# 创建一个标签
lab2 = tk.Label(root, text="分形参数设置【参数含义:参量名=参量值】", font=ft4)
lab2.place(x=20, y=255, width=490, height=40, anchor='nw')
# 大画布的宽度和高度变量,方便后期调整
big_width = 880
big_height = 690
# 创建蓝色边框的大画布
cv_big = tk.Canvas(root, bg="white", highlightthickness=1, highlightbackground="LightBlue")
cv_big.config(width=big_width, height=big_height)
cv_big.place(x=530, y=10, anchor='nw')
# 创建红色边框的小画布宽高设置为大画布的1/4
cv_small = tk.Canvas(cv_big, bg="white", highlightthickness=1, highlightbackground="tomato")
cv_small.config(width=big_width * 0.25, height=big_height * 0.25)
cv_small.place(x=0, y=big_height, anchor='sw')
#创建一个多行文本框
params_text = tk.Text(root, bg="white", highlightthickness=1, highlightbackground="LightBlue")
params_text.configure(font=ft3, spacing1=15)
params_text.place(x=20, y=300, width=480, height=310, anchor='nw')
from fractaltree1 import params_Y
# 初始化分形参数并输出
params_str = params_Y
params_text.insert(tk.INSERT, params_str)
from fractaltree1 import update_Text
# trace追踪变量w模式回调函数update_text
shape_var.trace('w', lambda *args, str_params_dict=str_params_dict: update_Text())
from fractaltree1 import delete_rb
from fractaltree1 import draw_pattern
from fractaltree1 import update_params
from fractaltree1 import draw_tree
# 创建四个按钮
button_delete = tk.Button(root, text="删除\n选项", command=delete_rb, font=ft2)
button_delete.place(x=20, y=700, width=90, height=75, anchor='sw')
button_pattern = tk.Button(root, text="基本图\n案绘制", command=draw_pattern, font=ft2)
button_pattern.place(x=130, y=700, width=120, height=75, anchor='sw')
button_params = tk.Button(root, text="绘制参\n数设置", command=update_params, font=ft2)
button_params.place(x=270, y=700, width=120, height=75, anchor='sw')
button_drawTree = tk.Button(root, text="绘制", command=draw_tree, font=ft2)
button_drawTree.place(x=410, y=700, width=90, height=75, anchor='sw')

88
X3.py

@ -0,0 +1,88 @@
import tkinter as tk
import tkinter.font as tf
import math
FinishLevel_Y = 14
TreePercent_Y = 0.7
LeftAngle_Y = 30
RightAngle_Y = 30
InitPoint_Y = (420, 500)
InitAngle_Y = 90
InitLong_Y = 150
params_Y = " 分形层数finishLevel={}\n" \
" 缩放比例treePercent={}\n" \
" 左枝倾角leftAngle={}\n" \
" 右枝倾角rightAngle={}\n" \
" 初始点initPoint={}\n" \
" 初始角initAngle={}\n" \
" 初始长度initLong={}".format(FinishLevel_Y, TreePercent_Y, LeftAngle_Y, RightAngle_Y,
InitPoint_Y, InitAngle_Y, InitLong_Y)
# 创建全局变量 cv_small 和 cv_big
cv_small = None
cv_big = None
# Y形基本图案函数
def yPattern():
cv_small.delete("all") # 清除画布
cv_small.create_line(30, 10, 90, 70, fill='red', width=2) # 左臂
cv_small.create_line(150, 10, 90, 70, fill='red', width=2) # 右臂
cv_small.create_line(90, 140, 90, 70, fill='red', width=2)
# Y形树执行函数
def run_Y_tree():
def Y_tree(depth, percent, left_angle, right_angle, start_point, angle, length, canvas):
if depth == 0:
return
end_point = (start_point[0] + length * math.cos(math.radians(angle)),
start_point[1] - length * math.sin(math.radians(angle)))
canvas.create_line(start_point, end_point, fill='LightBlue4', width=2)
Y_tree(depth - 1, percent, left_angle, right_angle, end_point, angle + right_angle, length * percent,
canvas) # 画右枝
Y_tree(depth - 1, percent, left_angle, right_angle, end_point, angle - left_angle, length * percent,
canvas) # 画左枝
root = tk.Tk()
root.geometry('1270x650')
root.title('分形树')
ft3 = tf.Font(family="黑体", size=18)
global cv_small, cv_big # 声明 cv_small 和 cv_big 为全局变量
# 创建蓝色边框的大画布
cv_big = tk.Canvas(root, bg="white", highlightthickness=1, highlightbackground="LightBlue")
cv_big_width = 880
cv_big_height = 690
cv_big.config(width=cv_big_width, height=cv_big_height)
cv_big.place(x=330, y=10, anchor='nw')
# 创建红色边框的小画布
cv_small_width = cv_big_width // 4
cv_small_height = cv_big_height // 4
cv_small = tk.Canvas(root, bg="white", highlightthickness=1, highlightbackground="red")
cv_small.config(width=cv_small_width, height=cv_small_height)
cv_small.place(x=530, y=cv_big_height - cv_small_height + 10, anchor='nw')
# 绘制Y形基本图案
yPattern()
# 调用绘制Y形分形树的函数
Y_tree(FinishLevel_Y, TreePercent_Y, LeftAngle_Y, RightAngle_Y, InitPoint_Y, InitAngle_Y, InitLong_Y, cv_big)
# 创建底部的结果输出框
output_text = tk.Text(root, bg="white", highlightthickness=1, highlightbackground="LightBlue")
output_text.configure(font=ft3, spacing1=8)
output_text.insert(tk.INSERT, params_Y)
# 设置文本框不可编辑
output_text.configure(state="disabled")
output_text.place(x=20, y=800, width=1390, height=80, anchor='sw')
root.mainloop()
# 调用运行Y形树的函数
run_Y_tree()

@ -0,0 +1,86 @@
# FH的python代码
# 文本分析和图像识别
# 开发时间 2023/7/17 16:40
import math
import fractaltree1
def run_Y_tree():
# Y形分形树函数
def Y_tree(depth, percent, left_angle, right_angle, start_point, angle, length, canvas):
if depth == 0:
return
end_point = (start_point[0] + length * math.cos(math.radians(angle)),
start_point[1] - length * math.sin(math.radians(angle)))
canvas.create_line(start_point, end_point, fill='LightBlue4', width=2)
# canvas.update()
# canvas.after(1)
Y_tree(depth - 1, percent, left_angle, right_angle, end_point, angle + right_angle, length * percent,
canvas) # 画右枝
Y_tree(depth - 1, percent, left_angle, right_angle, end_point, angle - left_angle, length * percent,
canvas) # 画左枝
# print(start_point,end_point)
# 清除画布
cv_big.delete("all")
# 将初始参数传入函数
Y_tree(finishLevel_Y, treePercent_Y, leftAngle_Y, rightAngle_Y, initPoint_Y, initAngle_Y, initLong_Y, cv_big)
output_text.delete("1.0", "end")
# 在底部的结果输出框输出分形树的绘制参数
text = " 结果输出:<分形绘制参数的字符串形式输出>\n finishLevel={}, treePercent={}, leftAngle={}, rightAngle={}, " \
"initPoint={}, initAngle={}, initLong={}".format(finishLevel_Y, treePercent_Y, leftAngle_Y,
rightAngle_Y, initPoint_Y, initAngle_Y, initLong_Y)
output_text.insert(tk.INSERT, text)
# 三角形分形树执行函数(倒的坐标系)
def run_Triangle_tree():
# 三角形分形树函数
def Triangle_tree(depth, percent, start_point, angle, length, canvas):
if depth == 0:
return
# 左枝点
end_point1 = (start_point[0] - length * math.sin(math.radians(angle)),
start_point[1] - length * math.cos(math.radians(angle)))
# 右枝点
end_point2 = (start_point[0] + length * math.sin(math.radians(angle)),
start_point[1] - length * math.cos(math.radians(angle)))
# 画三角形
canvas.create_polygon(start_point, end_point1, end_point2, fill='LightBlue4', outline='white')
# 画左枝
Triangle_tree(depth - 1, percent, end_point1, angle, length * percent, canvas)
# 画右枝
Triangle_tree(depth - 1, percent, end_point2, angle, length * percent, canvas)
# canvas.update()
# canvas.after(10)
# 方形分形树执行函数
def run_Rect_tree():
# 方形分形树函数
def Rect_tree(depth, point1, point2, angle, canvas):
# 直线的旋转point1是定点
def rotate(point1, point2, angle):
x1, y1 = point1[0], point1[1]
x2, y2 = point2[0], point2[1]
x = x1 + (x2 - x1) * math.cos(math.radians(angle)) + (y2 - y1) * math.sin(math.radians(angle))
y = y1 + (y2 - y1) * math.cos(math.radians(angle)) - (x2 - x1) * math.sin(math.radians(angle))
point = (x, y)
# print(point)
return point
# 直线的缩放point1是定点
def zoom(point1, point2, ratio):
x1, y1 = point1[0], point1[1]
x2, y2 = point2[0], point2[1]
x = x1 + (x2 - x1) * ratio
y = y1 + (y2 - y1) * ratio
point = (x, y)
return point
point3 = rotate(point1, point2, 90)
point4 = rotate(point2, point1, 270)
# print(point1,point2,point3,point4)
# 画正方形
canvas.create_polygon(point1, point2, point4, point3, fill='LightBlue4', outline='white')
if depth == 0:
return
point = rotate(point3, point4, angle) # 旋转
point = zoom(point3, point, math.cos(math.radians(angle))) # 缩放
Rect_tree(depth - 1, point, point4, angle, canvas) # 画左枝
Rect_tree(depth - 1, point3, point, angle, canvas) # 画右枝

37
Y.py

@ -0,0 +1,37 @@
import math
from time import sleep
import tkinter as tk
root = tk.Tk()
def Y_pattern():
canvas.delete("all") # 清除画布
canvas.create_line(20, 30, 80, 90, fill='red', width=2) # 左臂
canvas.create_line(140, 30, 80, 90, fill='red', width=2) # 右臂
canvas.create_line(80, 160, 80, 90, fill='red', width=2) # 中心线
def Y_tree(depth, percent, left_angle, right_angle, start_point, angle, length, canvas):
if depth == 0:
return
end_point = (start_point[0] + length * math.cos(math.radians(angle)),
start_point[1] - length * math.sin(math.radians(angle)))
canvas.create_line(start_point, end_point, width=2)
Y_tree(depth - 1, percent, left_angle, right_angle, end_point, angle + right_angle, length * percent,
canvas) # 画右枝
Y_tree(depth - 1, percent, left_angle, right_angle, end_point, angle - left_angle, length * percent,
canvas) # 画左枝
canvas.update()
sleep(0.001)
if __name__ == '__main__':
canvas = tk.Canvas(root, width=1200, height=1000)
canvas.pack()
finishLevel = 10
treePercent = 0.7
leftAngle = 60
rightAngle = 60
initPoint = (520, 600)
initAngle = 90
initLong = 160
Y_tree(finishLevel, treePercent, leftAngle,rightAngle,initPoint,initAngle,initLong,canvas)

@ -0,0 +1,57 @@
import pymysql
import pandas as pd
conn = pymysql.connect(user="root", password="123123", database="tree",
host="127.0.0.1", port=3306)
class DbUtil():
@staticmethod
def insert_to_fractal(name, e_name, basic_pattern, function_code, vars):
sql = """
insert into fractal(name, e_name, basic_pattern, function_code, vars) values (%s, %s, %s, %s, %s)
"""
cursor = conn.cursor()
cursor.execute(sql, (name, e_name, basic_pattern, function_code, vars))
conn.commit()
id = cursor.lastrowid
return id
@staticmethod
def update_to_fractal(fractal_id, name, e_name, basic_pattern, function_code, vars):
sql = """
update fractal set name=%s, e_name=%s, basic_pattern=%s, function_code=%s, vars=%s where id=%s
"""
cursor = conn.cursor()
cursor.execute(sql, (name, e_name, basic_pattern, function_code, vars, fractal_id))
conn.commit()
@staticmethod
def insert_to_var(name, value, comment, fractal_id):
sql = f"insert into fractal_var values ('{name}', '{value}', '{comment}', {fractal_id})"
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
@staticmethod
def delete_fractal(id):
conn.cursor().execute(f"delete from fractal_var where fractal_id={id}")
conn.commit()
conn.cursor().execute(f"delete from fractal where id={id}")
conn.commit()
@staticmethod
def execute(sql):
return pd.read_sql(sql, con=conn)
@staticmethod
def update(sql):
conn.cursor().execute(sql)
conn.commit()
@staticmethod
def query_fractal_by_id(id):
return pd.read_sql("select * from fractal where id={}".format(id), con=conn)
@staticmethod
def query_fractal_var_by_id(id):
return pd.read_sql("select * from fractal_var where fractal_id={}".format(id), con=conn)

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

@ -0,0 +1,14 @@
function_string = """
def recursive_function(parameter):
if parameter <= 0:
return
print(parameter)
recursive_function(parameter - 1)
recursive_function(parameter)
"""
# 定义参数字典
params = {'parameter': 5}
# 使用exec()执行函数字符串
exec(function_string, params)

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 615 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 664 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 115 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 218 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 726 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save