I need help figuring out how to use the value written in a textbox in PyQT5, and use that value to build an IF statement. Any suggestions on how to do it? I have tried to declare the text in the textbox as a variable and use it in the IF statement but I can't seem to figure it out how to do it properly, and every time i run the code, some exit code shows (-1073741819 (0xC0000005) ).
Summing up, can't use pass the value of the textbox to the variable in order to do an IF statement.
I had this code down below:
from PyQt5 import QtWidgets from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QTextEdit def window(): app = QApplication(sys.argv) win = QMainWindow() win.setGeometry(200, 200, 400, 400) win.setWindowTitle("Register Program") label = QtWidgets.QLabel(win) label.setText("Random Text") label.move(169, 15) label2 = QtWidgets.QLabel(win) label2.resize(300, 100) label2.setText("1- Register new person\n2- See all regestries\n3- See last regestry\n\nPress ESC to exit\n") label2.move(70, 50) textbox = QtWidgets.QLineEdit(win) textbox.setText("") textbox.resize(250, 25) textbox.move(70, 250) button1 = QtWidgets.QPushButton(win) button1.move(150, 300) button1.setText("Submit") button1.clicked.connect(clicked) button2 = QtWidgets.QPushButton(win) button2.move(150, 335) button2.setText("Close") button2.clicked.connect(close) win.show() sys.exit(app.exec_()) def clicked(): inpt = int(window().textbox.text) if inpt == 1: print("Hello") def close(): sys.exit() window()```
If you're just looking to get user input, there's a builtin static method you can call for requesting input of a particular type: https://doc.qt.io/qt-5/qinputdialog.html#getText
If you want to make your own widget however, you need to use the signals and slots to trigger a python method to store the value. This is easiest to do in a class. You can trigger the method whenever the text changes with the
textChanged signal and do whatever you need to do with it.
(Note, I haven't run this as I don't have PyQt5 currently installed, but it should work)
from PyQt5 import QtCore, QtGui, QtWidgets class Widget(QtWidgets.QWidget): def __init__(self, parent=None): # type: (QtWidgets.QWidget) -> None super(Widget, self).__init__(parent) self.line_edit = QtWidgets.QLineEdit() main_layout = QtWidgets.QVBoxLayout() main_layout.addWidget(self.line_edit) self.setLayout(main_layout) self.line_edit.textChanged.connect(self.on_text_changed) def get_text(self): return self.line_edit.text() def on_text_changed(self, text): print("The text was changed to:", text) if __name__ == '__main__': app = QtWidgets.QApplication() widget = Widget() widget.show() app.exec_()
Edit: Also, to clarify why you're getting an error, QApplication is a singleton. This means there can only ever be one created. If you try to create a second, you'll get an error. The best way to access the current QApplication is to call
QApplication.instance(). You also only call
app.exec_() once, as once the application is running it will continue to run in the background. You should use signal/slots to interact with the UI and trigger the code you want to run.
User contributions licensed under CC BY-SA 3.0