鱼C论坛

 找回密码
 立即注册
查看: 1008|回复: 1

[已解决]点击部门按钮,打开一个新的页面

[复制链接]
发表于 2024-1-22 18:49:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
[ 本帖最后由 千羽丶 于 2024-1-22 18:52 编辑 ]\n\n[code]import tkinter as tk
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]
最佳答案
2024-1-22 18:51:20
你的代码已经实现了点击部门按钮后打开一个新的页面的功能。接下来,我将对你的代码进行优化,让其更加简洁明了。

首先,我们可以将部门按钮的创建过程封装成一个函数,以便后续创建其他按钮时复用。代码如下:

  1. def create_department_button(window, row, column, text):
  2.     button = tk.Button(window, text=text, width=10, height=2)
  3.     button.grid(row=row, column=column, pady=10, padx=20)
  4.     return button
复制代码


然后,在登录成功后,我们可以销毁登录窗口并创建部门窗口的过程封装成一个函数,以提高代码的可读性。代码如下:

  1. def create_department_window():
  2.     login_window.destroy()

  3.     # 创建部门主窗口
  4.     department_window = tk.Tk()
  5.     department_window.title("学生会管理系统")
  6.     department_window.geometry("260x270")

  7.     # 创建按钮
  8.     button1 = create_department_button(department_window, 0, 0, "督察部")
  9.     button2 = create_department_button(department_window, 0, 1, "劳动部")
  10.     button3 = create_department_button(department_window, 1, 0, "秘书处")
  11.     button4 = create_department_button(department_window, 1, 1, "文体部")
  12.     button5 = create_department_button(department_window, 2, 0, "学习部")
  13.     button6 = create_department_button(department_window, 2, 1, "社会实践部")
  14.     button7 = create_department_button(department_window, 3, 0, "国旗班")
  15.     button8 = create_department_button(department_window, 3, 1, "礼仪队")

  16.     # 运行部门主窗口的主循环
  17.     department_window.mainloop()
复制代码


最后,在登录按钮的回调函数中,调用`create_department_window()`函数即可。代码如下:

  1. def login():
  2.     username = username_entry.get()  # 获取用户名输入框中的值
  3.     password = password_entry.get()  # 获取密码输入框中的值
  4.     if username == "1" and password == "1":
  5.         create_department_window()
  6.     else:
  7.         # 登录失败,给出提示信息
  8.         messagebox.showerror("错误", "用户名或密码错误!")
复制代码


经过优化后的完整代码如下:

  1. import tkinter as tk
  2. from tkinter import messagebox

  3. def create_department_button(window, row, column, text):
  4.     button = tk.Button(window, text=text, width=10, height=2)
  5.     button.grid(row=row, column=column, pady=10, padx=20)
  6.     return button

  7. def create_department_window():
  8.     login_window.destroy()

  9.     # 创建部门主窗口
  10.     department_window = tk.Tk()
  11.     department_window.title("学生会管理系统")
  12.     department_window.geometry("260x270")

  13.     # 创建按钮
  14.     button1 = create_department_button(department_window, 0, 0, "督察部")
  15.     button2 = create_department_button(department_window, 0, 1, "劳动部")
  16.     button3 = create_department_button(department_window, 1, 0, "秘书处")
  17.     button4 = create_department_button(department_window, 1, 1, "文体部")
  18.     button5 = create_department_button(department_window, 2, 0, "学习部")
  19.     button6 = create_department_button(department_window, 2, 1, "社会实践部")
  20.     button7 = create_department_button(department_window, 3, 0, "国旗班")
  21.     button8 = create_department_button(department_window, 3, 1, "礼仪队")

  22.     # 运行部门主窗口的主循环
  23.     department_window.mainloop()

  24. # 创建登录窗口
  25. login_window = tk.Tk()
  26. login_window.title("学生会管理系统")
  27. login_window.geometry("260x150")

  28. # 创建用户名和密码的标签和输入框
  29. username_label = tk.Label(login_window, text="用户名:")
  30. username_label.grid(row=0, column=0, padx=10, pady=10)  # 设置Label的位置

  31. username_entry = tk.Entry(login_window)
  32. username_entry.grid(row=0, column=1, padx=10, pady=10)  # 设置Entry的位置

  33. password_label = tk.Label(login_window, text="密码:")
  34. password_label.grid(row=1, column=0, padx=10, pady=10)  # 设置Label的位置

  35. password_entry = tk.Entry(login_window, show="*")
  36. password_entry.grid(row=1, column=1, padx=10, pady=10)  # 设置Entry的位置

  37. def login():
  38.     username = username_entry.get()  # 获取用户名输入框中的值
  39.     password = password_entry.get()  # 获取密码输入框中的值
  40.     if username == "1" and password == "1":
  41.         create_department_window()
  42.     else:
  43.         # 登录失败,给出提示信息
  44.         messagebox.showerror("错误", "用户名或密码错误!")

  45. # 创建登录按钮
  46. login_button = tk.Button(login_window, text="登录", command=login, width=10, height=2)
  47. login_button.grid(row=2, columnspan=2, pady=10)  # 设置Button的位置

  48. # 运行登录窗口的主循环
  49. login_window.mainloop()
复制代码


这样,代码看起来更加简洁,也更易于维护。希望对你有所帮助!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-1-22 18:51:20 | 显示全部楼层    本楼为最佳答案   
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 16:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表