2017-04-26 73 views
0

我正在做一个“包装”一个我用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()一样更改? (如果可能,不添加任何新的布局)。

谢谢。

回答

1

结构化编程与面向事件的编程有不同的范例,GUI使用事件来警告应执行该任务的插槽。

在你的情况下,处理部分必须以另一种方法来完成,当信号发出

对于你的情况称为QLineEdit小部件有2个信号,可以为您服务,第一个是editingFinished,和第二个是returnPressed,在这种情况下,我选择按下输入或返回键时发出的第二个。然后我们将该信号与执行任务的被调用进程槽连接。

我做了一些不影响设计的更改,除了信号和插槽之间的连接方式之外,首先将基类从QDialog更改为QWidget。如果你想关闭该窗口,你必须使用close()

完整代码:

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_()) 
+0

非常感谢。这很令人兴奋,我正在寻找。 –