from PyQt5 import uic from PyQt5.QtGui import QWindow from DATA225utils import make_connection class TitanicWindow(QWindow): """ The main application window. """ def __init__(self): """ Load the UI and initialize its components. """ super().__init__() # Load the display. self.ui = uic.loadUi('titanic_window.ui') # Initialize the class menu. self._initialize_class_menu() self.ui.class_menu.currentIndexChanged.connect(self._display_passenger_count) # Display the initial count. self._display_passenger_count() self.ui.show() def _initialize_class_menu(self): """ Initialize the class menu with passenger classes. """ for klass in ['1st', '2nd', '3rd']: self.ui.class_menu.addItem(klass, klass) def _display_passenger_count(self): """ Menu item event handler: Display the count of passengers of the chosen passenger class. """ klass = self.ui.class_menu.currentData() # Query the database to get the passenger count # of the chosen passenger class. conn = make_connection(config_file = 'titanic.ini') cursor = conn.cursor() sql = ( """ SELECT COUNT(*) FROM passengers """ f"WHERE class = '{klass}'" ) cursor.execute(sql) count = cursor.fetchone() cursor.close() conn.close() # Set the count into the text field. self.ui.the_count.setText(f'{count[0]}')