Calculations in PyQt5
-
Hello,
I am trying to add values of two qLineEdits but my approach is not working. The inputs for the qLineEdits to be calculated are integer values.
self.val1 = QLineEdit() self.val2 = QLineEdit() self.balAfter = QLineEdit() issBal = int(self.val1.text() ) - int(self.val2.text()) strIsBal = str(issBal) recBal = int(self.val1.text() ) + int(self.val2.text()) if self.issueBtn.isChecked(): print("TransactionType is %s" % (self.issueBtn.iss)) print(strIsBal) self.balAfter.setText(strIsBal) else: print("check your values")I have tried using issBal =:
issBal = (self.val1.int() ) - int(self.val2.int()) issrcv = issRcv = int(self.val1.int() ) + int(self.val2.int())but it didn't work. Any clue on how to resolve this in PyQt5?
-
Hi
"Didnt work" is a bad description of the result you got.
it didn't compile or the result was wrong etc ?
In any case, QString has a toInt() function you can use to get real integers.
I dont know what
int(self.val1.text() )
gives in python as its so typeless
but in c++ it would be deeply wrong. -
Hi
"Didnt work" is a bad description of the result you got.
it didn't compile or the result was wrong etc ?
In any case, QString has a toInt() function you can use to get real integers.
I dont know what
int(self.val1.text() )
gives in python as its so typeless
but in c++ it would be deeply wrong. -
Hello,
I am trying to add values of two qLineEdits but my approach is not working. The inputs for the qLineEdits to be calculated are integer values.
self.val1 = QLineEdit() self.val2 = QLineEdit() self.balAfter = QLineEdit() issBal = int(self.val1.text() ) - int(self.val2.text()) strIsBal = str(issBal) recBal = int(self.val1.text() ) + int(self.val2.text()) if self.issueBtn.isChecked(): print("TransactionType is %s" % (self.issueBtn.iss)) print(strIsBal) self.balAfter.setText(strIsBal) else: print("check your values")I have tried using issBal =:
issBal = (self.val1.int() ) - int(self.val2.int()) issrcv = issRcv = int(self.val1.int() ) + int(self.val2.int())but it didn't work. Any clue on how to resolve this in PyQt5?
-
@CEO
In addition to @jsulm. If "it's not working" would it not be a good to save your intermediate results likeint(self.val1.text() )into a variable to verify its value is what you expect it to be? -
@JonB owing to the fact that I would be setting the text of the result in another qLlineEdit, I am unable to set the result in the qLineEdit without first converting it to string.
@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 ifself.val1.text() == ""orself.val1.text() == "unintended"would Python happily returnint(self.val1.text() )as0?Otherwise I cannot see how "trying to add values of two qLineEdits" could not give the intended result.
-
@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 ifself.val1.text() == ""orself.val1.text() == "unintended"would Python happily returnint(self.val1.text() )as0?Otherwise I cannot see how "trying to add values of two qLineEdits" could not give the intended result.
-
@JonB I stated in the main post that the values for the qLineEdits are of data type INT.
Now when I tried to useissBal = int(self.val.text()) + int(self.val2.text()) self.balAfter.setText(IsBal)it says "invalid literal for int() with base 10: '' "
@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
QLineEditscannot be "of data type INT". They are of typeQString, orstrif 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 toint(). Which is why I suggested that you start by printing out what is inself.val.text()before you try to perform conversions to integer and mathematical operations on it. -
@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 ifself.val1.text() == ""orself.val1.text() == "unintended"would Python happily returnint(self.val1.text() )as0?Otherwise I cannot see how "trying to add values of two qLineEdits" could not give the intended result.
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) -
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)@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 goneprint self.val1andprint self.vall2on 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.vallorself.vall2after these lines? Why don't you print them out? Since you have only just createdself.vTxandself.v2Txas new, emptyQLineEdits in the code just above, they are going to be empty, aren't they? -
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)@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
-
@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 goneprint self.val1andprint self.vall2on 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.vallorself.vall2after these lines? Why don't you print them out? Since you have only just createdself.vTxandself.v2Txas new, emptyQLineEdits in the code just above, they are going to be empty, aren't they?@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 -
@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@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.vall2really are. Else you start assuming that, say, addition is going wrong, orint()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.
-
@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.vall2really are. Else you start assuming that, say, addition is going wrong, orint()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.
-
@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