我正在做一个“包装”一个我用PyQt GUI构建的程序的项目,而且我坚持着一个基本的东西。 我希望程序停止接收代码并等待我的输入,就像raw_input()
一样。这里是我的代码:如何等待PyQt的用户输入与行编辑?
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_()
正如你所看到的,有一个Line Edit及其变量。但它不会等待我的输入,它会继续执行代码,当然它不会让我更改“if”语句的结果。 如何暂停代码并等待用户输入,以便我的“if”语句结果将如raw_input()
一样更改? (如果可能,不添加任何新的布局)。
谢谢。
非常感谢。这很令人兴奋,我正在寻找。 –