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

What is the function of PyQt5 menus and toolbars in Python

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what is the function of PyQt5 menus and toolbars in Python, I believe most people do not know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.

1. Create the main window

The graphical interface program GUIdemo2.py, which we established above, has realized the creation of the main window by importing the graphical interface uiDemo1.py.

1.1 window types

Further, there are three types of main windows to create in QtDesigner: QMainWindow, QDialog, and QWidget.

QMainWindow: includes the menu bar, toolbar, status bar, and title bar, commonly used in application windows.

QDialog: a window that has no menu bar, toolbar, or title bar, and is often used in dialog boxes. QtDesigner further provides a dialog box with no default button, a dialog box with the bottom button, and a dialog box with the right button.

QWidget: uncertain about the type of window.

To create a new application window, you can usually choose MainWindow to create a graphical interface for the main window. Although the "MainWindow" created by QtDesigner is a blank graphics window, it has generated three basic controls: centralwidget, menubar, and statusbar. You can view these controls and structures in the object Viewer at the top right.

1.2 Edit the properties of the window

Select the main window with the mouse in QtDesigner, or select the object "MainWindow" in the object Viewer on the right, and the attribute Editor in the middle on the right will display the properties of the object "MainWindow". You can edit and modify the properties of an object in the attribute Editor.

Object name (objectName)

The name of the current window object (objectName) defaults to "MainWindow". The object name can be modified, but Xiaobai should not change it easily. A class Ui_MainWindow is defined in the generated .ui file and .py file, and the class is instantiated when the main program invokes the graphical interface.

If you change the object name, you need to change it in the main program. Some routines define the class Ui_Form, which sets the name of the window object to "Form".

Window title (windowTitle)

The default value for the title (windowTitle) of the current window object is also "MainWindow", so it is easy to confuse it with the object name (objectName).

The window title is displayed in the upper-left corner of the application window, usually the program name. The title of the window object can be modified as needed, and this example is changed to "digital image processing".

Application icon (windowIcon)

The icon (windowIcon) of the current window object defaults to Null and has no icon.

-Click on the right margin of the windowIcon line under the attribute Editor, and "[Theme]", "…" will appear. And the option box for the ▽\ triangledown ▽ button

-Click the "▽\ triangledown ▽" button, select "Select File" from "Select Resource", "Select File" and "Select theme", and select an icon file from the folder.

In this example, the\ image directory is established under the current Project path of Python, and the icons and image files are saved in the\ image directory. In this example, youcans.png is selected as the application icon, and readers can choose their own icon file.

Other object properties can also be edited and modified within the attribute Editor, such as:

Change the height of the MainWindow object to 800 and the width to 600

1.3 Preview of graphical interface design

Select: form-> Preview from the QtDesigner menu to preview the graphical interface of the design, and you can also choose to preview the display of different operating systems. This allows us to preview the display of the graphical interface without writing a main program so that we can modify it.

The preview of the application window you just created is shown in the following figure:

two。 Create menu bar 2.1 create first-level menu

The "MainWindow" graphics window created by QtDesigner automatically generates the top menu bar menubar, and a text input box "enter here" is displayed in the upper left corner of the graphics window.

Enter the title (title) of the menu object (menu):

Mouse click on the text input box "enter here", select the Chinese text input box, the border of the control becomes purple

Then double-click the selected control, the active text input box appears, and you can enter the title of the menu you want to establish.

Enter the menu title and enter the end, a first-level menu is established, for example: set the menu title to "File".

Enter the title of the menu object, and modify the properties of the menu object. A more general method is:

Select the object "menu" in the object Viewer on the right side of the QtDesigner, and the attribute Editor in the middle on the right will display the properties of the object "menu".

Select the title attribute in the attribute Editor and change it to the menu title: file.

After the establishment of the first-level menu "File", a new text input box "enter here" appears on the right side of the menu bar, and more menu objects can be created according to the above actions.

In this example, a first-level menu object "menuQuit" is also created with the title "exit".

2.2 create a secondary menu

QtDesigner creates a secondary menu, which actually adds an action to the first-level menu.

Click on the first-level menu object "File" and there are two drop-down menu options: "enter here" and "add Separator".

Select "enter here" and enter the title of the secondary menu.

We found that the text input box did not accept Chinese input at this time, so we entered the English "Open" to generate secondary menu items. Looking at the object Viewer on the right side of QtDesigner, what is created under menubar-menu is not a control object, but an action actionOpen. This is why the text input box does not accept Chinese just now.

By editing the properties of the action actionOpen, you can change the property "text" from "Open" to "open" in Chinese. Then click the menu "File", and the action actionOpen in the drop-down menu will be displayed as "Open" in Chinese. Similarly, under the first-level menu object "File", create two second-level menus (actions) "Save" and "Close", and change their properties "text" to "Save" and "close".

Click the secondary menu / action "Open", and there is a "+" button on the right, which can be clicked to create a new next-level menu.

Note: at this time, the action object "menu-actionOpen" created just now is automatically deleted and a menu object "menu-menu_2" is generated. This is because only the lowest menu item is set to the action object "QAction", while the parent menu item can only be the menu object "QMenu".

This example also creates an action object "actionQuit" under the first-level menu object "menuQuit", titled "Quit".

By previewing the application window, you can see the menu bar you just created:

2.3 Associated Action

We have created menu items in the graphical interface and added action objects to the menu items. However, "open", "save" and "close" in action objects are only the names and descriptions of these action objects, and there is no definition or association of specific actions.

Therefore, when you click on the menu, although the corresponding menu options will be displayed, you will not really "open" or "close" the menu. Take the action object "actionQuit" under the first-level menu "exit" as an example to explain how to associate the close action (close) with the action object "actionQuit".

