Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Python Qt6 - Show QWidget window horizontally and vertically centered on screen - How to do it?

Python Qt6 - Show QWidget window horizontally and vertically centered on screen - How to do it?

Scheduled Pinned Locked Moved Unsolved Qt for Python
9 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PythonQTMarlem
    wrote on last edited by
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • P PythonQTMarlem

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @PythonQTMarlem
      https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen

      1 Reply Last reply
      0
      • P Offline
        P Offline
        PythonQTMarlem
        wrote on last edited by
        #3

        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?

        JonBJ 1 Reply Last reply
        0
        • P PythonQTMarlem

          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?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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.

          P 1 Reply Last reply
          0
          • JonBJ JonB

            @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.

            P Offline
            P Offline
            PythonQTMarlem
            wrote on last edited by
            #5

            @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?

            JonBJ 1 Reply Last reply
            0
            • P PythonQTMarlem

              @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?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @PythonQTMarlem said in Python Qt6 - Show QWidget window horizontally and vertically centered on screen - How to do it?:

              include <QDesktopWidget>

              That is QDesktopWidget. Your import and error message show you are trying import DesktopWidget, which does not exist like it says. No Qt class/module would be provided without a leading Q.

              If you cannot translate the C++ to Python look at an example like https://pythonprogramminglanguage.com/pyqt5-center-window/, which uses the same principle.

              1 Reply Last reply
              1
              • P Offline
                P Offline
                PythonQTMarlem
                wrote on last edited by
                #7

                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)

                JonBJ 1 Reply Last reply
                0
                • P PythonQTMarlem

                  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)

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @PythonQTMarlem
                  So try Googling pyqt6 QDesktopWidget, where the first hit is https://stackoverflow.com/questions/68037950/module-pyqt6-qtwidgets-has-no-attribute-qdesktopwidget

                  1 Reply Last reply
                  1
                  • P Offline
                    P Offline
                    PythonQTMarlem
                    wrote on last edited by
                    #9

                    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!

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved