Python Tutorial – Create MessageBox with Pyside2

In this Python Tutorial we are going to learn how to Create MessageBox with Pyside2, The QMessageBox  class provides a modal dialog for informing the user or for asking the user a question and receiving an answer.

 

 

Also you can check more Python GUI Development tutorial in different libraries.

1: PyQt5 GUI Development Tutorials

2: TKinter GUI Development Tutorials

3: wxPython GUI Development Tutorials 

4: Kivy GUI Development Tutorials

 

 

 

What is QMessageBox ? 

A message box displays a primary text to alert the user to a situation, an informative text to further explain the alert or to ask the user a question, and an optional detailed text to provide even more data if the user requests it. A message box can also display an icon and standard buttons for accepting a user response.

Two APIs for using QMessageBox are provided, the property-based API, and the static functions. Calling one of the static functions is the simpler approach, but it is less flexible than using the property-based API, and the result is less informative. Using the property-based API is recommended.

 

 

 

So now this is the complete source code for this article

 

 

 

 

OK now in here we are going to set our window title, also we need to set the geometry of the window, like x and y position of the window, also width and height of the window .

 

 

 

In here we have created our QVBoxLayout, also we have created some QPushButtons with QLabel. you can see that we have connected the clicked signal of every button with the specific method that we will create later.

 

 

 

Also you need to add your widgets in the vbox layout.

 

 

 

The important point about the layout is  when you create layout, after adding widgets to the layout, you need to set the layout for the main window like this.

 

 

 

These are the methods for about, warning and information message boxes, and we have already connected these methods with clicked signals of QPushButton at the top.

 

 

 

And this is the question message box method.

 

 

 

 

Finally, we have  entered to the main loop of the application. The event handling starts from this point. also we have created the object of our Window and QApplication.

 

 

 

 

 

 

Run the complete code and this will be the result

Python Tutorial - Create MessageBox with Pyside2
Python Tutorial – Create MessageBox with Pyside2

 

 

 

 

 

Also you can watch the complete video for this article

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×