- import tkinter as tk
- import ctypes
- #告诉操作系统使用程序自身的dpi适配
- ctypes.windll.shcore.SetProcessDpiAwareness(1)
- #获取屏幕的缩放因子
- ScaleFactor=ctypes.windll.shcore.GetScaleFactorForDevice(0)
- # 创建主窗口
- root = tk.Tk()
- root.title("GM第二届欢唱活动 关键词检测")
- #设置程序缩放
- #这里的root是你设定的窗口
- root.tk.call('tk', 'scaling', ScaleFactor/54)
- # 定义关键词列表
- keywords = ["春", "夏", "秋", "冬", "东", "南", "西", "北",
- "风", "花", "雪", "月", "阴", "晴", "雨", "雾",
- "天", "长", "地", "久", "电", "光", "石", "火",
- "男", "欢", "女", "爱", "百", "转", "千", "回",
- "高", "山", "流", "水", "世", "界", "大", "同"]
- def check_keywords():
- # 获取输入的文本
- text = text_entry.get("1.0", tk.END)
- # 将文本分割成多行
- lines = text.splitlines()
- # 检查每一行是否包含关键词
- found_keywords = set(keyword for line in lines for keyword in keywords if keyword in line)
- # 在输出框中显示找到的关键词
- output_text.delete(1.0, tk.END)
- output_text.insert(tk.END, ', '.join(found_keywords))
- # 获取额外的关键词
- extra_keywords = [entry.get() for entry in extra_entries]
- # 检查额外的关键词是否在找到的关键词中
- found_extra_keywords = [keyword for keyword in extra_keywords if keyword in found_keywords]
- # 在第二个输出框中显示找到的额外关键词
- extra_output_text.delete(1.0, tk.END)
- extra_output_text.insert(tk.END, f"找到了 {len(found_extra_keywords)} 个额外的关键词: {', '.join(found_extra_keywords)}")
- # 创建输入框标题
- text_entry_label = tk.Label(root, text="将歌词粘贴到此处:")
- text_entry_label.grid(row=0, column=0, columnspan=3, sticky='w', padx=5, pady=5)
- # 创建输入框
- text_entry = tk.Text(root, height=12)
- text_entry.grid(row=1, column=0, columnspan=3, sticky='ew', padx=5, pady=5)
- # 创建额外输入框标题
- extra_entries_label = tk.Label(root, text="你的3个关键词:")
- extra_entries_label.grid(row=2, column=0, columnspan=3, sticky='w', padx=5, pady=5)
- # 创建3个额外的输入框
- extra_entries = [tk.Entry(root) for _ in range(3)]
- for i, entry in enumerate(extra_entries):
- entry.grid(row=3, column=i, sticky='ew', padx=5, pady=5)
- # 创建按钮,点击时会调用check_keywords函数
- check_button = tk.Button(root, text="检查关键词", command=check_keywords)
- check_button.grid(row=4, column=0, columnspan=3, sticky='ew', padx=5, pady=5)
- # 创建输出框标题
- output_text_label = tk.Label(root, text="你的歌词中包含下列关键词:")
- output_text_label.grid(row=5, column=0, columnspan=3, sticky='w', padx=5, pady=5)
- # 创建输出框
- output_text = tk.Text(root, height=5)
- output_text.grid(row=6, column=0, columnspan=3, sticky='ew', padx=5, pady=5)
- # 创建第二个输出框
- extra_output_text = tk.Text(root, height=5)
- extra_output_text.grid(row=7, column=0, columnspan=3, sticky='ew', padx=5, pady=5)
- # 设置列宽度自动调整
- for i in range(3):
- root.columnconfigure(i, weight=1)
- # 运行主循环
- root.mainloop()
复制代码 |