You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
11 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from tkinter import *
from dao.UserDao import UserDao
from service.StudentService import *
from service.UserService import UserService
class StudentUI:
root_window = Tk() # 根容器
left_frame = None # 左侧容器
right_frame = None # 右侧容器
list_entry = [] # 存放Entry组件用于参数传递和清空按钮的触发
@staticmethod
def init_login():
StudentUI.clear_frame(StudentUI.root_window) # 清空容器
StudentUI.root_window.title("登录界面")
width = 220 # 窗口的宽度
height = 140 # 窗口的高度
screen_width = StudentUI.root_window.winfo_screenwidth() # 获得屏幕宽度
screen_height = StudentUI.root_window.winfo_screenheight() # 获得屏幕高度
size_geo = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
(screen_height - height) / 2)
StudentUI.root_window.geometry(size_geo) # 设置窗口的大小、位置(居中)
label_1 = Label(StudentUI.root_window, text="用户名:") # 定义组件
label_2 = Label(StudentUI.root_window, text="密码:")
entry_1 = Entry(StudentUI.root_window, bd=5)
entry_2 = Entry(StudentUI.root_window, bd=5, show="*")
label_3 = Label(StudentUI.root_window, text="") # 为美化界面
label_4 = Label(StudentUI.root_window, text="")
label_1.grid(row=1, column=0) # 以网格布局的形式加入到root_window中
entry_1.grid(row=1, column=1)
label_3.grid(row=2, column=0)
label_2.grid(row=3, column=0)
entry_2.grid(row=3, column=1)
label_4.grid(row=4, column=0)
button_1 = Button(StudentUI.root_window, text="登录", command=lambda: StudentUI.submit_button(
lambda: UserService.login_dispose(entry_1.get(), entry_2.get()), StudentUI.init_root)) # 按钮
button_2 = Button(StudentUI.root_window, text="注册", command=StudentUI.init_register)
button_1.grid(row=5, column=0)
button_2.grid(row=5, column=1)
StudentUI.root_window.mainloop()
@staticmethod
def init_register():
StudentUI.clear_frame(StudentUI.root_window) # 清空容器
StudentUI.root_window.title("注册界面")
width = 220 # 窗口的宽度
height = 140 # 窗口的高度
screen_width = StudentUI.root_window.winfo_screenwidth() # 获得屏幕宽度
screen_height = StudentUI.root_window.winfo_screenheight() # 获得屏幕高度
size_geo = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
(screen_height - height) / 2)
StudentUI.root_window.geometry(size_geo) # 设置窗口的大小、位置(居中)
label_1 = Label(StudentUI.root_window, text="用户名:") # 定义组件
label_2 = Label(StudentUI.root_window, text="密码:")
entry_1 = Entry(StudentUI.root_window, bd=5)
entry_2 = Entry(StudentUI.root_window, bd=5, show="*")
label_3 = Label(StudentUI.root_window, text="") # 为美化界面
label_4 = Label(StudentUI.root_window, text="")
label_1.grid(row=1, column=0) # 以网格布局的形式加入到root_window中
entry_1.grid(row=1, column=1)
label_3.grid(row=2, column=0)
label_2.grid(row=3, column=0)
entry_2.grid(row=3, column=1)
label_4.grid(row=4, column=0)
button_1 = Button(StudentUI.root_window, text="提交", command=lambda: StudentUI.submit_button(
lambda: UserService.register_dispose(entry_1.get(), entry_2.get()), StudentUI.init_login)) # 按钮
button_2 = Button(StudentUI.root_window, text="返回登录", command=StudentUI.init_login)
button_1.grid(row=5, column=0)
button_2.grid(row=5, column=1)
StudentUI.root_window.mainloop()
@staticmethod
def init_root():
StudentUI.clear_frame(StudentUI.root_window) # 清空容器
StudentUI.root_window.title("学生信息管理系统") # 设置标题
width = 700 # 窗口的宽度
height = 400 # 窗口的高度
screen_width = StudentUI.root_window.winfo_screenwidth() # 获得屏幕宽度
screen_height = StudentUI.root_window.winfo_screenheight() # 获得屏幕高度
size_geo = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
(screen_height - height) / 2)
StudentUI.root_window.geometry(size_geo) # 设置窗口的大小、位置(居中)
StudentUI.left_frame = Frame(StudentUI.root_window)
StudentUI.right_frame = Frame(StudentUI.root_window)
StudentUI.left_frame.place(width=width / 3, height=height) # 设置左侧容器
StudentUI.right_frame.place(width=width / 3 * 2, height=height, x=width / 3) # 设置右侧容器
menu = Menu(StudentUI.root_window) # 添加菜单
# tearoff=0是关闭第一行虚线
menu_1 = Menu(menu, tearoff=0)
menu.add_cascade(label="功能", menu=menu_1)
menu_2 = Menu(menu, tearoff=0)
menu.add_cascade(label="文件", menu=menu_2)
menu_3 = Menu(menu, tearoff=0)
menu.add_cascade(label="帮助", menu=menu_3)
menu_1.add_cascade(label="添加学生", command=StudentUI.add_student) # 添加菜单项
menu_1.add_cascade(label="删除学生", command=StudentUI.delete_student)
menu_1.add_cascade(label="修改学生", command=StudentUI.update_student)
menu_2.add_cascade(label="保存为Excel", command=StudentService.to_excel)
menu_3.add_cascade(label="关于", command=StudentUI.about)
StudentUI.root_window["menu"] = menu # 将菜单加入根容器
StudentUI.add_student() # 初始化默认界面
StudentUI.query_all()
StudentUI.root_window.mainloop() # 显示窗口
@staticmethod
def clear_frame(frame):
for widget in frame.winfo_children(): # 清空frame,防止组件重叠
widget.destroy()
if frame != StudentUI.right_frame:
StudentUI.list_entry.clear() # 清空列表
@staticmethod
def clear_entry():
for i in range(len(StudentUI.list_entry)):
StudentUI.list_entry[i].delete(0, 'end')
return True
@staticmethod
def add_student():
StudentUI.clear_frame(StudentUI.left_frame) # 清空容器
list_name = ['学号:', '姓名:', '性别:', '年龄:', '电话:', '宿舍号:'] # 标签列表
for i in range(len(list_name)): # 将组件实例化、加入到容器中
label = Label(StudentUI.left_frame, text=list_name[i])
entry = Entry(StudentUI.left_frame, bd=5)
label.grid(row=i, column=0)
entry.grid(row=i, column=1)
StudentUI.list_entry.append(entry)
button_1 = Button(StudentUI.left_frame, text="提交",
command=lambda: StudentUI.submit_button(
lambda: StudentService.add_student(StudentUI.list_entry),
StudentUI.query_all))
button_2 = Button(StudentUI.left_frame, text="清除",
command=lambda: StudentUI.clear_entry())
button_1.grid(row=len(list_name), column=0)
button_2.grid(row=len(list_name), column=1)
@staticmethod
def delete_student():
StudentUI.clear_frame(StudentUI.left_frame) # 清空容器
label = Label(StudentUI.left_frame, text="学号:")
label.grid(row=0, column=0)
entry = Entry(StudentUI.left_frame, bd=5)
entry.grid(row=0, column=1)
StudentUI.list_entry.append(entry)
button_1 = Button(StudentUI.left_frame, text="提交", command=lambda: StudentUI.submit_button(
lambda: StudentService.delete_student(entry.get()),
StudentUI.query_all))
button_2 = Button(StudentUI.left_frame, text="清除", command=lambda: StudentUI.clear_entry())
button_1.grid(row=1, column=0)
button_2.grid(row=1, column=1)
@staticmethod
def update_student():
StudentUI.clear_frame(StudentUI.left_frame) # 清空容器
label = Label(StudentUI.left_frame, text='学号')
entry = Entry(StudentUI.left_frame, bd=5)
label.grid(row=0, column=0)
entry.grid(row=0, column=1)
button_1 = Button(StudentUI.left_frame, text="查询",
command=lambda: StudentUI.submit_button(StudentUI.clear_entry,
lambda: StudentService.query_student(entry.get(),
StudentUI.list_entry)))
button_2 = Button(StudentUI.left_frame, text="清除", command=lambda: entry.delete(0, 'end'))
button_1.grid(row=1, column=0)
button_2.grid(row=1, column=1)
list_name = ['姓名:', '性别:', '年龄:', '电话:', '宿舍号:']
for i in range(len(list_name)): # 将组件实例化、加入到容器中
label_2 = Label(StudentUI.left_frame, text=list_name[i])
entry_2 = Entry(StudentUI.left_frame, bd=5)
label_2.grid(row=i + 2, column=0) # 前面的学号和查询按钮占了两行所以i要加2
entry_2.grid(row=i + 2, column=1)
StudentUI.list_entry.append(entry_2)
button_1 = Button(StudentUI.left_frame, text="提交",
command=lambda: StudentUI.submit_button(
lambda: StudentService.update_student(entry.get(), StudentUI.list_entry),
StudentUI.query_all))
button_2 = Button(StudentUI.left_frame, text="清除", command=lambda: StudentUI.clear_entry())
button_1.grid(row=len(list_name) + 2, column=0)
button_2.grid(row=len(list_name) + 2, column=1)
@staticmethod
def query_all():
StudentUI.clear_frame(StudentUI.right_frame) # 清空容器
scrollBary = Scrollbar(StudentUI.right_frame, orient=VERTICAL) # 滚动条初始化scrollBary为垂直滚动条scrollBarx为水平滚动条
scrollBarx = Scrollbar(StudentUI.right_frame, orient=HORIZONTAL)
scrollBary.pack(side=RIGHT, fill=Y) # 靠右充满Y轴
scrollBarx.pack(side=BOTTOM, fill=X) # 靠下充满X轴
lb = Listbox(StudentUI.right_frame, width=111, height=32, )
lb.pack()
scrollBary.config(command=lb.yview) # 滚动条与页面内容的位置同步
scrollBarx.config(command=lb.xview)
# 调用方法
StudentService.query_all(lb)
@staticmethod
def submit_button(method_1, method_2):
if method_1() is not None:
method_2()
@staticmethod
def about():
showinfo(title="关于", message="en~~~~\n好像没啥可以说的....\n")