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 use PyQt5 basic controls in Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use the basic PyQt5 controls in Python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Introduction to PyQt5 controls 1.1 what is a control

Control, also known as control object, is not only the most basic component type of Qt user interface, but also the basic structure of user interface. Everything displayed in the user interface is a control, such as buttons, labels, text boxes, menu bars, toolbars, status bars, and even the entire window itself.

The QWidget class is the base class of PyQt5 and the parent class of all controls. All Qt controls are subclasses of the QWidget class, so they inherit the characteristics of the QWidget class, can receive user input events, such as mouse and keyboard input, and can display the contents of the control in the user interface, as well as store other controls.

1.2 Edit the properties of the control

Control has properties. Different types of controls have some of the same properties as well as some different proprietary properties.

The same properties of the control include name, shape, location, size, and formatting. The specific properties of the control include: display content, accept input, user interaction, date, frame, and so on.

All Qt controls are subclasses of the QWidget class and inherit the properties of the QWidget class. Therefore, our learning control can learn by class, first learning the common properties of its inherited parent class, and then learning the proprietary properties unique to the control. Learning about a control is a structure diagram of the inheritance relationship of a class.

We take the graphical interface uiDemo5.ui as an example to introduce the general properties of the control and edit and modify the properties of the control.

As mentioned earlier, the object Viewer at the top right of the QtDesigner shows the control name and structure in the interface, and the property Editor below shows the various properties of the selected control object. Select a control in the graphical interface, or select a control in the object Viewer, and the properties of the control object are displayed in the property Editor.

Whether it is a window object MainWindow, a menu bar menubar, a toolbar toolBar, a button pushButton, or an edit line lineEdit, they all have some of the same properties, such as:

ObjectName: the control name, which is very important. To be exact, objectName is used to access the control and interact with the control in the ID,Python program of the control. All controls must have a unique objectName and cannot have the same name. You cannot use Chinese characters.

Geometry: size and location of the control.

The width and height of the control can be modified. For MainWindow, the width / height of the control is the width / height of the graphical interface program window.

The control position (X, Y) is the coordinate value of the control in the window control, the control position of MainWindow cannot be modified, and the position of other controls (X, Y) can be modified as needed.

MinimumSize and maximumSize are the minimum and maximum dimensions of the control object after the main window size is stretched.

Font: font selection displayed by the control, including font, font size, style, effect, and so on.

1.3 Control types for PyQt5

To understand the subclasses of the QtWidgets class, you can print out all the subclasses using "subclasses ()", with a total of about 30 subclasses.

Import PyQt5 print (QtWidgets.QWidget.__subclasses__ ())

In general, these control types can be classified as follows:

Input controls:

Buttons: QPushButton (key button), QToolButton (tool button), QRadioButton (radio check box), QCheckButton (multi-check box), QCommandLinkButton (button to connect command)

Keyboard input controls: QlineEdit (single-line input), QTextEdit (multi-line input), QPlainTextEdit (ordinary multi-line input), QkeySequenceEdit (shortcut key input)

Adjust input controls: QAbstractSpinBox (step size adjustment input), QDateEdit (date input), QTimeEdit (time input), QDateTimeEdit (date and time input)

Digital adjustment box controls: QSpinBox (integer digital adjustment frame), QDoubleSpinBox (floating point digital adjustment frame)

Slide input controls: QDial (rotary drag input), QSlider (straight drag input), QScrollBar (scroll bar), QRubberBand (rubber band drag)

Drop-down input controls: QComboBox (combo box drop-down option), QSlider (straight drag input), QScrollBar (scroll bar), QRubberBand (rubber band drag)

Dialog input controls: QDialog (dialog box), QColorDialog (color dialog box), QFileDialog (file dialog box), QFontDialog (font dialog box), QInputDialog (input dialog box)

Calendar control: QCalendarWidget (date selection part)

Display controls:

Content display controls: QLabel (display box), QLCDNumber (liquid crystal display), QProgressBar (progress bar)

Dialog box display controls: QMessageBox (message box), QErrorMessage (error box), QProgressDialog (progress box)

Advanced controls:

Container control: QToolBox,QDialogButtonBox,QGroupBox,QMdiSubWindow

