I am new to PyQT5, and today i tried to make a small program that is basically like a very simple calculator. There are two fields for text with a button in between them. A mathematical expression is entered into the left field, then the button is pressed, the entire expression goes trough eval() and the result is supposed to appear in the right field. However, upon pressing the button, the program crashes, no matter how simple the expression in the left field is. I don't see the issue in my code or don't know what could be the issue yet, hence why I'm here asking for help. What could be causing the constant crashing?
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(960, 540, 400, 75)
self.setWindowTitle('Calculation')
self.btn = QPushButton('->', self)
self.btn.resize(50, 50)
self.btn.move(175, 15)
self.btn.clicked.connect(self.click)
self.text1 = QLineEdit(self)
self.text1.move(25, 27)
self.text2 = QLineEdit(self)
self.text2.move(240, 27)
def click(self):
self.text2.setText(eval(self.text1.text()))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec())