[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import ttk
from tkinter import font as tkFont
import random
# 名字库
names = [
"伯庸", "正则", "灵均", "修能", "骐", "峻茂", "信芳", "圣哲", "茂行", "曼路", "望舒", "陆离", "珵美", "云旗", "辰良",
"玉锵", "璆鸣", "安歌", "浩倡", "乐康", "齐光", "宜修", "扬灵", "飞龙", "杜若", "扬波", "既明", "翾飞", "青云", "飞扬",
"诚勇", "坚志", "宝璐", "承宇", "怀信", "怀瑾", "广志", "嘉树", "秉德", "淑离", "翼遥", "休德", "辰星", "淑尤", "耀灵",
"凯风", "琬琰", "冬荣", "博衍", "自清", "昂驹", "绎心", "高翔", "怀德", "显荣", "晏晏", "幼清", "盛德", "静闲", "嫮",
"振理", "慈仁", "贤俊", "逾明", "舒志", "凌恒", "诚贞", "景云", "依斐", "淑清", "嘉月", "庆云", "奋翼", "鸿永", "垂文",
"扬采", "素洁", "妙仪", "菀青", "嘉志", "昭华", "宝璋", "硕明", "俊彦", "清和", "蓁蓁", "燕飞", "淑慎", "惠然", "静姝",
"炜彤", "洵美", "燕婉", "如云", "邦媛", "灵雨", "朝雨", "芃芃", "绿竹", "琇莹", "巧倩", "美盼", "佩玉", "琼琚", "陶陶",
"佩玖", "静好", "舜华", "舜英", "德音", "子佩", "零露", "婉兮", "婉如", "琼华", "美清", "如英", "清猗", "素衣", "菁菁",
"采苓", "文茵", "湄", "渥丹", "美淑", "舒窈", "楚楚", "如雪", "荟蔚", "翕", "依依", "霏霏", "燕绥", "令仪", "闻天",
"金玉", "攸宁", "骏德", "翰飞", "巧颜", "溱溱", "嘉卉", "雅南", "苾芬", "思柔", "英英", "维桢", "缉熙", "鹰扬", "玉瓒",
"思齐", "徽音", "静嘉", "清馨", "柔嘉", "柔惠", "懿德", "穆清", "哲成", "秉文", "德纯", "骏惠", "振鹭", "和铃", "馨宁",
"纯熙", "其琛", "于飞", "羽飞", "雨霏", "思远", "明哲", "卉迟", "济桓", "石玉", "乔木", "修远", "芳华", "芳蔼", "江沅",
"若华", "志致", "志行", "雅醇", "雅歌", "雅厚", "雅亮", "雅胜", "怡然", "弈鸣", "熠然", "懿美", "潆泓", "颖逸", "余馨",
"羽丰", "语心", "悠然", "悠永", "悠柔", "煜月", "韫玉", "悦心", "悦欣", "悦畅", "悦爱", "志尚", "志鹏", "致远", "钟灵",
"钟毓", "朝辉", "周晟"
]
def generate_names(surname, count):
selected_names = random.sample(names, count)
full_names = [surname + name for name in selected_names]
return full_names
class NameGeneratorApp:
def __init__(self, root):
self.root = root
root.title("取名器")
# 定义字体
self.custom_font = tkFont.Font(family="康熙字典体(全字库)", size=16) # 小二号字体大小约为16pt
self.setup_ui()
def setup_ui(self):
# 姓氏输入
self.surname_label = ttk.Label(self.root, text="请输入姓氏:", font=self.custom_font)
self.surname_label.grid(column=0, row=0, padx=10, pady=10)
self.surname_entry = ttk.Entry(self.root, font=self.custom_font)
self.surname_entry.grid(column=1, row=0, padx=10, pady=10)
# 名字个数输入
self.count_label = ttk.Label(self.root, text="请输入要输出名字的个数:", font=self.custom_font)
self.count_label.grid(column=0, row=1, padx=10, pady=10)
self.count_entry = ttk.Entry(self.root, font=self.custom_font)
self.count_entry.grid(column=1, row=1, padx=10, pady=10)
# 生成按钮
self.generate_button = ttk.Button(self.root, text="生成名字", command=self.on_generate_clicked)
self.generate_button.grid(column=0, row=2, columnspan=2, pady=10)
# 输出结果的Text控件
self.result_text = tk.Text(self.root, height=10, width=50, font=self.custom_font)
self.result_text.grid(column=0, row=3, columnspan=2, pady=10, padx=10)
self.result_text.tag_configure('center', justify='center') # 配置一个名为'center'的tag
self.result_text.config(state=tk.DISABLED) # 初始设置为不可编辑状态
def on_generate_clicked(self):
surname = self.surname_entry.get()
try:
count = int(self.count_entry.get())
full_names = generate_names(surname, count)
result_text = "\n".join(full_names)
except ValueError:
result_text = "请输入有效的名字个数!"
except Exception as e:
result_text = f"发生错误:{str(e)}"
# 更新Text控件内容
self.result_text.config(state=tk.NORMAL) # 允许编辑以更新内容
self.result_text.delete('1.0', tk.END) # 清空当前内容
self.result_text.insert(tk.END, result_text) # 插入新的结果
self.result_text.tag_add('center', '1.0', 'end') # 应用'center' tag到所有文本
self.result_text.config(state=tk.DISABLED) # 再次设置为不可编辑状态
def main():
root = tk.Tk()
app = NameGeneratorApp(root)
root.mainloop()
if __name__ == "__main__":
main()