from qtpy.QtWidgets import QWidget, QDesktopWidget class MyWidget(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('Centered Widget') self.resize(250, 150) # Get the screen geometry screen = QDesktopWidget().screenGeometry() # Center the widget on the screen self.move((screen.width() - self.width()) // 2, (screen.height() - self.height()) // 2) widget = MyWidget() widget.show()
from qtpy.QtWidgets import QApplication, QDesktopWidget if __name__ == '__main__': app = QApplication([]) # Get the number of screens screen_count = QDesktopWidget().screenCount() print(f'Screen count: {screen_count}') # Get the size of each screen for i in range(screen_count): screen_size = QDesktopWidget().screenGeometry(i).size() print(f'Screen {i} size: {screen_size.width()}x{screen_size.height()}') app.exec_()In this example, we initialize a QApplication object and use the QDesktopWidget class to get the number of screens and their sizes. We then print this information to the console. This code can be useful for debugging or for determining the best layout for a multi-monitor setup. Overall, these examples use the qtpy QtWidgets QDesktopWidget library to access information about the user's desktop and create responsive user interfaces.