1 Star 0 Fork 1

yoyoliyang/magic_square_generator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
magic_square_generator.py 4.04 KB
一键复制 编辑 原始数据 按行查看 历史
yoyoliyang 提交于 2022-11-10 16:01 . first commit
# 幻方生成器
# ver: 1.0
# author: ocean
# date: 22.11.8
# 输出一栏本来打算使用label绘制,但是不如Text方式方便
# 因为性能问题,1000行以上幻方的生成与绘制会出现卡顿问题
from tkinter import *
from tkinter.ttk import *
from core import MagicSquareGenerator
from label_grid import MsGrid, MsText
# 初始化根窗口
title = "幻方生成器"
root = Tk()
root.title(title)
width, height = 800, 400
root.geometry("%dx%d+%d+%d" % (width, height,
root.winfo_screenwidth() // 2 - width // 2,
root.winfo_screenheight() // 2 - height // 2))
root.resizable(False, False)
# 创建PanedWindow组件,用来平铺LabelFrame
# pw = Panedwindow(orient=HORIZONTAL)
# ==== 画布 ====
left_frame = LabelFrame(root,
text="幻方区域",
width=500)
# pw.add(left_frame)
right_frame = LabelFrame(root,
text="操作区域",
width=200)
# pw.add(right_frame)
# 阶数标签
degree = Label(right_frame,
justify=LEFT,
text="阶数:",
width=6)
# 阶数输入框
degree_entry = Entry(right_frame,
width=10)
degree_entry.focus_set() # 设置焦点为当前输入框
# 幻和标签
ms_sum = Label(right_frame,
text="幻和:",
width=6)
# 幻和输出框
ms_sum_var = IntVar()
ms_sum_var.set(0)
ms_sum_entry = Entry(right_frame,
state="disabled",
textvariable=ms_sum_var,
width=10)
# 生成幻方按钮
generator_btn = Button(right_frame,
text="生成幻方",
width=20)
# 复制到剪切板按钮
copy_btn = Button(right_frame,
text="复制到剪贴板",
width=20)
# 说明按钮
readme_msg = """
幻方:给定n行n列方格,把1~n²填入方格中,使每行、每列及对角线的和都相等。
不存在3阶以下的幻方。
超出窗口显示范围的幻方,由于软件窗口限制,无法在表格中完全显示,不过仍然可以复制到剪贴板。"""
readme_btn = Message(right_frame,
text=readme_msg,
justify=LEFT
)
# 放置right_frame及内部组件
degree.grid(row=0, column=0, padx=5, pady=5)
degree_entry.grid(row=0, column=1, padx=5, pady=5)
ms_sum.grid(row=1, column=0, padx=5)
ms_sum_entry.grid(row=1, column=1, padx=5)
generator_btn.grid(row=2, column=0, columnspan=2, padx=5, pady=15, ipady=5, sticky=N)
copy_btn.grid(row=3, column=0, columnspan=2, ipady=5)
readme_btn.grid(row=34, column=0, columnspan=2, padx=5, pady=5)
left_frame.pack(fill=BOTH, padx=5, pady=5, side=LEFT)
right_frame.pack(fill=BOTH, padx=5, pady=5, side=RIGHT)
# 操作部分
def get_magic_square(*args):
"""避免使用局部变量导致的组件无法控制"""
global magic_square, magic_text # magic_grid
"""
# 删除之前的旧label weight
if magic_grid:
for labels in magic_grid.labels:
for label in labels:
label.place_forget()
"""
s = degree_entry.get()
if not s.isnumeric():
return
n = int(s)
if n < 3 or n > 1000:
return
print('阶数:', n)
magic_square = MagicSquareGenerator(n)
magic_text.text.delete(1.0, END) # 删除从第一行到末尾的,如果是0.1那么之前删除的会获取到
magic_text.text.insert(END, magic_square.magic_square_string)
# print(magic_square)
# magic_grid = MsGrid(left_frame, n, magic_square.magic_square, 2, 2)
# magic_grid.labels_place()
ms_sum_var.set(magic_square.ms_sum)
# 初始化数据
magic_square = None # 修改此处为正常幻方
# magic_grid = None
magic_text = MsText(left_frame, 2, 2)
magic_text.text_place()
# 配置生成按钮
generator_btn.config(command=get_magic_square)
degree_entry.bind("<Return>", get_magic_square)
copy_btn.config(command=lambda: magic_text.text.clipboard_append(magic_text.text.get(1.0, END)))
root.mainloop()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yoyoliyang/magic_square_generator.git
[email protected]:yoyoliyang/magic_square_generator.git
yoyoliyang
magic_square_generator
magic_square_generator
master

搜索帮助