Pyqt5 Simple Menu App Windows And Mac

In this section, we learn to create status bars, menu bars and toolbars. A menu is a set of commands located in the menu bar. A toolbar has buttons that have common commands in applications. The status bar displays status information, usually under the application window.

QMainWindow

The QMainWindow class provides a main application window. This allows us to create a classic program framework with status bars, toolbars and menu bars.

Statusbar (status bar)

A status bar is a component used to display status information.

The initial setup process for Mac OS X will require a lot of very heavy compilation, so you are going to be in for a world of pain and a very long wait if you try to do this on a Mac mini.

The status bar is created with the help of the QMainWindow component.

self.statusBar().showMessage(“Ready”)

To get the status bar, we call the statusBar () method of the class QtGui. QMainWindow. The first call to this method creates a status bar. Subsequence calls return status bar objects. ShowMessage () displays information on the status bar.

Following is the result of the operation of this small example program:

Pyqt5 Simple Menu App Windows And Mac

Simple menu

The menu bar is a common component of GUI applications. He is a group of commands located on multiple menus. Mac OS treats menu bars differently. To get similar output, we can add the following line: menubar. setNativeMenubar (False).)

Pyqt5 Simple Menu App Windows And Mac

In the example program above, we created a menu bar with a menu. This menu contains an action that will terminate the application if selected. Of course, a status bar was created. This action can also use Ctrl + Q shortcuts.

Windows

QAction is an abstract class that runs in menu bars, toolbars, and custom keyboard shortcuts. In the three lines above, we created a behavior using a specific icon and an’Exit’tag. Further, a shortcut key is defined for this behavior. The third line creates a status prompt that is displayed on the status bar when the mouse passes through the menu option.

When we select this particular behavior, a trigger signal is submitted. The signal is connected to the quit () method of the QApplication component. This will terminate the program.

The menuBar () method creates a menu bar. We created a file button using addMenu () and added an action using the addAction () method.

Below is a screenshot of this small example:

![enter description here][2

Submenu

A submenu is a menu located in another menu.

In this example, we have two menu options: one in the file menu and the other in the Import submenu of the file.

The new menu is created using QMenu.

An action is added to the submenu by using addAction ().

Options menu

In the following example, we create a button that can be selected or not.

This code example creates a view menu with an action. This behavior shows or hides the status bar. Menu options are selected when the status bar is visible.

Using the checkable option, we created an optional menu.

Because the status bar is visible at the beginning, we use the setChecked () method to set the behavior.

Depending on the status selected by the behavior, we set whether the status bar is displayed or not.

Context menu

A context menu, also known as a pop-up menu, is a list of commands that appear in some contexts. For example, in an Opera web browser, when you right-click on a web page, we get a context menu. Here we can reload a page, rollback, or view the page source code. If we right-click on a toolbar, we will get another context menu for the administration toolbar.

In order to be able to use context menus, we must re-integrate the contextMenuEvent () method.

The context menu is displayed by the exec_() method. They get the coordinates of the mouse pointer from the event object. The mapToGlobal () method passes the component coordinates to the global screen coordinates.

If the behavior returned from the context menu equals the exit behavior, the program is terminated.

toolbar

In an application, the menu bar organizes all commands. The toolbar provides fast access to common commands.

In the example above, we created a simple toolbar. The toolbar has a tool behavior, an exit behavior, and terminates the program when triggered.

Like the menu bar in the example above, we created a behavior object. The object has a label, icon and a shortcut. A quit () method in Qt. QMainWindow is connected to the trigger signal.

The toolbar is created by addToolBar (). We use the addAction () method to add an action object to the toolbar.

Put them together

In the final example, we will create a menu bar, a toolbar and a status bar. We will also create a central component.

Here, we create a text editing component. We also set it up as the central component of QMainWindow. The central component is divided into all the remaining spaces.

In this chapter, we learned about the menu bar, toolbar and status bar, as well as a main program window. Hope to give you a reference, but also hope that you support more developpaer.

About the App

  • App name: pyqt5
  • App description: Python bindings for v5 of Qt
  • App website: http://www.riverbankcomputing.co.uk/software/pyqt/download5

Using Pyqt5

Install the App

  1. Press Command+Space and type Terminal and press enter/return key.
  2. Run in Terminal app:
    ruby -e '$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)' < /dev/null 2> /dev/null
    and press enter/return key.
    If the screen prompts you to enter a password, please enter your Mac's user password to continue. When you type the password, it won't be displayed on screen, but the system would accept it. So just type your password and press ENTER/RETURN key. Then wait for the command to finish.
  3. Run:
    brew install pyqt5

Pyqt5 Simple Menu App Windows And Mac Windows 7

Done! You can now use pyqt5.

Pyqt5 Tools For Mac

Similar Software for Mac