Repository navigation
Expand file tree
/
Copy pathMainWindowExtended.py
More file actions
executable file
·56 lines (47 loc) · 1.72 KB
/
Copy pathMainWindowExtended.py
File metadata and controls
executable file
·56 lines (47 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env -S uv run --script
import sys
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QMainWindow
class MainWindow(QMainWindow):
"""
A custom main window that overrides event handlers for key presses and resizing.
"""
def __init__(self, parent=None):
"""
Constructor for the MainWindow.
"""
super().__init__(parent)
self.resize(1024, 720)
self.setWindowTitle("Extending a MainWindow Class")
def keyPressEvent(self, event):
"""
Overrides the keyPressEvent to handle key presses.
This method is called every time the window receives a key event.
"""
# If the 'Escape' key is pressed, exit the application.
if event.key() == Qt.Key.Key_Escape:
QApplication.instance().quit()
# Otherwise, pass the event to the base class
else:
super().keyPressEvent(event)
def resizeEvent(self, event):
"""
Overrides the resizeEvent to handle window resizing.
This method is called every time the window size changes.
"""
# Get the new size from the event
size = event.size()
# Create a new title string using an f-string for formatting
title = f"Extending a MainWindow Class size is {size.width()} {size.height()}"
# Set the new window title
self.setWindowTitle(title)
# Call the base class's resizeEvent
super().resizeEvent(event)
if __name__ == "__main__":
# Create the application instance
app = QApplication(sys.argv)
# Create and show our custom main window
window = MainWindow()
window.show()
# Start the application's event loop
sys.exit(app.exec())