|  | 
 
| 
[ 本帖最后由 千羽丶 于 2024-1-22 18:52 编辑 ]\n\n[code]import tkinter as tk
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  from tkinter import messagebox
 
 # 创建登录窗口
 login_window = tk.Tk()
 login_window.title("学生会管理系统")
 login_window.geometry("260x150")
 
 # 创建用户名和密码的标签和输入框
 username_label = tk.Label(login_window, text="用户名:")
 username_label.grid(row=0, column=0, padx=10, pady=10)  # 设置Label的位置
 
 username_entry = tk.Entry(login_window)
 username_entry.grid(row=0, column=1, padx=10, pady=10)  # 设置Entry的位置
 
 password_label = tk.Label(login_window, text="密码:")
 password_label.grid(row=1, column=0, padx=10, pady=10)  # 设置Label的位置
 
 password_entry = tk.Entry(login_window, show="*")
 password_entry.grid(row=1, column=1, padx=10, pady=10)  # 设置Entry的位置
 
 
 def login():
 username = username_entry.get()  # 获取用户名输入框中的值
 password = password_entry.get()  # 获取密码输入框中的值
 if username == "1" and password == "1":
 # 销毁登录窗口
 login_window.destroy()
 
 # 创建部门主窗口
 department_window = tk.Tk()
 department_window.title("学生会管理系统")
 department_window.geometry("260x270")
 
 # 创建按钮
 button1 = tk.Button(department_window, text="督察部", width=10, height=2)
 button1.grid(row=0, column=0, pady=10, padx=20)
 
 button2 = tk.Button(department_window, text="劳动部", width=10, height=2)
 button2.grid(row=0, column=1, pady=10, padx=20)
 
 button3 = tk.Button(department_window, text="秘书处", width=10, height=2)
 button3.grid(row=1, column=0, pady=10, padx=20)
 
 button4 = tk.Button(department_window, text="文体部", width=10, height=2)
 button4.grid(row=1, column=1, pady=10, padx=20)
 
 button5 = tk.Button(department_window, text="学习部", width=10, height=2)
 button5.grid(row=2, column=0, pady=10, padx=20)
 
 button6 = tk.Button(department_window, text="社会实践部", width=10, height=2)
 button6.grid(row=2, column=1, pady=10, padx=20)
 
 button7 = tk.Button(department_window, text="国旗班", width=10, height=2)
 button7.grid(row=3, column=0, pady=10, padx=20)
 
 button8 = tk.Button(department_window, text="礼仪队", width=10, height=2)
 button8.grid(row=3, column=1, pady=10, padx=20)
 
 # 运行部门主窗口的主循环
 department_window.mainloop()
 else:
 # 登录失败,给出提示信息
 messagebox.showerror("错误", "用户名或密码错误!")
 
 
 # 创建登录按钮
 login_button = tk.Button(login_window, text="登录", command=login, width=10, height=2)
 login_button.grid(row=2, columnspan=2, pady=10)  # 设置Button的位置
 
 # 运行登录窗口的主循环
 login_window.mainloop()[/code]
 
你的代码已经实现了点击部门按钮后打开一个新的页面的功能。接下来,我将对你的代码进行优化,让其更加简洁明了。 
首先,我们可以将部门按钮的创建过程封装成一个函数,以便后续创建其他按钮时复用。代码如下:
 复制代码def create_department_button(window, row, column, text):
    button = tk.Button(window, text=text, width=10, height=2)
    button.grid(row=row, column=column, pady=10, padx=20)
    return button
然后,在登录成功后,我们可以销毁登录窗口并创建部门窗口的过程封装成一个函数,以提高代码的可读性。代码如下:
 复制代码def create_department_window():
    login_window.destroy()
    # 创建部门主窗口
    department_window = tk.Tk()
    department_window.title("学生会管理系统")
    department_window.geometry("260x270")
    # 创建按钮
    button1 = create_department_button(department_window, 0, 0, "督察部")
    button2 = create_department_button(department_window, 0, 1, "劳动部")
    button3 = create_department_button(department_window, 1, 0, "秘书处")
    button4 = create_department_button(department_window, 1, 1, "文体部")
    button5 = create_department_button(department_window, 2, 0, "学习部")
    button6 = create_department_button(department_window, 2, 1, "社会实践部")
    button7 = create_department_button(department_window, 3, 0, "国旗班")
    button8 = create_department_button(department_window, 3, 1, "礼仪队")
    # 运行部门主窗口的主循环
    department_window.mainloop()
最后,在登录按钮的回调函数中,调用`create_department_window()`函数即可。代码如下:
 复制代码def login():
    username = username_entry.get()  # 获取用户名输入框中的值
    password = password_entry.get()  # 获取密码输入框中的值
    if username == "1" and password == "1":
        create_department_window()
    else:
        # 登录失败,给出提示信息
        messagebox.showerror("错误", "用户名或密码错误!")
经过优化后的完整代码如下:
 复制代码import tkinter as tk
from tkinter import messagebox
def create_department_button(window, row, column, text):
    button = tk.Button(window, text=text, width=10, height=2)
    button.grid(row=row, column=column, pady=10, padx=20)
    return button
def create_department_window():
    login_window.destroy()
    # 创建部门主窗口
    department_window = tk.Tk()
    department_window.title("学生会管理系统")
    department_window.geometry("260x270")
    # 创建按钮
    button1 = create_department_button(department_window, 0, 0, "督察部")
    button2 = create_department_button(department_window, 0, 1, "劳动部")
    button3 = create_department_button(department_window, 1, 0, "秘书处")
    button4 = create_department_button(department_window, 1, 1, "文体部")
    button5 = create_department_button(department_window, 2, 0, "学习部")
    button6 = create_department_button(department_window, 2, 1, "社会实践部")
    button7 = create_department_button(department_window, 3, 0, "国旗班")
    button8 = create_department_button(department_window, 3, 1, "礼仪队")
    # 运行部门主窗口的主循环
    department_window.mainloop()
# 创建登录窗口
login_window = tk.Tk()
login_window.title("学生会管理系统")
login_window.geometry("260x150")
# 创建用户名和密码的标签和输入框
username_label = tk.Label(login_window, text="用户名:")
username_label.grid(row=0, column=0, padx=10, pady=10)  # 设置Label的位置
username_entry = tk.Entry(login_window)
username_entry.grid(row=0, column=1, padx=10, pady=10)  # 设置Entry的位置
password_label = tk.Label(login_window, text="密码:")
password_label.grid(row=1, column=0, padx=10, pady=10)  # 设置Label的位置
password_entry = tk.Entry(login_window, show="*")
password_entry.grid(row=1, column=1, padx=10, pady=10)  # 设置Entry的位置
def login():
    username = username_entry.get()  # 获取用户名输入框中的值
    password = password_entry.get()  # 获取密码输入框中的值
    if username == "1" and password == "1":
        create_department_window()
    else:
        # 登录失败,给出提示信息
        messagebox.showerror("错误", "用户名或密码错误!")
# 创建登录按钮
login_button = tk.Button(login_window, text="登录", command=login, width=10, height=2)
login_button.grid(row=2, columnspan=2, pady=10)  # 设置Button的位置
# 运行登录窗口的主循环
login_window.mainloop()
这样,代码看起来更加简洁,也更易于维护。希望对你有所帮助!如果还有其他问题,请随时提问。
 
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 | 
 |