2013-04-12 20 views
0
import sys 
from PyQt4.QtCore import SIGNAL 
from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout 

class Form(QDialog): 
    def __init__(self, parent=None): 
     super(Form, self).__init__(parent) 

     self.le = QLineEdit() 
     self.le.setText("Host") 

     self.pb = QPushButton() 

     self.pb.setText("Connect") 

     layout = QFormLayout() 
     layout.addWidget(self.le) 
     layout.addWidget(self.pb) 

     self.setLayout(layout) 
     self.connect(self.pb, SIGNAL("clicked()"),self.button_click) 
     self.setWindowTitle("Learning") 

    def button_click(self): 
     # shost is a QString object 
     shost = self.le.text() 
     #what should I write here to access the other python file and give shost as input string to that file 


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

文本該文本作爲輸入到下面給出的其他python文件。我需要修改上面的代碼中的button_click(self)函數,以便它將lineedit中的文本作爲下面的python文件的輸入。如何給一個文件作爲輸入到其他文件的輸出,如何給,當我在lineedit輸入文字,然後點擊連接按鈕也應該給在LineEdit進入PyQt4中到另一個文件中,輸入字符串現在

# Requiredfile.py 
Enteredstring = input('Enter string:') 
print Enteredstring 

回答

0

考慮你的PyQt代碼是正確的,

import sys 
from PyQt4.QtCore import SIGNAL 
from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout 

class Form(QDialog): 
    def __init__(self, parent=None): 
    super(Form, self).__init__(parent) 

    self.le = QLineEdit() 
    self.le.setText("Host") 

    self.pb = QPushButton() 

    self.pb.setText("Connect") 

    layout = QFormLayout() 
    layout.addWidget(self.le) 
    layout.addWidget(self.pb) 

    self.setLayout(layout) 
    self.connect(self.pb, SIGNAL("clicked()"),self.button_click) 
    self.setWindowTitle("Learning") 

    def button_click(self): 
    # shost is a QString object 
    shost = self.le.text() 
    import Requiredfile.py 
    Requiredfile.func(shost) 


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

你犯了一個在你的GUI文件命名爲您requiredfile.py和進口Requiredfile.py func功能和shost VAR傳遞到文件中。

這裏是你的Requiredfile.py

def func(shost): 
    Enteredstring = input('Enter string:') 
    print Enteredstring 
    print shost 

所以在這裏你得到shost在Requiredfile.py,做任何你想要與shost

相關問題