コード例 #1
0
ファイル: main.py プロジェクト: sjtryba/seating-chart
class MainScreen(BoxLayout):
    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)    # Call the superclass __init__()
        self.students = {}  # Start a dictionary for all of the students

        self.student_drop_down = DropDown() # Make a drop down for the student screens
        self.ids.student_screen.ids.student_drop_down_button.bind(on_release=self.student_drop_down.open)
        self.student_drop_down.bind(on_select=lambda instance, x: setattr(self.ids.student_screen.ids.student_drop_down_button, "text", x))

    def save_student(self):
        name = self.ids.student_screen.ids.student_name.text

        property_values = []    # Pull all of the properties from the screen and store them in a list
        property_values.append(self.ids.student_screen.ids.close_to_board)
        property_values.append(self.ids.student_screen.ids.reading.value)
        property_values.append(self.ids.student_screen.ids.writing.value)
        property_values.append(self.ids.student_screen.ids.math.value)
        property_values.append(self.ids.student_screen.ids.science.value)
        property_values.append(self.ids.student_screen.ids.chattiness.value)
        property_values.append(self.ids.student_screen.ids.focus.value)
        property_values.append(self.ids.student_screen.ids.safety.value)

        if name in self.students:   # If the student is already in the dictionary...
            for i, prop in enumerate(self.students[name].properties):   # Go through each property...
                setattr(self.students[name], prop, property_values[i])  # and set the property to the right value

        else:   # If the student is not already in the dictionary...
            student = Student1()    # Make a new student screen object
            student.name = name     # Set the name of the student

            for i, prop in enumerate(student.properties):   # Go through each property...
                setattr(student, prop, property_values[i])  # and set the property to the right value

            self.ids.screen_manager.add_widget(student)    # Add the student screen to the screen manager
            self.add_student_button(student.name, self.ids.distractions_screen) # Make and add a button to the distractions screens
            self.add_student_button(student.name, self.ids.conflicts_screen)    # Make and add a button to the conflicts screens
            self.add_student_to_drop_down(student.name)     # Add the student to the drop down of students
            self.students[student.name] = student           # Add the student screen to the dictionary of students

    def add_student_button(self, name, screen):
        btn_left = ToggleButton()       # Make a button for the left side of the screens
        btn_left.text = name            # Set the text of the button to the students name
        btn_left.size_hint_y = None     # Change the vertical height of the button
        btn_left.height = 40            # Set the height of the button
        btn_left.group = screen.name    # Set the group of the button

        btn_right = ToggleButton()      # Make a button for the right side of the screens
        btn_right.text = name           # Set the text of the button to the students name
        btn_right.size_hint_y = None    # Change the vertical height of the button
        btn_right.height = 40           # Set the height of the button

        screen.ids.left.add_widget(btn_left)                # Add the button to the left side of the screen
        screen.ids.right.add_widget(btn_right)              # Add the button to the left side of the screen

    def add_student_to_drop_down(self, name):
        btn_drop_down = Button()  # Make a button for the drop down menus
        btn_drop_down.text = name  # Set the text of the button to the students name
        btn_drop_down.size_hint_y = None  # Change the vertical height of the button
        btn_drop_down.height = 40  # Set the height of the button
        btn_drop_down.bind(on_release=lambda btn_drop_down: self.student_drop_down.select(btn_drop_down.text))

        self.student_drop_down.add_widget(btn_drop_down)    # Add the button to the drop down

    def create_student_drop_down(self):
        # Clear any buttons from the current drop down
        self.student_drop_down.clear_widget()

        # Create the drop down that shows all of the students
        for key in self.students:
            btn = Button()
            btn.text = key

            btn.size_hint_y = None
            btn.height = 40
            btn.bind(on_release=lambda btn: self.student_drop_down.select(btn.text))

    def display_student_drop_down(self):
        pass