Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

PyQt beginner needs help when using QLabel



  • Hello I'm a beginner who just have started using PyQt

    I'm trying to make a counter, but I'm having trouble to display number through QLabel

    QLabel only shows one digit, so the number I can display is only between 1 and 9.

    Can anybody tell me the way I can handle this?

    I've searched qt document and many forum QnAs but I could't find the solution that fits for me.

    Thanks for all the people helping me.

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QToolTip
    from PyQt5.QtGui import QIcon, QPixmap, QFont
    from PyQt5.QtCore import QCoreApplication, Qt
    
    
    class Counter(QWidget):
    
        def __init__(self):
            super().__init__()
            self.UI()
    
        def UI(self):
            self.count = 0
            self.button()
            self.tooltip()
            self.Num_Counted()
    
            self.reset()
            self.increment()
            self.decrement()
    
            self.setWindowTitle('Counter')
            self.setGeometry(500, 500, 400, 400)
            self.show()
    
        def Num_Counted(self):
            self.Num = QLabel(self)
            self.Num.setAlignment(Qt.AlignRight)
            self.Num.setText('00' + str(self.count))
            self.Num.setFont(QFont("Helvetica", 75, weight=70))
            self.Num.move(200, 100)
    
        def reset(self):
            self.count = 0
            self.Num.setText(str(self.count))
    
        def increment(self):
            self.count += 1
            self.Num.setText(str(self.count))
    
        def decrement(self):
            self.count -= 1
            self.Num.setText(str(self.count))
    
        def button(self):
            self.plusbutton = QPushButton('+', self)
            self.plusbutton.setFixedSize(340, 40)
            self.plusbutton.move(30, 240)
            self.plusbutton.clicked.connect(self.increment)
    
            self.minusbutton = QPushButton('-', self)
            self.minusbutton.setFixedSize(340, 40)
            self.minusbutton.move(30, 290)
            self.minusbutton.clicked.connect(self.decrement)
    
            self.resetbutton = QPushButton('reset', self)
            self.resetbutton.setFixedSize(340, 40)
            self.resetbutton.move(30, 340)
            self.resetbutton.clicked.connect(self.reset)
    
        def tooltip(self):
            self.plusbutton.setToolTip('add 1')
            self.minusbutton.setToolTip('subtract 1')
    
    
    loop = QApplication(sys.argv)
    exe_instance = Counter()
    loop.exec_()
    
    

  • Lifetime Qt Champion

    @CharliePark As far as I can see you do not use layouts, so I guess your label is simply too small.



  • @CharliePark
    What is your question? You have a QLabel and you call self.Num.setText(str(self.count)) so it should show the number, which is what you said you want to achieve.



  • @JonB the problem is that only a digit of number is shown (sorry for my poor english)

    for example, if the number is 21, only 1 is shown.


  • Lifetime Qt Champion

    @CharliePark As far as I can see you do not use layouts, so I guess your label is simply too small.



  • @jsulm I've searched several documents about layout and I got the answer for my problem. Thanks a lot for your help!


Log in to reply