Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report