简体   繁体   中英

How to wait for user input in PyQt with Line Edit?

I'm working on a project of "wrapping" a program I built with PyQt GUI and I'm stuck with a basic thing. I want the program to stop procceeding with the code and wait for my input, just like raw_input() . Here is my code:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys


class myWidget(QDialog):

    def __init__(self,parent=None):
        super(myWidget, self).__init__(parent)

        self.lineEdit = QLineEdit()
        self.textBrowser = QTextBrowser()
        self.top_btn = QPushButton("Ask me")
        self.bottom_btn = QPushButton("disable")
        layout = QVBoxLayout()
        layout.addWidget(self.textBrowser)
        layout.addWidget(self.lineEdit)
        layout.addWidget(self.top_btn)
        layout.addWidget(self.bottom_btn)
        self.setLayout(layout)
        self.lineEdit.setDisabled(True)
        self.lineEdit.clear()
        self.connect(self.top_btn, SIGNAL("clicked()"), self.inputFunc)
        self.connect(self.bottom_btn, SIGNAL("clicked()"), self.disableLine)



    def inputFunc(self):
        self.lineEdit.clear()
        self.lineEdit.setDisabled(False)
        self.textBrowser.setText("Welcome to #1 button. what do you want to do?")
        userInput = self.lineEdit.text()
        if userInput == "anything":
            self.textBrowser.append("Ok i will leave you alone")
            exit()
        else:
            self.textBrowser.append("say what?")



    def disableLine(self):
        self.lineEdit.clear()
        self.textBrowser.append("Line edit is disabled")
        self.lineEdit.setDisabled(True)


app = QApplication(sys.argv)
form = myWidget()
form.show()
app.exec_()

As you can see, there is a Line Edit and its variable. But it doesn't wait for my input, it proceed with the code and of course it doesn't let me to change the result of my "if" statement. How do I pause the code and wait for the user input so my "if" statement result will be changed just like raw_input() ? (if possible, without adding any new layouts.)

Thank you.

Structured programming has a different paradigm than event-oriented programming, GUIs use events to warn the slots that should perform that task.

In your case, the processing part must be done in another method and called when a signal is issued

For your case the QLineEdit widget has 2 signals that could serve you, the first one is editingFinished , and the second is returnPressed , in this case I choose the second that is emitted when I press the enter or return key. Then we connect that signal with the called process slot, that executes the task.

I made some changes that do not affect the design, first change the base class from QDialog to QWidget, in addition to the style of connection between the signals and the slots. If you want to close the window you must use close()

complete code:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

class myWidget(QWidget):
    def __init__(self,parent=None):
        super(myWidget, self).__init__(parent)
        self.lineEdit = QLineEdit()
        self.textBrowser = QTextBrowser()
        self.top_btn = QPushButton("Ask me", )
        self.bottom_btn = QPushButton("disable")
        layout = QVBoxLayout()
        layout.addWidget(self.textBrowser)
        layout.addWidget(self.lineEdit)
        layout.addWidget(self.top_btn)
        layout.addWidget(self.bottom_btn)
        self.setLayout(layout)
        self.lineEdit.setDisabled(True)
        self.top_btn.clicked.connect(self.inputFunc)
        self.lineEdit.returnPressed.connect(self.process)
        #self.bottom_btn.clicked.connect(self.disableLine)
    def inputFunc(self):
        self.lineEdit.setDisabled(False)
        self.textBrowser.setText("Welcome to #1 button. what do you want to do?")
    def process(self):
        userInput = self.lineEdit.text()
        if userInput == "anything":
            self.textBrowser.append("Ok i will leave you alone")
            #self.close()
        else:
            self.textBrowser.append("say what?")
        self.lineEdit.clear()

app = QApplication(sys.argv)
w = myWidget()
w.show()
sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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