Course Content
PyQt5 Basics – Installation Process, Qt Designer Intro
In this part we are going to talk about some basics functionalities in PyQt5, we go through installation process, creating QPushButton, Signals and working with Qt Designer.
0/4
PyQt5 Tutorials
About Lesson

In this PyQt5 LCDNumber lesson we want to learn about Working with QLCDNumber in PyQt, so The QLCDNumber widget displays a number with LCD-like digits. It can display a number in just about any size. It can display decimal, hexadecimal, octal or binary numbers. It is easy to connect to data sources using the display() slot, which is overloaded to take any of five argument types.

 

In this lesson we are going to create three examples on pyqt QLCDNumber class, the first and second example is using coding and in the third example we are going to use Qt Designer.

 

So in the first example we want to display our system clock using QLCDNumber. this is the complete source code for the first example.

 

Displaying System Clock with QLCDNumber

You can see that we have imported our required classes especially QLCDNumber class that is used for creating LCDNumber digits.

 

 

These are our window requirements like window title, window icon, width and height of the window, x and y position of the window.

 

 

So in here we are going to create a QTimer class object, the QTimer class provides repetitive and single-shot timers. the QTimer class provides a high-level programming interface for timers. To use it, create a QTimer, connect its timeout() signal to the appropriate slots, and call start(). you can see that we have started the timer in 1000 meli seconds.

 

 

This is used for creating the object of QLCDNumber.

 

 

Give background color for the LCDNumber.

 

 

In here we have created the object of QTime, because we want to get the system time. Also you need to convert the time to string.

 

 

This is the code that we want to show our time in lcd. you can use display() method of QLCDNumber class.

 

 

Run the complete code and this is the result.

PyQt5 LCDNumber - Working with QLCDNumber in PyQt
PyQt5 LCDNumber – Working with QLCDNumber in PyQt

 

Creating Random Number Generator with LCDNumber

In this example we are going to create a simple random number generator with QLCDNumber, so this is the complete source code for the example.

 

 

In this example we have a QLCDNumber widget, with a QPushButton. also you can see that we have connected the clicked() signal of button with the rand_generator() method.

 

 

And this is our method that we have already connected with the clicked signal of the button.

 

 

Run the complete code and this is the result.

Working with QLCDNumber in PyQt
Working with QLCDNumber in PyQt

 

Creating Random Number Generator with QT Designer

OK now we want to create our random number generator application with Qt Designer, you can just write pyqt5designer in your terminal, after opening the Qt Designer you need to create Dialog without Buttons window. now we add widgets in Qt Designer.

  • Add a QVBoxLayout in the Qt Designer 
  • Add a QLCDNumber widget with a QPushButton in the Designer 

 

 

Also we need to add some styles for the QLCDNumber and QPushButton, so right click on the window and choose Change Stylesheet. and add this style.

 

 

So this is our design.

QLCD Designer Style
QLCD Designer Style

 

 

After completing the design you need to save the .ui file, iam going to name it random.ui, now copy the file and paste it in the Scripts folder of your Python installation, because we want to convert our ui file in to python file and for converting you need to use pyuic5 module. pyuic5 module is located in the Scripts folder of your Python installation, run this command for converting in your terminal.

 

 

And this is the converted code also we have added our valueChanged() signal with the method that we want to connect.

 

 

Run the complete code and this is the result.

PyQt Random Generator App
PyQt Random Generator App
Thank you for visiting my website! If you enjoyed the free courses, please consider supporting my works on Patreon.
Exercise Files
No Attachment Found
No Attachment Found