用Python写一个带界面的宿舍管理系统

用Python写一个宿舍管理系统

下面是一个简单的Python宿舍管理系统的例子。这个系统可以添加学生,为学生分配宿舍,以及查看宿舍的学生列表。

```python

class Dormitory:

def \_\_init\_\_(self, name, capacity):

    self.name = name

    self.capacity = capacity

    self.students = \[\]



def add\_student(self, student):

    if len(self.students) < self.capacity:

        self.students.append(student)

        return True

    else:

        return False



def get\_students(self):

    return self.students

class Student:

def \_\_init\_\_(self, name, id):

    self.name = name

    self.id = id

class DormitoryManager:

def \_\_init\_\_(self):

    self.dormitories = \[\]



def add\_dormitory(self, dormitory):

    self.dormitories.append(dormitory)



def allocate\_dormitory(self, student):

    for dormitory in self.dormitories:

        if dormitory.add\_student(student):

            return True

    return False



def get\_dormitory\_students(self, dormitory\_name):

    for dormitory in self.dormitories:

        if dormitory.name == dormitory\_name:

            return dormitory.get\_students()

    return None

# Test the system

manager = DormitoryManager()

# Add dormitories

manager.add_dormitory(Dormitory(“Dormitory 1”, 2))

manager.add_dormitory(Dormitory(“Dormitory 2”, 1))

# Add students

student1 = Student(“Alice”, 1)

student2 = Student(“Bob”, 2)

student3 = Student(“Charlie”, 3)

# Allocate dormitories

manager.allocate_dormitory(student1)

manager.allocate_dormitory(student2)

manager.allocate_dormitory(student3)

# Print students in each dormitory

print(“Students in Dormitory 1:”)

for student in manager.get_dormitory_students(“Dormitory 1”):

print(student.name)

print(“Students in Dormitory 2:”)

for student in manager.get_dormitory_students(“Dormitory 2”):

print(student.name)

```

这个系统有三个类:`Dormitory`,`Student`和`DormitoryManager`。`Dormitory`类表示一个宿舍,它有名字和容量,以及一个学生列表。`Student`类表示一个学生,它有名字和ID。`DormitoryManager`类管理所有的宿舍,它可以添加宿舍,为学生分配宿舍,以及查看宿舍的学生列表。

升级下加个界面:

创建一个图形用户界面(GUI)需要使用到一些库,例如Tkinter(Python的标准GUI库)。下面是一个使用Tkinter创建宿舍管理系统界面的例子:

```python

import tkinter as tk

from tkinter import messagebox

class Dormitory:

def \_\_init\_\_(self, name, capacity):

    self.name = name

    self.capacity = capacity

    self.students = \[\]



def add\_student(self, student):

    if len(self.students) < self.capacity:

        self.students.append(student)

        return True

    else:

        return False



def get\_students(self):

    return self.students

class Student:

def \_\_init\_\_(self, name, id):

    self.name = name

    self.id = id

class DormitoryManager:

def \_\_init\_\_(self):

    self.dormitories = \[\]



def add\_dormitory(self, dormitory):

    self.dormitories.append(dormitory)



def allocate\_dormitory(self, student):

    for dormitory in self.dormitories:

        if dormitory.add\_student(student):

            return True

    return False



def get\_dormitory\_students(self, dormitory\_name):

    for dormitory in self.dormitories:

        if dormitory.name == dormitory\_name:

            return dormitory.get\_students()

    return None

class DormitoryGUI:

def \_\_init\_\_(self, master):

    self.master = master

    self.manager = DormitoryManager()



    self.dormitory\_name\_label = tk.Label(master, text="Dormitory Name")

    self.dormitory\_name\_label.pack()



    self.dormitory\_name\_entry = tk.Entry(master)

    self.dormitory\_name\_entry.pack()



    self.dormitory\_capacity\_label = tk.Label(master, text="Dormitory Capacity")

    self.dormitory\_capacity\_label.pack()



    self.dormitory\_capacity\_entry = tk.Entry(master)

    self.dormitory\_capacity\_entry.pack()



    self.add\_dormitory\_button = tk.Button(master, text="Add Dormitory", command=self.add\_dormitory)

    self.add\_dormitory\_button.pack()



def add\_dormitory(self):

    dormitory\_name = self.dormitory\_name\_entry.get()

    dormitory\_capacity = int(self.dormitory\_capacity\_entry.get())

    dormitory = Dormitory(dormitory\_name, dormitory\_capacity)

    self.manager.add\_dormitory(dormitory)

    messagebox.showinfo("Success", "Dormitory added successfully")

root = tk.Tk()

my_gui = DormitoryGUI(root)

root.mainloop()

```

这个程序创建了一个简单的GUI,用户可以输入宿舍的名字和容量,然后点击"Add Dormitory"按钮添加宿舍。当宿舍添加成功后,会弹出一个消息框显示"Dormitory added successfully"。

点击下方安全链接前往获取

CSDN大礼包:《Python入门&进阶学习资源包》免费分享

Python实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

用Python写一个带界面的宿舍管理系统_第1张图片

用Python写一个带界面的宿舍管理系统_第2张图片

Python书籍和视频合集

观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

用Python写一个带界面的宿舍管理系统_第3张图片

Python副业创收路线

用Python写一个带界面的宿舍管理系统_第4张图片

这些资料都是非常不错的,朋友们如果有需要《Python学习路线&学习资料》,点击下方安全链接前往获取

CSDN大礼包:《Python入门&进阶学习资源包》免费分享

本文转自网络,如有侵权,请联系删除。

你可能感兴趣的:(python,开发语言)