Files
cyzg/components/page1.py
2025-08-21 16:02:08 +08:00

150 lines
4.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import tkinter as tk
import os
import platform
import webbrowser
from tkinter import ttk
from components import *
class Page1:
def __init__(self, root, top_options, bottom_options):
self.root = root
self.top_options = top_options
self.bottom_options = bottom_options
# ========== 顶部复选框区域 ==========
self.top_frame = tk.Frame(self.root, bg=BG_COLOR, pady=25)
self.top_frame.pack(side=tk.TOP, fill=tk.X)
self.top_vars = []
for option in top_options:
var = tk.BooleanVar(value=True)
chk = ttk.Checkbutton(self.top_frame, text=option, variable=var, style="Custom.TCheckbutton")
chk.pack(side=tk.LEFT, padx=20)
self.top_vars.append(var)
# ========== 分割线 ==========
separator = tk.Frame(self.root, height=1, bg="black")
separator.pack(side=tk.TOP, fill=tk.X, padx=20)
# ========== 底部复选框区域 ==========
self.bottom_frame = tk.Frame(self.root, bg=BG_COLOR, pady=25)
self.bottom_frame.pack(side=tk.TOP, fill=tk.X)
self.bottom_vars = []
for option in bottom_options:
var = tk.BooleanVar(value=False)
chk = ttk.Checkbutton(self.bottom_frame, text=option, variable=var, style="Custom.TCheckbutton")
chk.pack(side=tk.LEFT, padx=20)
self.bottom_vars.append(var)
# ========== 说明文本区域 ==========
text_frame = tk.Frame(self.root, bg=BG_COLOR)
text_frame.pack(side=tk.TOP, padx=40, pady=10, fill=tk.X)
text_widget = tk.Text(
text_frame,
height=6,
width=60,
wrap=tk.WORD,
bg=BG_COLOR,
fg=FG_COLOR,
relief=tk.FLAT,
borderwidth=0,
font=(FONT_FAMILY, 12),
padx=10,
pady=10
)
text_widget.pack(expand=True, fill=tk.X)
text_widget.insert(tk.END, instructions)
text_widget.config(state=tk.DISABLED)
# just for fun
url = "https://v.zikai.wang/zikai/cyzg"
start_pos = instructions.find(url)
if start_pos != -1:
# 计算起始和结束索引tkinter 使用 "行.列" 格式)
start_index = f"1.0 + {start_pos} chars"
end_index = f"{start_index} + {len(url)} chars"
# 创建一个超链接标签
text_widget.tag_add("link", start_index, end_index)
text_widget.tag_config("link",
foreground="blue",
underline=True,
font=(FONT_FAMILY, 12, "underline"))
# 定义点击事件
def open_link(event):
try:
webbrowser.open(url)
except Exception as e:
print(e)
try:
os_name = platform.system()
if os_name == "Windows":
os.system(f'echo {url} | clip')
elif os_name == "Darwin":
os.system(f'echo "{url}" | pbcopy')
else:
os.system(f'echo "{url}" | xclip -selection clipboard')
except Exception as e:
print(e)
def on_enter(event):
text_widget.config(cursor="hand2")
def on_leave(event):
text_widget.config(cursor="")
# 绑定事件
text_widget.tag_bind("link", "<Button-1>", open_link) # 左键点击
text_widget.tag_bind("link", "<Enter>", on_enter) # 鼠标进入
text_widget.tag_bind("link", "<Leave>", on_leave) # 鼠标离开
def top_selected(self):
"""返回第一行中被选中的项"""
return [opt for opt, var in zip(self.top_options, self.top_vars) if var.get()]
def bottom_selected(self):
"""返回第二行中被选中的项"""
return [opt for opt, var in zip(self.bottom_options, self.bottom_vars) if var.get()]
if __name__ == "__main__":
top_options = ["1", "2", "3", "4"]
bottom_options = ["未做过的题", "错过的题", ]
root = tk.Tk()
app = Page1(root, top_options, bottom_options)
# 按钮区域
button_frame = tk.Frame(root)
button_frame.pack(side=tk.TOP, pady=20)
# 打印上一行选中项的按钮
btn_print_top = tk.Button(
button_frame,
text="1",
command=lambda: print(app.top_selected()),
font=("Helvetica", 14),
bg="lightblue",
width=20
)
btn_print_top.pack(side=tk.LEFT, padx=10)
# 打印下一行选中项的按钮
btn_print_bottom = tk.Button(
button_frame,
text="2",
command=lambda: print(app.bottom_selected()),
font=("Helvetica", 14),
bg="lightgreen",
width=20
)
btn_print_bottom.pack(side=tk.LEFT, padx=10)
root.mainloop()