如何将文件输出作为输入提供给其他文件,如何将pyqt4中LineEdit中输入的文本作为输入字符串提供给另一个文件

时间:2013-04-12 10:27:11

标签: python python-2.7 pyqt pyqt4 qlineedit

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

现在当我在lineedit中输入文本并单击连接按钮时,它应该将文本作为其他python文件的输入,如下所示。我需要修改上面代码中的button_click(self)函数,以便它将lineedit中的文本作为下面python文件的输入。

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

1 个答案:

答案 0 :(得分: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_()

在requiredfile.py中创建一个名为func的函数,并在gui文件中导入Requiredfile.py并将shost var传递给该文件。

这是你的Requiredfile.py

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

所以在这里你得到你的Requiredfile.py中的shost,做你想做什么shost