Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to call the dialog box in the main window in Python

Shulou Source: shulou.com Published: 2022-06-02 07:18:10 10月04日 Update

In this issue, the editor will bring you about how to call the dialog box in the main window in Python. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

First, create two windows

According to the ultimate goal of this article-to call the conversation window in the main window, we first use the qt designer to create two windows, one of which is the main window MainWindows and the other is a conversation window Dialog with two buttons.

Create a new MainWindows window through the qt designer and place a button in it:

Save as a UI file and create a new conversation window:

Also saved as a UI file.

So we have two UI files in our local folder that can be converted to Python code:

Convert UI files to Python code files

After saving the UI files for the two windows, we continue to use the pyuic5 tool to convert the two UI files into Python code files.

Open the command line and enter:

Run the converted Python file to see if it is normal:

Everything is fine, and then we can connect and invoke the two windows.

Call the dialog window from the main window

Before moving on to the next step, let's add a text label in the main window mainWindow to make the text empty and the font enlarged:

With this label, we can feed back the operation of the dialog box in the label tag. Save the UI file and convert the Python code again using pyuic5.

To invoke the dialog window in the main window code, we need to introduce the dialog window in the mainWindow.py file:

Then define a method in the main class of the main window to display the dialog window:

Once the method is defined, we need to bind the button with a click call, which is set in the setupUi () method:

This enables the button to click the bind dialog box:

We know that our dialog box has two buttons, a "OK", a "Cancel", in our GUI program, clicking on any one will close the dialog box, the effect seems to be the same, but in the internal mechanism of PyQt5, these are two different events, below we will show its different mechanism through the label tag set before.

Let's modify the click_button () method as follows:

Here we assign the operation of the dialog box to a variable, and then compare it with QtWidgets.QDialog.Accepted. QtWidgets.QDialog.Accepted represents the receiving event of the dialog box, that is, the event represented by "OK". If it is the receiving time, set the label label to "clicked OK", otherwise set the label label to "clicked Cannel".

The above is the editor for you to share in the Python how to call the dialog box in the main window, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Dialogue dialog box file two code button label method event different content text mechanism designer analysis design run next professional small and medium Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Apple Shulou Technology Linux Shulou Information