import tkinter as tk
class JapaneseTable:
def __init__(self, root):
self.root = root
self.root.title("五十音图")
# 设置窗口属性
self.root.overrideredirect(True) # 去除边框
self.root.attributes("-topmost", True) # 默认置顶
self.root.attributes("-alpha", 0.95) # 透明度
self.root.geometry("500x400+100+100") # 窗口大小和位置
# 添加拖动功能
self.drag_data = {"x": 0, "y": 0}
self.root.bind("<Button-1>", self.start_drag)
self.root.bind("<B1-Motion>", self.drag_window)
# 创建布局
self.create_widgets()
# 绑定右键菜单
self.root.bind("<Button-3>", self.show_menu)
def create_widgets(self):
# 平假名、片假名和罗马音数据(按行排列)
hiragana = [
['あ', 'い', 'う', 'え', 'お'],
['か', 'き', 'く', 'け', 'こ'],
['さ', 'し', 'す', 'せ', 'そ'],
['た', 'ち', 'つ', 'て', 'と'],
['な', 'に', 'ぬ', 'ね', 'の'],
['は', 'ひ', 'ふ', 'へ', 'ほ'],
['ま', 'み', 'む', 'め', 'も'],
['や', 'い', 'ゆ', 'え', 'よ'],
['ら', 'り', 'る', 'れ', 'ろ'],
['わ', 'い', 'う', 'え', 'を']
]
katakana = [
['ア', 'イ', 'ウ', 'エ', 'オ'],
['カ', 'キ', 'ク', 'ケ', 'コ'],
['サ', 'シ', 'ス', 'セ', 'ソ'],
['タ', 'チ', 'ツ', 'テ', 'ト'],
['ナ', 'ニ', 'ヌ', 'ネ', 'ノ'],
['ハ', 'ヒ', 'フ', 'ヘ', 'ホ'],
['マ', 'ミ', 'ム', 'メ', 'モ'],
['ヤ', 'イ', 'ユ', 'エ', 'ヨ'],
['ラ', 'リ', 'ル', 'レ', 'ロ'],
['ワ', 'イ', 'ウ', 'エ', 'ヲ']
]
# 罗马音数据
romaji = [
['a', 'i', 'u', 'e', 'o'],
['ka', 'ki', 'ku', 'ke', 'ko'],
['sa', 'shi', 'su', 'se', 'so'],
['ta', 'chi', 'tsu', 'te', 'to'],
['na', 'ni', 'nu', 'ne', 'no'],
['ha', 'hi', 'fu', 'he', 'ho'],
['ma', 'mi', 'mu', 'me', 'mo'],
['ya', 'i', 'yu', 'e', 'yo'],
['ra', 'ri', 'ru', 're', 'ro'],
['wa', 'i', 'u', 'e', 'wo']
]
# 创建表格
for i in range(10):
for j in range(5):
# 创建单元格容器
cell_frame = tk.Frame(self.root, bg="#f8f8f8", bd=1, relief="solid")
cell_frame.grid(row=i, column=j, padx=2, pady=2, sticky="nsew")
# 平假名(大号)
hira_label = tk.Label(cell_frame, text=hiragana[i][j],
font=("Arial Unicode MS", 16), # 使用通用字体
bg="#f8f8f8", fg="#000000")
hira_label.pack(side="top", fill="x", pady=(5, 0))
# 片假名(小号)
kata_label = tk.Label(cell_frame, text=katakana[i][j],
font=("Arial Unicode MS", 12), # 使用通用字体
bg="#f8f8f8", fg="#555555")
kata_label.pack(side="top", fill="x", pady=(2, 0))
# 罗马音(更小号)
romaji_label = tk.Label(cell_frame, text=romaji[i][j],
font=("Arial Unicode MS", 10), # 使用通用字体
bg="#f8f8f8", fg="#0000ff")
romaji_label.pack(side="top", fill="x", pady=(0, 5))
# 设置行高
self.root.grid_rowconfigure(i, weight=1)
# 设置列宽
for j in range(5):
self.root.grid_columnconfigure(j, weight=1)
# 添加控制按钮
control_frame = tk.Frame(self.root, bg="#f8f8f8")
control_frame.grid(row=10, column=0, columnspan=5, sticky="ew")
# 置顶按钮
self.pin_button = tk.Button(control_frame, text="置顶",
command=self.toggle_pin,
font=("Arial", 10), width=5)
self.pin_button.pack(side="left", padx=5, pady=5)
# 关闭按钮
close_button = tk.Button(control_frame, text="关闭",
command=self.root.destroy,
font=("Arial", 10), width=5)
close_button.pack(side="right", padx=5, pady=5)
def toggle_pin(self):
"""切换置顶状态"""
self.root.attributes("-topmost", not self.root.attributes("-topmost"))
self.pin_button.config(text="取消置顶" if self.root.attributes("-topmost") else "置顶")
def show_menu(self, event):
"""显示右键菜单"""
menu = tk.Menu(self.root, tearoff=0)
menu.add_command(label="切换置顶", command=self.toggle_pin)
menu.add_separator()
menu.add_command(label="关闭", command=self.root.destroy)
menu.tk_popup(event.x_root, event.y_root)
def start_drag(self, event):
"""开始拖动"""
self.drag_data["x"] = event.x
self.drag_data["y"] = event.y
def drag_window(self, event):
"""拖动窗口"""
x = self.root.winfo_rootx() + (event.x - self.drag_data["x"])
y = self.root.winfo_rooty() + (event.y - self.drag_data["y"])
self.root.geometry(f"+{x}+{y}")
if __name__ == "__main__":
root = tk.Tk()
app = JapaneseTable(root)
root.mainloop()这个代码现在有以下几个问题需要修复:1、只显示平假没有显示片假和罗马音。2、颜色显示完全没有。3、优化面板,酌情添加其他功能,确保主要功能正常。4、让字体变得更加好看,面板更加好看舒适。5、让我能够在看文档的同时能够参考这个五十音图学习日语,这个是我的主要目的