Basic app re-structured¶
Introduction¶
This app is going to be exactly like pyside6_example_1
from the previous page except that code will be broken into
a set of files in the following directory structure.
menus/
file_menu.py
help_menu.py
menu_bar.py
panels/
bottom_panel.py
main_panel.py
root_panel.py
top_panel.py
main.py
main_window.py
Complete code for it available here.
Once installed using pipx
you can run this app by typing
$ pyside6_example_2
MainWindow¶
from typing import override
from PySide6.QtGui import QCloseEvent
from PySide6.QtWidgets import QMainWindow
from pyside6_by_example.examples.basic_2.menus import MainMenuBar
from pyside6_by_example.examples.basic_2.panels import BottomPanel, MainPanel, RootPanel, TopPanel
from pyside6_by_example.tools import AppPersistence
from pyside6_by_example.tools.geometry_helpers import set_geometry
class MainWindow(QMainWindow):
def __init__(self, screen_dim: tuple[int, int], app_persistence: AppPersistence):
super().__init__()
self.app_state = app_persistence.state
self.setWindowTitle("Main Window")
self.setObjectName("main")
set_geometry(self, self.app_state, screen_dim)
self.setMenuBar(MainMenuBar(self))
self.setCentralWidget(RootPanel(
TopPanel(), MainPanel(), BottomPanel()
))
@override
def closeEvent(self, event: QCloseEvent):
self.app_state.save_geometry(self.objectName(), self.saveGeometry())
event.accept()
Key differences here are:
- Function set_geometry(...) being generic moved to it its own file, and it uses object name (set by calling self.setObjectName(...) to record geometry under it.
- Classes
MainMenuBar
,RootPanel
,TopPanel
,MainPanel
andBottomPanel
are now placed into their own files as ismain()
function.