Structural controls: QMainWindow,QTabwidget,QStackedWidget,QSplitter,QDockWidget

Scroll controls: QTextBrowser,QScrollArea,QAbstractItemView,QMdiarea,QGraphicsView

Auxiliary control: QFocusFrame,QSizeGrip,QDesktopWidget

Other controls

In the WidgetBox toolbar on the left side of QtDesigner, group commonly used controls by category. Drag the control icon in the toolbar to the graphical interface editing window in the middle of QtDesigner with the mouse to create a selected control in the image interface.

two。 Button Control 2.1 introduction to Button Control

Buttons are the most commonly used type of control.

In the "Buttons" group in the "WidgetBox" toolbar on the left side of QtDesigner, there are many different types of button controls: PushButton (key button), QToolButton (tool button), QRadioButton (radio check box), QCheckButton (multi-check box), QCommandLinkButton (button to connect commands). QDialogButtonBox (standard button box) provides a series of standard buttons, which are equivalent to the combination of button controls.

Common properties of button controls:

Text: displaying text

Icon: setting icon

IconSize: icon siz

Shortcut: setting shortcut key

Checkable: sets whether to automatically switch buttons

Checked: set default check statu

AutoRepeat: whether the setting is automatically repeated when the user presses

AutoExclusive: sets whether automatic exclusivity is enabled (sets checkboxes)

Signal trigger condition of the button control:

Clicked (): signals when the button is first pressed and then released

Clicked (bool): signals when the button is pressed for the first time and then released, and passes the current state to the outside world

Pressed (): press the left mouse button to signal when the mouse cursor is inside the button

Released (): signals when the left mouse button is released

Toggled (bool): signals when the state of a button changes and transmits the current state to the outside world

Common modes / states of button controls:

Available or unavailable, button grayed out when disabled

Standard button, switch button or menu button

On or off (for switch buttons only)

Default state or normal state

Whether to repeat automatically

Whether it is in the pressed state

Drag the button control in the toolbar to the graphical interface editing window in the middle of QtDesigner with the mouse to create a button control in the image interface. As shown in the following figure, we created several different button controls on the left side of the graphical interface uiDemo5.ui.

2.2 Button (QPushButton)

QPushButton (key button) is the most commonly used button, press (or click) button to perform an action or answer questions, such as: OK, apply, cancel, close, Yes, No and help.

Button controls usually display a text label (text), you can select an icon for the button (icon), and you can choose to set a shortcut key (shortcut).

The above properties of the key button can be edited and modified in the corresponding property row in the attribute Editor.

When a keystroke button is activated by a mouse or shortcut, the button emits a clicked () signal that triggers a specific action through the connection slot function.

2.3 other buttons

QToolButton (tool buttons) is commonly used in toolbars and is usually created when you create an instance of QAction. Tool buttons usually display an icon that provides quick access to specific commands or options.

QRadioButton (checkbox) is a checkbox with a text label that can be turned on (checked) or off (unchecked). When the checkbox is selected or cleared, it emits a "toggled ()" signal, which can trigger a specific action through the connection slot function.

QCheckButton (checkbox) is a check box with a text label that provides a semi-selected state (optional) in addition to turning it on (selected) or off (unselected). When the check box is selected or cleared, a "stateChanged ()" signal is emitted, and a specific action can be triggered by the connection slot function.

The radio box defines the "multiple one" selection, while the check box provides the "multiple multiple" selection.

QCommandLinkButton (the button to connect the command) looks like a flattened QPushButton and comes with a right-pointing icon that is similar to a radio button for choosing between a set of mutually exclusive options.

QDialogButtonBox (standard button box) provides a series of standard buttons that can be arranged horizontally or vertically, often used in dialog boxes and message boxes. The standard buttons defined by Qt include: Yes, No, OK, Cancel, Ignore, Open, Save, Close, Apply, Help and so on. You can customize and add them.

3. Introduction to input Control 3.1 input Control

The "Input Widget" group in the WidgetBox toolbar on the left side of QtDesigner has several different types of input controls, such as:

