I'm new to PyQt5. I'm currently working on a calculator just for some practice, but for some reason, the app crashes when I use the eval()
function in the QLabel().setText()
function.
Here's a sample snippet:
import PyQt5.QtWidgets as qtw
import PyQt5.QtGui as qtg
class MainWindow:
def __init__(self):
self.win = qtw.QWidget()
self.win.setLayout(qtw.QVBoxLayout())
self.label = qtw.QLabel("2+3")
self.label.setFont(qtg.QFont("consolas", 16))
self.win.layout().addWidget(self.label)
self.btn = qtw.QPushButton("Click Me", clicked=self.pressed)
self.win.layout().addWidget(self.btn)
self.win.show()
def pressed(self):
# self.label.setText("Hello World 123") # Does not crash
# print(self.label.text()) # Does not crash
self.label.setText(eval(self.label.text())) # Crashes
app = qtw.QApplication([])
main_window = MainWindow()
app.exec_()
Here, the app crashes when I use self.label.setText(eval(self.label.text()))
. The crash does not happen when I use .setText()
or .text()
separately (as shown in the snippet), so I'm pretty sure it's not a problem with either of the methods. I'm not able to understand why the app crashes just for using the eval()
function.
Is there any solution to this problem? It would be great if anyone could help me out. Thanks in advance.
OS: Windows 11, Python Version: 3.10.5