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

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

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

1 回答 1

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做任何你想做的事情

于 2013-04-12T13:29:00.853 回答