Text input controls: QlineEdit (single-line input), QTextEdit (multi-line input), QPlainTextEdit (normal multi-line input)

Digital input controls: QSpinBox (integer data input), QDoubleSpinBox (floating point data input)

Adjust input controls: QAbstractSpinBox (step size adjustment input), QDateEdit (date input), QTimeEdit (time input), QDateTimeEdit (date and time input).

Drag the input control from the toolbar to the graphical interface editing window in the middle of QtDesigner with the mouse to create a button control in the image interface. As shown in the previous figure, we created several different input controls in the middle and right side of the graphical interface uiDemo5.ui.

3.2 text input control

Single-line text input box (QlineEdit)

The QLineEdit control is a single-line text editor that allows users to enter and edit single-line text and supports undo, redo, cut, paste, and drag-and-drop functions.

QLineEdit control properties, signals, functions, and support password input mode, text box automatic completion, in this time without a detailed introduction, interested readers can view the relevant documents and routines.

Multiline text input box (QTextEdit)

The QTextEdit control is a WYSIWYG multiline rich text editor that allows users to enter and edit multiple lines of text, supports a subset of HTML4 tags, loads plain text and rich text files, and is suitable for editing and browsing large rich text files.

QTextEdit applies to paragraphs and characters. You can display images, lists and tables. If the text is too large to be viewed in a text-edited viewport, a scroll bar appears.

TextEdit can display not only text but also HTML documents

Normal multiline input (QPlainTextEdit)

The QPlainTextEdit control is a multiline plain text editor that allows users to enter and edit multiline plain text without supporting tables or embedded frames. It is optimized for plain text processing to handle larger documents and faster responses.

3.3 adjust input control

Step size adjustment input (QAbstractSpinBox)

QAbstractSpinBox abstracts the common functions of all step size regulators into a parent class, which can also be instantiated directly. QAbstractSpinBox contains a QLineEdit and two QPushbutton, and data changes can be entered by clicking a button or using the keyboard.

Date and time input (QDateEdit/QTimeEdit/QDateTimeEdit)

The QDateEdit control is used to edit the date, the QTimeEdit control is used to edit the time, and QDateTimeEdit edits the date and time at the same time. You can use the up and down buttons on the keyboard to increase or decrease the date and time.

Integer digital adjustment frame (QSpinBox)

QSpinBox is a counter control that allows the user to select an integer to increment or decrease by pressing the up and down keys, or to enter the value of the integer directly. The default value range is 0-99, and the step size is 1 at a time.

Floating point digital adjustment frame (QDoubleSpinBox)

QDoubleSpinBox is a floating-point data counter control that handles floating-point values. The default precision is 2 decimal places. -

4. Python application calls the graphical interface

Save the graphical interface of the design to uiDemo5.ui in QtDesigner, run PyUIC to convert the selected .ui file to .py file, and generate the uiDemo5.py file in that path.

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

# GUIdemo5.py# Demo5 of GUI by PyQt5# Copyright 2021 youcans, XUPT# Crated:2021-10-12import sysfrom PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBoxfrom uiDemo5 import Ui_MainWindow # Import Ui_MainWindow interface class class MyMainWindow (QMainWindow, Ui_MainWindow) in uiDemo5.py: # inherit QMainWindow class and Ui_MainWindow interface class def _ init__ (self, parent=None): super (MyMainWindow Self). _ init__ (parent) # initialize parent class self.setupUi (self) # inherit Ui_MainWindow interface class def trigger_actHelp (self): # Action actHelp triggers QMessageBox.about (self, "About", "" Digital Image processing Toolbox v1.0\ nCopyright YouCans " XUPT 2021 "") returnif _ _ name__ = ='_ main__': app = QApplication (sys.argv) # used in the QApplication method Create the application object myWin = MyMainWindow () # instantiate the MyMainWindow class, create the main window myWin.show () # end the process in the desktop display control myWin sys.exit (app.exec_ ()) # and exit the program

Because this paper mainly introduces the commonly used basic controls, the signal of the control has not been connected to the slot function in the program, so when running the program uiDemo5.py, some of the button controls do not trigger the action after clicking.

These are all the contents of this article entitled "how to use PyQt5 basic controls 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