# 引用模块
import tkinter as tk
from tkinter import ttk
#引用文件
import log
import animation_page as act_plot
# 应用主窗口类
class MainWindow(tk.Tk):
def __init__(self):
super().__init__()
root = self
root.title("Frame Switch with Grid Example")
# 创建一个框架,作为按钮命令栏
button_frame = tk.Frame(root)
# 创建两个按钮来控制框架的显示,并将它们放置在网格的底部
button1 = tk.Button(button_frame, text="Show Frame 1", command=lambda: self.show_frame(frame1))
button2 = tk.Button(button_frame, text="Show Frame 2", command=lambda: self.show_frame(frame2))
button3 = tk.Button(button_frame, text="Show Frame 3", command=lambda: self.show_frame(frame2))
# 摆放 按钮
# 使用 grid_rowconfigure 和 grid_columnconfigure 来确保框架填充整个容器
button1.grid(row=0, column=0, sticky="nsew")
button2.grid(row=0, column=1, sticky="nsew")
button3.grid(row=0, column=2, sticky="nsew")
button_frame.grid_rowconfigure(0, weight=0)# 这里 weight=0 因为我们不想让按钮行扩展
button_frame.grid_columnconfigure(0, weight=1)
button_frame.grid_columnconfigure(1, weight=1)
# 创建两个框架,作为容器
frame1 = tk.Frame(root)
frame2 = tk.Frame(root)
# 存为 列表
self.frame_ls = list()
self.frame_ls.append(frame1)
self.frame_ls.append(frame2)
self.current_frame = frame1
# 在每个框架中添加一些标签作为示例内容
label1 = tk.Label(frame1, text="This is Frame 1")
label1.pack(pady=20, padx=20)
label2 = tk.Label(frame2, text="This is Frame 2")
label2.pack(pady=20, padx=20)
# 放置 命令栏
button_frame.grid(row=0, column=0, sticky="nsew", padx=10, pady=10)
# 初始时只将第一个框架放置在网格中
self.show_frame(frame1)
# 使用 grid_rowconfigure 和 grid_columnconfigure 来确保框架填充整个窗口
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)
#*********************************************************************
# 创建菜单栏
menubar = tk.Menu(root)
# 创建文件菜单(主菜单项)
file_menu = tk.Menu(menubar, tearoff=0)
file_menu.add_command(label="新建", command=self.on_menu_fun)
file_menu.add_command(label="打开", command=self.on_menu_fun)
# 添加分隔符
file_menu.add_separator()
# 添加退出选项(通常这里会是退出程序的命令,但为了简单起见,我们省略了它)
# file_menu.add_command(label="退出", command=root.quit)
# 将文件菜单添加到菜单栏
menubar.add_cascade(label="文件", menu=file_menu)
# 创建编辑菜单(主菜单项)
edit_menu = tk.Menu(menubar, tearoff=0)
edit_menu.add_command(label="剪切", command=self.on_menu_fun)
edit_menu.add_command(label="复制", command=self.on_menu_fun)
edit_menu.add_command(label="粘贴", command=self.on_menu_fun)
# 将编辑菜单添加到菜单栏
menubar.add_cascade(label="编辑", menu=edit_menu)
# 将菜单栏配置到主窗口
root.config(menu=menubar)
def show_frame(self,show_frame):
self.current_frame = show_frame
# 隐藏其他框架
for each_frame in self.frame_ls:
each_frame.grid_remove()
# 显示当前框架(如果之前被隐藏了的话)
if not self.current_frame.winfo_viewable():
self.current_frame.grid(row=1, column=0, sticky="nsew", padx=10, pady=10)# 相同 columnspan=2,
def on_menu_fun(self):
print("menu fun")
if __name__ == "__main__":
root = MainWindow()
# 获取屏幕的宽度和高度
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# 设置窗口的尺寸与屏幕相同
root.geometry(f"{screen_width}x{screen_height}")
root.mainloop()