Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Calculations in PyQt5
Forum Updated to NodeBB v4.3 + New Features

Calculations in PyQt5

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 3.3k Views 2 Watching
  • 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.
  • JonBJ JonB

    @CEO
    Sorry, I'm not with you. You are (trying to) convert a string from a line edit to an integer. You may know what is in the text edit, but we do not. Since I presume that in order to work your calculation must have the desired input numbers, if it were me I would want to verify and print what your calculation is getting for its numbers. For example if self.val1.text() == "" or self.val1.text() == "unintended" would Python happily return int(self.val1.text() ) as 0?

    Otherwise I cannot see how "trying to add values of two qLineEdits" could not give the intended result.

    C Offline
    C Offline
    CEO.
    wrote on last edited by
    #7

    @JonB I stated in the main post that the values for the qLineEdits are of data type INT.
    Now when I tried to use

    
    issBal = int(self.val.text()) + int(self.val2.text())
                                                  self.balAfter.setText(IsBal)
    

    it says "invalid literal for int() with base 10: '' "

    JonBJ 1 Reply Last reply
    0
    • C CEO.

      @JonB I stated in the main post that the values for the qLineEdits are of data type INT.
      Now when I tried to use

      
      issBal = int(self.val.text()) + int(self.val2.text())
                                                    self.balAfter.setText(IsBal)
      

      it says "invalid literal for int() with base 10: '' "

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #8

      @CEO said in Calculations in PyQt5:

      I stated in the main post that the values for the qLineEdits are of data type INT.

      Values in QLineEdits cannot be "of data type INT". They are of type QString, or str if you prefer in Python. You may (try to) convert them to an integer, but that is another matter.

      it says "invalid literal for int() with base 10: '' "

      Which seems to be exactly what I suggested. It is telling you that you are passing " " as the string value to int(). Which is why I suggested that you start by printing out what is in self.val.text() before you try to perform conversions to integer and mathematical operations on it.

      1 Reply Last reply
      2
      • JonBJ JonB

        @CEO
        Sorry, I'm not with you. You are (trying to) convert a string from a line edit to an integer. You may know what is in the text edit, but we do not. Since I presume that in order to work your calculation must have the desired input numbers, if it were me I would want to verify and print what your calculation is getting for its numbers. For example if self.val1.text() == "" or self.val1.text() == "unintended" would Python happily return int(self.val1.text() ) as 0?

        Otherwise I cannot see how "trying to add values of two qLineEdits" could not give the intended result.

        C Offline
        C Offline
        CEO.
        wrote on last edited by
        #9

        @JonB

        I even created another file just to test the codes freely, but it's still not working. I am trying to do calculations using the qLineEdit values but it's not working. Let's use this as an example:

                val1 = QLabel(self)
        
                val1.setText("val1:")
        
                val1.setFont(QFont("Times, BOLD", 10))
        
                val1.move(30, 30)
        
                val1.resize(130, 30)
        
        
        
                self.vTx = QLineEdit(self)
        
                self.vTx.move(300, 30)
        
                self.vTx.resize(350, 30)
        
                self.vTx.setFont(QFont("Times, Bold", 10))
        
        
        
                val2 = QLabel(self)
        
                val2.setText("val2:")
        
                val2.setFont(QFont("Times, Bold", 10))
        
                val2.move(30, 80)
        
                val2.resize(200, 30)
        
        
        
                self.v2Tx = QLineEdit(self)
        
                self.v2Tx.move(300, 80)
        
                self.v2Tx.resize(350, 30)
        
                self.v2Tx.setFont(QFont("Times, Bold", 10))
        
        
        
        
        
                self.v3Tx = QLineEdit(self)
        
                self.v3Tx.move(700, 30)
        
                self.v3Tx.resize(250, 30)
        
                self.v3Tx.setFont(QFont("Times, Bold", 10))
        
        
        
                btn1 = QPushButton('Register new user', self)
        
                btn1.move(100, 350)
        
                btn1.resize(150, 30)
        
                btn1.clicked.connect(self.calc)
        
        
        
                self.vall = self.vTx.text()
        
                self.vall2 = self.v2Tx.text()
        
        
        
            def calc(self):
        
                print(self.v2Tx.text)
        
                try:
        
                    total = int(self.vall) + int(self.vall2)
        
                    strT = str(total)
        
                    print(self.v3Tx.setText(strT))
        
                except Exception as err:
        
                    print(err)
        
        JonBJ J.HilkJ 2 Replies Last reply
        0
        • C CEO.

          @JonB

          I even created another file just to test the codes freely, but it's still not working. I am trying to do calculations using the qLineEdit values but it's not working. Let's use this as an example:

                  val1 = QLabel(self)
          
                  val1.setText("val1:")
          
                  val1.setFont(QFont("Times, BOLD", 10))
          
                  val1.move(30, 30)
          
                  val1.resize(130, 30)
          
          
          
                  self.vTx = QLineEdit(self)
          
                  self.vTx.move(300, 30)
          
                  self.vTx.resize(350, 30)
          
                  self.vTx.setFont(QFont("Times, Bold", 10))
          
          
          
                  val2 = QLabel(self)
          
                  val2.setText("val2:")
          
                  val2.setFont(QFont("Times, Bold", 10))
          
                  val2.move(30, 80)
          
                  val2.resize(200, 30)
          
          
          
                  self.v2Tx = QLineEdit(self)
          
                  self.v2Tx.move(300, 80)
          
                  self.v2Tx.resize(350, 30)
          
                  self.v2Tx.setFont(QFont("Times, Bold", 10))
          
          
          
          
          
                  self.v3Tx = QLineEdit(self)
          
                  self.v3Tx.move(700, 30)
          
                  self.v3Tx.resize(250, 30)
          
                  self.v3Tx.setFont(QFont("Times, Bold", 10))
          
          
          
                  btn1 = QPushButton('Register new user', self)
          
                  btn1.move(100, 350)
          
                  btn1.resize(150, 30)
          
                  btn1.clicked.connect(self.calc)
          
          
          
                  self.vall = self.vTx.text()
          
                  self.vall2 = self.v2Tx.text()
          
          
          
              def calc(self):
          
                  print(self.v2Tx.text)
          
                  try:
          
                      total = int(self.vall) + int(self.vall2)
          
                      strT = str(total)
          
                      print(self.v3Tx.setText(strT))
          
                  except Exception as err:
          
                      print(err)
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #10

          @CEO said in Calculations in PyQt5:

          I am trying to do calculations using the qLineEdit values but it's not working.

          I keep saying: until it's working, stop going int(self.vall) + int(self.vall2) until you have first gone print self.val1 and print self.vall2 on the line above, so we and you know what is actually going on.

                  self.vall = self.vTx.text()
          
                  self.vall2 = self.v2Tx.text()
          

          What do you think is in self.vall or self.vall2 after these lines? Why don't you print them out? Since you have only just created self.vTx and self.v2Tx as new, empty QLineEdits in the code just above, they are going to be empty, aren't they?

          C 1 Reply Last reply
          1
          • C CEO.

            @JonB

            I even created another file just to test the codes freely, but it's still not working. I am trying to do calculations using the qLineEdit values but it's not working. Let's use this as an example:

                    val1 = QLabel(self)
            
                    val1.setText("val1:")
            
                    val1.setFont(QFont("Times, BOLD", 10))
            
                    val1.move(30, 30)
            
                    val1.resize(130, 30)
            
            
            
                    self.vTx = QLineEdit(self)
            
                    self.vTx.move(300, 30)
            
                    self.vTx.resize(350, 30)
            
                    self.vTx.setFont(QFont("Times, Bold", 10))
            
            
            
                    val2 = QLabel(self)
            
                    val2.setText("val2:")
            
                    val2.setFont(QFont("Times, Bold", 10))
            
                    val2.move(30, 80)
            
                    val2.resize(200, 30)
            
            
            
                    self.v2Tx = QLineEdit(self)
            
                    self.v2Tx.move(300, 80)
            
                    self.v2Tx.resize(350, 30)
            
                    self.v2Tx.setFont(QFont("Times, Bold", 10))
            
            
            
            
            
                    self.v3Tx = QLineEdit(self)
            
                    self.v3Tx.move(700, 30)
            
                    self.v3Tx.resize(250, 30)
            
                    self.v3Tx.setFont(QFont("Times, Bold", 10))
            
            
            
                    btn1 = QPushButton('Register new user', self)
            
                    btn1.move(100, 350)
            
                    btn1.resize(150, 30)
            
                    btn1.clicked.connect(self.calc)
            
            
            
                    self.vall = self.vTx.text()
            
                    self.vall2 = self.v2Tx.text()
            
            
            
                def calc(self):
            
                    print(self.v2Tx.text)
            
                    try:
            
                        total = int(self.vall) + int(self.vall2)
            
                        strT = str(total)
            
                        print(self.v3Tx.setText(strT))
            
                    except Exception as err:
            
                        print(err)
            
            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #11

            @CEO
            vall is not defined in the code sniped you posted, there's vall1 and all 2, both are QLables and contain due to this:

            self.vall = self.vTx.text()
            
                    self.vall2 = self.v2Tx.text()
            

            an empty string, that stays that way because I fail to see a connection to the LineEdits textChanged signal

            you're trying to cast this to an int and add the numbers, thats going to fail no matter what


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            C 1 Reply Last reply
            1
            • JonBJ JonB

              @CEO said in Calculations in PyQt5:

              I am trying to do calculations using the qLineEdit values but it's not working.

              I keep saying: until it's working, stop going int(self.vall) + int(self.vall2) until you have first gone print self.val1 and print self.vall2 on the line above, so we and you know what is actually going on.

                      self.vall = self.vTx.text()
              
                      self.vall2 = self.v2Tx.text()
              

              What do you think is in self.vall or self.vall2 after these lines? Why don't you print them out? Since you have only just created self.vTx and self.v2Tx as new, empty QLineEdits in the code just above, they are going to be empty, aren't they?

              C Offline
              C Offline
              CEO.
              wrote on last edited by CEO.
              #12

              @JonB Hello, I've discovered where the error is from. Thanks a lot. I used your method by first printing the texts of the qLineEdits like: print(vTx.text()) and print(v2Tx.text()) It made me realize in my main file, I was using a wrong qLineEdit, a qLE of another class.

              Thanks. I have resolved it with your help.
              I believe everyone who commented was right, I was the one who never used the right QLineEdit. I'm grateful

              JonBJ 1 Reply Last reply
              1
              • C CEO.

                @JonB Hello, I've discovered where the error is from. Thanks a lot. I used your method by first printing the texts of the qLineEdits like: print(vTx.text()) and print(v2Tx.text()) It made me realize in my main file, I was using a wrong qLineEdit, a qLE of another class.

                Thanks. I have resolved it with your help.
                I believe everyone who commented was right, I was the one who never used the right QLineEdit. I'm grateful

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #13

                @CEO
                When things are going wrong, or not as you anticipate, it's always a good idea to break problematic complex expressions like, say, total = int(self.vall) + int(self.vall2) into its constituent segments and verify what, say, self.vall, self.vall2 really are. Else you start assuming that, say, addition is going wrong, or int() doesn't know how convert strings. One needs to work from/verify the innermost elements outward.

                If you choose to use a Python debugger while developing it's even easier to examine these things interactively as required.

                C 1 Reply Last reply
                1
                • JonBJ JonB

                  @CEO
                  When things are going wrong, or not as you anticipate, it's always a good idea to break problematic complex expressions like, say, total = int(self.vall) + int(self.vall2) into its constituent segments and verify what, say, self.vall, self.vall2 really are. Else you start assuming that, say, addition is going wrong, or int() doesn't know how convert strings. One needs to work from/verify the innermost elements outward.

                  If you choose to use a Python debugger while developing it's even easier to examine these things interactively as required.

                  C Offline
                  C Offline
                  CEO.
                  wrote on last edited by
                  #14

                  @JonB I agree with you. I'll try to be using your suggested format subsequently

                  1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @CEO
                    vall is not defined in the code sniped you posted, there's vall1 and all 2, both are QLables and contain due to this:

                    self.vall = self.vTx.text()
                    
                            self.vall2 = self.v2Tx.text()
                    

                    an empty string, that stays that way because I fail to see a connection to the LineEdits textChanged signal

                    you're trying to cast this to an int and add the numbers, thats going to fail no matter what

                    C Offline
                    C Offline
                    CEO.
                    wrote on last edited by
                    #15

                    @J-Hilk you're right. I've fixed it. Thanks for your contribution.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #16

                      Hi,

                      Just in case, for "integer only QLineEdit" there's QSpinBox. No need to fiddle with strings at all.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1

                      • Login

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