Pyside2 Layout Management With QHBoxLayout

In this Pyside2 article iam going to talk about Layout Management in Pyside2, basically we are using QVBoxLayout and also QHBoxLayout for this tutorial.  also you can watch more articles on Python GUI Development with Pyside2 in the below links.

 

Check Python GUI Development With Pyside2

 

1: Getting Started With Pyside2 | Qt For Python 

2: Pyside2 GUI Creating First Window 

3: Pyside2 GUI Creating Window Icon

4: Pyside2 GUI How To Create Icon Modes

5: Pyside2 GUI How To Create Tooltip

6: Pyside2 GUI QPushButton With Signal And Slot

7: Pyside2 GUI Making Center The Window 

8: Python GUI How To Create AboutBox

9: Python GUI How to Create Digital Clock in Pyside2

10: How To Create StatusBar In Pyside2 

11: Pyside2 Creating QProgressBar

 

Also the QHBoxLayout is used to construct horizontal box layout objects and the QVBoxLayout is used  to construct vertical box layout objects.

 

So now this is the complete code for Pyside2 Layout Management With QVBoxLayout & QHBoxLayout

 

 

 

OK in the above code  first we have imported our classes from Pyside2 library

 

 

And this is our main window class that inherits from QWidget, and we are going to add a few requirements for our window in this class like title, width, height and also we are calling our two methods in this class. after that we create our QVboxLayout in our class.

 

 

Also this method is for setting our window icon

 

 

So now this is the method that we are going to create our Layout, basically we are going to add three QPushButton in our QHBoxLayout.

 

 

Also every Pyside2 application must create an application object. The sys.argv parameter is a list of arguments from a command line.

Python scripts can be run from the shell. It is a way how we can control the startup of our scripts.

 

Finally, we enter the mainloop of the application. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. The mainloop ends if we call the exit() method or the main widget is destroyed. The sys.exit() method ensures a clean exit. The environment will be informed how the application ended.

The exec_() method has an underscore. It is because the exec is a Python keyword. And thus, exec_() was used instead

 

 

So now run the complete code and this will be the result

PyQt5 Layout Management With QVBoxLayout & QHBoxLayout
PyQt5 Layout Management With QVBoxLayout & QHBoxLayout

 

 

 

Also you can watch the complete video for this article

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

2 thoughts on “Pyside2 Layout Management With QHBoxLayout”

  1. First of all, thank you so much for uploading good lecture materials.
    It is a very valuable resource for me to study.

    It didn’t work if I changed this part( class Window(QWidget): ) to this one ( class Window(QMainWindow):.)
    Can you tell what is the cause? Is it not possible here( QMainWindow )? ps.(Because I’m Korean, I’m not good at English. I used a translator.)

    • yea there are difference between these two, A QWidget is the base class for all drawable classes . Any QWidget-based class can be shown as a window by showing it when it has no parent.

      and QMainwindow is used for the main window, and It has places for a menu bar, a status bar, a toolbar, and other widgets

Comments are closed.

Share via
Copy link
Powered by Social Snap
×