2017-04-26 74 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

非常感謝。這很令人興奮,我正在尋找。 –