简体   繁体   English

在 Label 中打印一些文本,具体取决于文本框输入 - PyQT5

[英]Printing some text in a Label, depending on textbox input - PyQT5

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.我需要帮助弄清楚如何使用 PyQT5 中的文本框中写入的值,并使用该值来构建 IF 语句。 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) ).我试图将文本框中的文本声明为变量并在 IF 语句中使用它,但我似乎无法弄清楚如何正确执行它,并且每次运行代码时,都会显示一些退出代码(- 1073741819(0xC0000005))。

Summing up, can't use pass the value of the textbox to the variable in order to do an IF statement.总结一下,不能使用将文本框的值传递给变量来执行 IF 语句。

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如果您只是想获得用户输入,可以调用内置的 static 方法来请求特定类型的输入: https://doc.ZE85823B4E7DB1064F4301E1C749/#qinput.html/getText-5/78

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.但是,如果您想制作自己的小部件,则需要使用信号和插槽来触发 python 方法来存储值。 This is easiest to do in a class.这在 class 中最容易做到。 You can trigger the method whenever the text changes with the textChanged signal and do whatever you need to do with it.每当文本随textChanged信号发生变化时,您都可以触发该方法,并使用它做任何您需要做的事情。

(Note, I haven't run this as I don't have PyQt5 currently installed, but it should work) (注意,我没有运行它,因为我目前没有安装 PyQt5,但它应该可以工作)

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.编辑:另外,为了澄清你为什么会出错,QApplication 是 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() .访问当前 QApplication 的最佳方法是调用QApplication.instance() You also only call app.exec_() once, as once the application is running it will continue to run in the background.您也只需调用app.exec_()一次,因为一旦应用程序运行,它将继续在后台运行。 You should use signal/slots to interact with the UI and trigger the code you want to run.您应该使用信号/插槽与 UI 交互并触发您要运行的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM