Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PyQt beginner needs help when using QLabel
Forum Updated to NodeBB v4.3 + New Features

PyQt beginner needs help when using QLabel

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 3 Posters 424 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CharliePark
    wrote on last edited by
    #1

    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_()
    
    
    JonBJ 1 Reply Last reply
    0
    • C CharliePark

      @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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply
      3
      • C CharliePark

        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_()
        
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @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.

        1 Reply Last reply
        1
        • C Offline
          C Offline
          CharliePark
          wrote on last edited by CharliePark
          #3

          @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.

          jsulmJ 1 Reply Last reply
          0
          • C CharliePark

            @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.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            C 1 Reply Last reply
            3
            • jsulmJ jsulm

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

              C Offline
              C Offline
              CharliePark
              wrote on last edited by
              #5

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved