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 control the display of LCD screen by PyQt5 buttons

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how PyQt5 controls the LCD screen with buttons. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Basic knowledge 1. Brief introduction of PyQt5 signals and slots

Signal and slot is the core mechanism in QT, and it is also the mechanism of direct communication between objects in PyQt5. When a signal is released, the connected slot function will be executed automatically, such as clicking / releasing button can trigger slot function.

The characteristics of signals and slots are as follows:

(1) one signal can connect multiple slot functions.

(2) one slot can monitor multiple signals.

(3) one signal can be connected to another.

(4) the connection between signal and slot can be made between different threads.

There are two ways to use signals and slots in PyQt5: built-in signals and slots, custom signals and slots; signals and slots are connected through connect () and unbound by disconnect () function. In this paper, we learn to use PyQt built-in signals and slots.

2. Introduction to QPushButton in PyQt5

The normal button in PyQt5 is also a QPushButton and the most commonly used widget in the user interface. Its base class is the QAbstracButton class and has the following general usage:

QPushButton class, which is used as follows:

3. Introduction to QLCDNumber in PyQt

The QLCDNumber control is used to display a number with an LCD-like effect. The main functions used by the control are as follows:

II. Examples

QT Desinger Design UI

Open designer.exe, create it using the default Main Window, and click the Create button directly.

The design UI diagram is as follows, and saved as btn.ui

two。 Convert btn.ui to ui_btn.py

Enter the ui_test.py directory and enter the following command:

Pyuic5-o ui_test.py test.ui

3. Program analysis

The function of the program is that when you click button, the content of the LCD display will last + 1

Import sysimport PyQt5.QtWidgets as qwimport ui_btnnum = 0if _ _ name__ = = "_ _ main__": app = qw.QApplication (sys.argv) w = qw.QMainWindow () ui = ui_btn.Ui_MainWindow () ui.setupUi (w) ui.lcdNumber.setDecMode () # set display mode # ui.lcdNumber.setStyleSheet ("border:2pxsolidgreen;color:red;background:silver ") ui.lcdNumber.setSegmentStyle (ui.lcdNumber.Flat) def lcd_show (): global num num = num + 1 ui.lcdNumber.display (num) ui.pushButton.clicked.connect (lcd_show) w.show () sys.exit (app.exec_ ())

The main difference between the code in this section and the environment building chapter in the previous section is the handling of QpushButton and QLCDNumber.

With regard to QPushButton, the program uses ui.pushButton.clicked.connect (lcd_show) to correlate the signal with the slot function. In this case, the signal is clicked (), and the corresponding slot function is lcd_show (). In the slot function, the display (num) function of QLCDNumber is called to display the number.

With regard to QLCDNumber, the program first calls the setDecMode () function to display decimal data, then sets the display mode to Flat, and finally displays numbers in the corresponding slot function of pushButton.

III. Operation

Enter the file directory, here is to enter the 2-btn folder directory, the console type python3 run.py, you can pop up the above page designed with QT Designer.

Open the comment content and modify the LCD style

Ui.lcdNumber.setStyleSheet ("border:2pxsolidgreen;color:red;background:silver;")

The results are as follows:

Thank you for reading! This is the end of the article on "how PyQt5 controls the LCD display through buttons". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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