Here we will briefly explain the mechanism of signals and slots in PyQt5. Signal and slot are the communication mechanism between objects in PyQt5. To put it simply, after a signal is connected to a slot function, when the signal is triggered, the corresponding slot function is executed automatically.

Therefore, to associate an action with an action object (or other object) is to define the action of the object (such as clicking on a menu item) as a signal and connect to the corresponding action handler. When the signal is triggered, the corresponding slot function (action handling function) is executed automatically. For example, connecting the click action object "actionQuit" as a signal to the action processing class close () automatically executes the class close () when the object is clicked, which in turn executes the closeEvent () method in the QDialog class.

The operation to associate the action function close () with the action object "actionQuit" is as follows:

From the "signal / slot Editor" window at the lower right of the QtDesigner, click the green "+" to create a new signal / slot connection

Click "" and select the object "actionQuit" from the menu.

Click "" and select "triggered ()" from the menu.

Click "" and select "MainWindow" from the menu.

Click "" and select "closed ()" from the menu.

The effect of the above operation is that when the sender object "actionQuit" triggers "triggered ()", the receiver object "MainWindow" executes the slot function "closed ()".

The use of the "signal / slot editor" in QtDesigner is indeed cumbersome, especially the support for menu bars and toolbars is not friendly, and does not support first-level menu objects directly connecting slot functions.

In contrast, for menu bars and toolbars, it is easier to establish a signal / slot connection through connect () in the main program:

Self.actionQuit.triggered.connect (MainWindow.close) 3. Create a toolbar

Toolbars / toolbars are common components in the graphical interface, placing a set of button controls in a row at the top of the graphics window.

3.1 add Toolbar

The operation of adding toolbars in QtDesigner is simple, but not easy to find:

Right-click and select add Toolbar.

You can also select Add Toolbar to Other Area to add the toolbar to the left, right, or bottom of the window.

After you add a toolbar, a toolbar appears below the top menu bar. The newly added toolbar is empty, very narrow, and can easily be ignored without attention.

3.2 add and edit action objects

Only action controls (QAction) can be added to the toolbar, so add / edit actions first.

In fact, action controls can only be added to the menu bar above, but the menu bar allows you to directly add action controls (QAction) when entering secondary menus, while the toolbar only allows you to select existing action controls to add.

Select Action Editor in the lower right window of QtDesigner to create or edit action objects.

When setting up the second-level menu, we have established actionOpen, actionSave, actionClosr, actionQuit and other action objects. These action objects are displayed in the Action Editor sub-window.

The steps to add / edit action objects are:

Click the first icon button "New" on the toolbar of the Action Editor sub-window, and the New Action dialog box pops up.

If you double-click the action object in the Action Editor, the Edit Action dialog box pops up, and you can edit the properties of the existing action object.

The structure of the New Action dialog box is the same as that of the Edit Action dialog box, and it is easier to understand the meaning of the property options in the New Action dialog box by comparing the properties of the existing action objects.

"text" refers to the title of the action, is the display content of the control, and can be in Chinese or English, such as "Open"

"object name" refers to the name of the action, is the name of the calling object in the program, and can only be in English, such as "actionOpen"

"icon" refers to the action icon displayed in the toolbar. Click the "▽\ triangledown ▽" button and select the "Select File" option to select the icon file from the folder.

This example adds action objects: "actionSetup/ Settings", "actionHelp/ help", and edits the action object "actionQuit/ exit" setting action icon.

3.3 add action objects to the toolbar

Select "Action Editor" from the lower right window of QtDesigner, click the left mouse button to select the action control, press the mouse for a long time, drag it to the toolbar of the graphics window and release it, then add the action control to the toolbar.

If the action object has an icon, the action icon is displayed on the toolbar; if the action object does not set the icon, the text information of the action title is displayed on the toolbar.

This example adds all six action objects in the Action Editor to the toolbar.

Preview the application window and you can see the toolbar you just created.

The three action objects "Open", "Save" and "close" in the toolbar of the window are displayed as text, while the three action objects "Settings", "help" and "exit" are displayed as icons. This is mainly to demonstrate different display options. Then we will make changes to add icons for action objects such as Open, Save, close, and so on.

4. Write Python applications to call the graphical interface

Add icons to "open", "save", "close" and other action objects, and save the graphical interface of the design as uiDemo3.ui in QtDesigner.

Select the uiDemo3.ui file in PyCharm, click the right mouse button to call up the drop-down menu, select: ExternalTools-> PyUIC, click the left mouse button to run PyUIC to convert the selected .ui file into .py file, and generate the uiDemo3.py file in that path.

We write a main program to call the designed graphical interface uiDemo3.py, we can complete a graphical interface application.

# GUIdemo3.py# Demo3 of GUI by PyQt5# Copyright 2021 youcans, XUPT# Crated:2021-10-08from PyQt5.QtWidgets import QApplication QMainWindowimport sysimport uiDemo3 # Import the image interface design file if _ _ name__ ='_ main__': app = QApplication (sys.argv) # create the application object MainWindow = QMainWindow () # create the main window ui = uiDemo3.Ui_MainWindow () ui.setupUi (MainWindow) MainWindow.show () # display the main window sys.exit (app.exec_ ()) # exit in the main thread

In the main program GUIdemo3.py, we import the graphical interface uiDemo3.py through "import uiDemo3".

When you run the program GUIdemo3, the pop-up graphical interface is consistent with the preview effect in 3.3. except that the action objects "open", "save" and "close" after adding icons have been displayed as icons on the toolbar.

Only the "exit" button and menu items are valid in GUIdemo3. Click to exit the application. Other buttons and menu items are not valid. This is because we have not associated an action program (connection slot function) for these controls.

These are all the contents of the article "what are the functions of PyQt5 menus and toolbars in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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

Development

Wechat

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

12
Report