import tkinter as tk import openpyxl as xl from tkinter import ttk from tkinter import messagebox import openpyxl import datetime import os from openpyxl.utils import get_column_letter from openpyxl.styles import Font class ExcelApp: def __init__(self, master): self.master = master master.title("Excel App") # 获取屏幕的宽度和高度 screen_width = master.winfo_screenwidth() screen_height = master.winfo_screenheight() # 将窗口的大小设置为屏幕的大小 master.geometry("%dx%d" % (screen_width, screen_height)) # 创建菜单栏 menubar = tk.Menu(master) master.config(menu=menubar)在這個窗口前在創建一個登錄窗口,儅輸入正確的秘密賬號時才能打開這個窗口,反之就不打開
时间: 2024-01-01 13:04:04 浏览: 95
好的,可以将下面的代码添加到 `ExcelApp` 类的 `__init__` 方法中,以创建登录窗口:
```
# 创建登录窗口
login_window = tk.Toplevel(master)
login_window.title("登录")
login_window.geometry("300x150")
login_window.resizable(0, 0)
# 创建用户名和密码的 Label 和 Entry
tk.Label(login_window, text="用户名:").place(x=50, y=30)
username_entry = tk.Entry(login_window)
username_entry.place(x=110, y=30)
tk.Label(login_window, text="密码:").place(x=50, y=70)
password_entry = tk.Entry(login_window, show="*")
password_entry.place(x=110, y=70)
# 创建登录按钮
def login():
username = username_entry.get()
password = password_entry.get()
if username == "your_username" and password == "your_password":
login_window.destroy()
else:
tk.messagebox.showwarning(title="登录失败", message="用户名或密码错误!")
tk.Button(login_window, text="登录", command=login).place(x=130, y=110)
# 创建 Excel App 窗口
if login_window.wait_window() == 0:
master.destroy()
else:
# 创建菜单栏
menubar = tk.Menu(master)
master.config(menu=menubar)
# 添加菜单项和事件处理函数
file_menu = tk.Menu(menubar, tearoff=0)
file_menu.add_command(label="新建", command=self.new_file)
file_menu.add_command(label="打开", command=self.open_file)
file_menu.add_command(label="保存", command=self.save_file)
file_menu.add_command(label="另存为", command=self.save_file_as)
file_menu.add_separator()
file_menu.add_command(label="退出", command=self.quit)
menubar.add_cascade(label="文件", menu=file_menu)
edit_menu = tk.Menu(menubar, tearoff=0)
edit_menu.add_command(label="剪切", command=self.cut)
edit_menu.add_command(label="复制", command=self.copy)
edit_menu.add_command(label="粘贴", command=self.paste)
menubar.add_cascade(label="编辑", menu=edit_menu)
help_menu = tk.Menu(menubar, tearoff=0)
help_menu.add_command(label="关于", command=self.about)
menubar.add_cascade(label="帮助", menu=help_menu)
# 创建工具栏
toolbar = tk.Frame(master, bd=1, relief=tk.RAISED)
self.new_button = tk.Button(toolbar, text="新建", command=self.new_file)
self.new_button.pack(side=tk.LEFT, padx=2, pady=2)
self.open_button = tk.Button(toolbar, text="打开", command=self.open_file)
self.open_button.pack(side=tk.LEFT, padx=2, pady=2)
self.save_button = tk.Button(toolbar, text="保存", command=self.save_file)
self.save_button.pack(side=tk.LEFT, padx=2, pady=2)
self.cut_button = tk.Button(toolbar, text="剪切", command=self.cut)
self.cut_button.pack(side=tk.LEFT, padx=2, pady=2)
self.copy_button = tk.Button(toolbar, text="复制", command=self.copy)
self.copy_button.pack(side=tk.LEFT, padx=2, pady=2)
self.paste_button = tk.Button(toolbar, text="粘贴", command=self.paste)
self.paste_button.pack(side=tk.LEFT, padx=2, pady=2)
toolbar.pack(side=tk.TOP, fill=tk.X)
# 创建状态栏
statusbar = tk.Label(master, text="就绪", bd=1, relief=tk.SUNKEN, anchor=tk.W)
statusbar.pack(side=tk.BOTTOM, fill=tk.X)
# 创建表格控件
self.sheet_view = SheetView(master)
self.sheet_view.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
```
请将上面的 `your_username` 和 `your_password` 替换为实际的用户名和密码。登录窗口会在 Excel App 窗口前面弹出来,只有在输入正确的用户名和密码后才能进入 Excel App 窗口。登录窗口关闭后,才会创建 Excel App 窗口。如果登录窗口被关闭,则 Excel App 窗口也会随之关闭。
阅读全文