Python Qt6 - Show QWidget window horizontally and vertically centered on screen - How to do it?
-
Hello,
the following code:import sys from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, \ QPushButton, QFormLayout, QLineEdit class Window(QWidget): define __init__(self): super().__init__() self.UI() define UI(self): self.setWindowTitle("QFormLayout()") self.lname = QLabel("&Name:") self.nameLineEdit = QLineEdit() self.lmail = QLabel("&Email:") self.emailLineEdit = QLineEdit() self.lname.setBuddy(self.nameLineEdit) self.lmail.setBuddy(self.emailLineEdit) self.btn_programm_beenden = QPushButton("En&de", self) self.btn_terminate_program.setDefault(True) self.btn_programm_beenden.clicked.connect( self.programm_beeden) formLayout = QFormLayout() #Several control elements can be placed in a row #to be included formLayout.addRow(self.lname,self.nameLineEdit) formLayout.addRow(self.lmail,self.emailLineEdit) formLayout.addRow(self.btn_terminate_program) self.setLayout(formLayout) def program_finished(self): QApplication.instance().quit() app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec())
Question:
How can I center the window horizontally and vertically on the screen? -
Hello,
the following code:import sys from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, \ QPushButton, QFormLayout, QLineEdit class Window(QWidget): define __init__(self): super().__init__() self.UI() define UI(self): self.setWindowTitle("QFormLayout()") self.lname = QLabel("&Name:") self.nameLineEdit = QLineEdit() self.lmail = QLabel("&Email:") self.emailLineEdit = QLineEdit() self.lname.setBuddy(self.nameLineEdit) self.lmail.setBuddy(self.emailLineEdit) self.btn_programm_beenden = QPushButton("En&de", self) self.btn_terminate_program.setDefault(True) self.btn_programm_beenden.clicked.connect( self.programm_beeden) formLayout = QFormLayout() #Several control elements can be placed in a row #to be included formLayout.addRow(self.lname,self.nameLineEdit) formLayout.addRow(self.lmail,self.emailLineEdit) formLayout.addRow(self.btn_terminate_program) self.setLayout(formLayout) def program_finished(self): QApplication.instance().quit() app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec())
Question:
How can I center the window horizontally and vertically on the screen? -
Thank you.
But when I start the Python program I get this error message:
Traceback (most recent call last):
File "D:\Projekte\Python\qtadressverw\qtadressv.py", line 26, in <module>
import DesktopWidget
ModuleNotFoundError: No module named 'DesktopWidget'What can I do?
-
Thank you.
But when I start the Python program I get this error message:
Traceback (most recent call last):
File "D:\Projekte\Python\qtadressverw\qtadressv.py", line 26, in <module>
import DesktopWidget
ModuleNotFoundError: No module named 'DesktopWidget'What can I do?
@PythonQTMarlem said in Python Qt6 - Show QWidget window horizontally and vertically centered on screen - How to do it?:
import DesktopWidget
ModuleNotFoundError: No module named 'DesktopWidget'You should not have any line
import DesktopWidget
, that does not occur anywhere in the reference I gave you, and Qt would not define a class/module spelled like that. -
@PythonQTMarlem said in Python Qt6 - Show QWidget window horizontally and vertically centered on screen - How to do it?:
import DesktopWidget
ModuleNotFoundError: No module named 'DesktopWidget'You should not have any line
import DesktopWidget
, that does not occur anywhere in the reference I gave you, and Qt would not define a class/module spelled like that.@JonB in Your Link there is:
include <QStyle> include <QDesktopWidget>
It's C-Code, but I know what it means.
Now I get this errormessage:
D:\Projekte\Python\qtadressverw\venv\Scripts\python.exe D:\Projekte\Python\qtadressverw\qtadressv.py
Traceback (most recent call last):
File "D:\Projekte\Python\qtadressverw\qtadressv.py", line 146, in <module>
fenster = FensterKlasse()
File "D:\Projekte\Python\qtadressverw\qtadressv.py", line 32, in init
self.GUI()
File "D:\Projekte\Python\qtadressverw\qtadressv.py", line 40, in GUI
QStyle.alignedRect(Qt.LeftToRight,
AttributeError: type object 'Qt' has no attribute 'LeftToRight'What I have to do?
-
@JonB in Your Link there is:
include <QStyle> include <QDesktopWidget>
It's C-Code, but I know what it means.
Now I get this errormessage:
D:\Projekte\Python\qtadressverw\venv\Scripts\python.exe D:\Projekte\Python\qtadressverw\qtadressv.py
Traceback (most recent call last):
File "D:\Projekte\Python\qtadressverw\qtadressv.py", line 146, in <module>
fenster = FensterKlasse()
File "D:\Projekte\Python\qtadressverw\qtadressv.py", line 32, in init
self.GUI()
File "D:\Projekte\Python\qtadressverw\qtadressv.py", line 40, in GUI
QStyle.alignedRect(Qt.LeftToRight,
AttributeError: type object 'Qt' has no attribute 'LeftToRight'What I have to do?
@PythonQTMarlem said in Python Qt6 - Show QWidget window horizontally and vertically centered on screen - How to do it?:
include <QDesktopWidget>
That is
QDesktopWidget
. Yourimport
and error message show you are trying importDesktopWidget
, which does not exist like it says. No Qt class/module would be provided without a leadingQ
.If you cannot translate the C++ to Python look at an example like https://pythonprogramminglanguage.com/pyqt5-center-window/, which uses the same principle.
-
Thanks! But from PyQt5 to PyQt6 there were changes: Traceback (most recent call last):
File "D:\Projects\Python\qtadressverw\qtadressv.py", line 23, in <module>
from PyQt6.QtWidgets import QWidget, QDesktopWidget
ImportError: cannot import name 'QDesktopWidget' from 'PyQt6.QtWidgets' (D:\Projects\Python\qtadressverw\venv\lib\site-packages\PyQt6\QtWidgets.pyd) -
Thanks! But from PyQt5 to PyQt6 there were changes: Traceback (most recent call last):
File "D:\Projects\Python\qtadressverw\qtadressv.py", line 23, in <module>
from PyQt6.QtWidgets import QWidget, QDesktopWidget
ImportError: cannot import name 'QDesktopWidget' from 'PyQt6.QtWidgets' (D:\Projects\Python\qtadressverw\venv\lib\site-packages\PyQt6\QtWidgets.pyd)@PythonQTMarlem
So try Googlingpyqt6 QDesktopWidget
, where the first hit is https://stackoverflow.com/questions/68037950/module-pyqt6-qtwidgets-has-no-attribute-qdesktopwidget -
Thanks very much! It works! But my problem is I would like to figure it out myself with this documentation https://doc.qt.io/qtforpython/ . But I have no idea how to do that!