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. Design of window with PyQt 5
Forum Updated to NodeBB v4.3 + New Features

Design of window with PyQt 5

Scheduled Pinned Locked Moved Unsolved Qt for Python
15 Posts 5 Posters 2.8k Views 2 Watching
  • 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.
  • Samuel BachorikS Offline
    Samuel BachorikS Offline
    Samuel Bachorik
    wrote on last edited by
    #5

    Like this iam sorry

    from PyQt5 import QtWidgets
    from PyQt5.QtWidgets import QMainWindow
    import sys
    from PyQt5.QtWidgets import QApplication

    class Window(QMainWindow):

    def __init__(self):
        super().__init__()
    
        self.name = ""
        self.password = ""
    
        self.setGeometry(300, 300, 400, 500)
        self.setWindowTitle("Hello")
    
        self.b1 = QtWidgets.QPushButton(self)
        self.b1.setText("Button")
        self.b1.move(120, 155)
        self.b1.resize(170, 32)
        self.show()
    

    if name == "main":
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec())

    1 Reply Last reply
    0
    • Volodymyr14V Offline
      Volodymyr14V Offline
      Volodymyr14
      wrote on last edited by
      #6

      Don`t do these setGeometry!

      class Window(QMainWindow):
          
          def __init__(self, parent=None):
              super(Window, self).__init__(parent)
              self.setWindowTitle("Please login")
      
      if __name__ == "__main__":
          ...
          app_window = Window()
          app_window.show()
          desktop = QtWidgets.QApplication.desktop()
          resolution = desktop.availableGeometry()
          app_window.setFixedWidth(resolution.width() / 2)
          app_window.setFixedHeight(resolution.height() / 2)
          app_window.move(resolution.center() - app_window.rect().center())
          sys.exit(app.exec_())
      

      PyQt/PySide

      1 Reply Last reply
      2
      • Volodymyr14V Offline
        Volodymyr14V Offline
        Volodymyr14
        wrote on last edited by
        #7

        Or if want to save stretch:

        class Window(QMainWindow):
        
            def __init__(self, parent=None):
                super(Window, self).__init__(parent)
                self.setWindowTitle("Please login")
                self.setMinimumWidth(resolution.width() / 2)
                self.setMinimumHeight(resolution.height() / 2)
                ...
        
        
        if __name__ == "__main__":
            ...
            app = QtWidgets.QApplication(sys.argv)
            desktop = QtWidgets.QApplication.desktop()
            resolution = desktop.availableGeometry()
            app_window = Window()
            app_window.show()
            app_window.move(resolution.center() - app_window.rect().center())
            sys.exit(app.exec_())
        

        And button to the layout )

        PyQt/PySide

        1 Reply Last reply
        2
        • Samuel BachorikS Offline
          Samuel BachorikS Offline
          Samuel Bachorik
          wrote on last edited by
          #8

          @Volodymyr14 Thank you for tip !!!

          JonBJ 1 Reply Last reply
          0
          • Volodymyr14V Offline
            Volodymyr14V Offline
            Volodymyr14
            wrote on last edited by
            #9

            @Samuel-Bachorik no problem. The second variant is better because the screens are different and the size will be from minimum signed to a resolution of the screen. Really, in applications, it is not a good idea to use fixed sizes, set geometry, fixed resize and move. No fixed integers with sizes, just relative values. For example, to the fixed size put the size of sceen or widget divided on 2.

            PyQt/PySide

            1 Reply Last reply
            1
            • Samuel BachorikS Samuel Bachorik

              @Volodymyr14 Thank you for tip !!!

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

              @Samuel-Bachorik
              All this self.b1.move(120, 155)-type stuff is possible but is usually not the right way to go about things. You would normally create QLayouts on your widgets --- if you are really using a QMainWindow for your app that would be on the QMainWindow::centralWidget() --- and then add your child widgets onto those layouts. Not doing so may lead to unexpected sizing/positioning issues. If you have not read through https://doc.qt.io/qt-5/layout.html, may I suggest you do so and reconsider in that light.

              1 Reply Last reply
              1
              • Samuel BachorikS Offline
                Samuel BachorikS Offline
                Samuel Bachorik
                wrote on last edited by Samuel Bachorik
                #11

                Thank you for you help guys ! But iam not sure exatly about these sizes of my window. When i was working GUI in Tkinter whole program was able to scale without doing something extra. But at least how can i stop to scaling my window ? Just permament ressolution and no resizing allowed. You know also when i do not define set geometry from where this program know how big my window should be ?

                1 Reply Last reply
                0
                • Samuel BachorikS Offline
                  Samuel BachorikS Offline
                  Samuel Bachorik
                  wrote on last edited by
                  #12

                  I need to learn and figure it out how to make this in right way. What should i do when i want scale with all widgeds in window when i try to resize it while using.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    Hi,

                    Use layouts.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • Samuel BachorikS Offline
                      Samuel BachorikS Offline
                      Samuel Bachorik
                      wrote on last edited by
                      #14

                      Thank you guys for your help !

                      1 Reply Last reply
                      0
                      • Volodymyr14V Offline
                        Volodymyr14V Offline
                        Volodymyr14
                        wrote on last edited by
                        #15

                        @Samuel-Bachorik You don`t need to care about scaling. If you will use a resolution of the desktop you will have the window that will be similar for any device with any screen resolution. To build the elements inside this window you can build these elements (buttons, fields, etc) with layouts without setting sizes explicitly. It is better to use the QGridLayout, or you can use QVBoxLayout or QHBoxLayout if the app is simple in functional elements. Or another way to make sizes relative to the window. For example:

                        self.button.setFixedWidth(self.width() / 3)
                        

                        That will be button width as one-third of the window size, and it will work for any device with any resolution. And do not any these "resize(100, 100)".

                        PyQt/PySide

                        1 Reply Last reply
                        1

                        • Login

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