我使用qt Designer生成我的代码。我想让我的5个文本框在释放运行按钮时将5个参数传递给python函数(该函数不在此代码中)。我不太确定如何做到这一点,我对pyqt很新。
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.runText = ""
self.scriptText = ""
self.changeText = ""
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(580, 200)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.Run = QtGui.QPushButton(self.centralwidget)
self.Run.setGeometry(QtCore.QRect(250, 150, 75, 23))
self.Run.setObjectName(_fromUtf8("Run"))
self.Script = QtGui.QLabel(self.centralwidget)
self.Script.setGeometry(QtCore.QRect(70, 10, 46, 13))
self.Script.setObjectName(_fromUtf8("Script"))
self.Hosts = QtGui.QLabel(self.centralwidget)
self.Hosts.setGeometry(QtCore.QRect(270, 10, 46, 13))
self.Hosts.setObjectName(_fromUtf8("Hosts"))
self.CHange = QtGui.QLabel(self.centralwidget)
self.CHange.setGeometry(QtCore.QRect(470, 10, 46, 13))
self.CHange.setObjectName(_fromUtf8("CHange"))
self.ScriptLine = QtGui.QLineEdit(self.centralwidget)
self.ScriptLine.setGeometry(QtCore.QRect(30, 30, 113, 20))
self.ScriptLine.setObjectName(_fromUtf8("ScriptLine"))
self.HostLine = QtGui.QLineEdit(self.centralwidget)
self.HostLine.setGeometry(QtCore.QRect(230, 30, 113, 20))
self.HostLine.setObjectName(_fromUtf8("HostLine"))
self.ChangeLine = QtGui.QLineEdit(self.centralwidget)
self.ChangeLine.setGeometry(QtCore.QRect(430, 30, 113, 20))
self.ChangeLine.setText(_fromUtf8(""))
self.ChangeLine.setObjectName(_fromUtf8("ChangeLine"))
self.Cla = QtGui.QLabel(self.centralwidget)
self.Cla.setGeometry(QtCore.QRect(260, 80, 211, 16))
self.Cla.setText(_fromUtf8(""))
self.Cla.setObjectName(_fromUtf8("Cla"))
self.Sla = QtGui.QLabel(self.centralwidget)
self.Sla.setGeometry(QtCore.QRect(260, 100, 211, 16))
self.Sla.setText(_fromUtf8(""))
self.Sla.setObjectName(_fromUtf8("Sla"))
self.Hla = QtGui.QLabel(self.centralwidget)
self.Hla.setGeometry(QtCore.QRect(260, 120, 201, 16))
self.Hla.setText(_fromUtf8(""))
self.Hla.setObjectName(_fromUtf8("Hla"))
self.Cla_2 = QtGui.QLabel(self.centralwidget)
self.Cla_2.setGeometry(QtCore.QRect(250, 60, 111, 16))
self.Cla_2.setObjectName(_fromUtf8("Cla_2"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(210, 100, 46, 13))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(210, 120, 46, 13))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_3 = QtGui.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(200, 80, 46, 13))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.lineEdit = QtGui.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(30, 80, 113, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.lineEdit_2 = QtGui.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(430, 80, 113, 20))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Wingdings 2"))
font.setPointSize(1)
self.lineEdit_2.setFont(font)
self.lineEdit_2.setAutoFillBackground(False)
self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.label_4 = QtGui.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(60, 60, 81, 16))
self.label_4.setObjectName(_fromUtf8("label_4"))
self.label_5 = QtGui.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(460, 60, 46, 13))
self.label_5.setObjectName(_fromUtf8("label_5"))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.ScriptLine, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.Sla.setText)
QtCore.QObject.connect(self.HostLine, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.Hla.setText)
QtCore.QObject.connect(self.ChangeLine, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.Cla.setText)
QtCore.QObject.connect(self.Run, QtCore.SIGNAL(_fromUtf8("released()")), self.ScriptLine.clear)
QtCore.QObject.connect(self.Run, QtCore.SIGNAL(_fromUtf8("released()")), self.HostLine.clear)
QtCore.QObject.connect(self.Run, QtCore.SIGNAL(_fromUtf8("released()")), self.ChangeLine.clear)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.Run.setText(_translate("MainWindow", "Run", None))
self.Script.setText(_translate("MainWindow", "Script", None))
self.Hosts.setText(_translate("MainWindow", "Hosts", None))
self.CHange.setText(_translate("MainWindow", "Change", None))
self.ScriptLine.setPlaceholderText(_translate("MainWindow", "Enter script file name", None))
self.HostLine.setPlaceholderText(_translate("MainWindow", "Enter Host file name", None))
self.ChangeLine.setPlaceholderText(_translate("MainWindow", "Enter Change file name", None))
self.Cla_2.setText(_translate("MainWindow", "Files to be used:", None))
self.label.setText(_translate("MainWindow", "Script:", None))
self.label_2.setText(_translate("MainWindow", "Hosts:", None))
self.label_3.setText(_translate("MainWindow", "Change:", None))
self.label_4.setText(_translate("MainWindow", "User Name", None))
self.label_5.setText(_translate("MainWindow", "Password", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
我不知道这段代码是否符合您的需求:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(366, 185)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayout_2 = QtGui.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
self.Script = QtGui.QLabel(self.centralwidget)
self.Script.setAlignment(QtCore.Qt.AlignCenter)
self.Script.setObjectName(_fromUtf8("Script"))
self.gridLayout_2.addWidget(self.Script, 0, 0, 1, 1)
self.Hosts = QtGui.QLabel(self.centralwidget)
self.Hosts.setAlignment(QtCore.Qt.AlignCenter)
self.Hosts.setObjectName(_fromUtf8("Hosts"))
self.gridLayout_2.addWidget(self.Hosts, 0, 1, 1, 1)
self.Change = QtGui.QLabel(self.centralwidget)
self.Change.setAlignment(QtCore.Qt.AlignCenter)
self.Change.setObjectName(_fromUtf8("Change"))
self.gridLayout_2.addWidget(self.Change, 0, 2, 1, 1)
self.ScriptLine = QtGui.QLineEdit(self.centralwidget)
self.ScriptLine.setObjectName(_fromUtf8("ScriptLine"))
self.gridLayout_2.addWidget(self.ScriptLine, 1, 0, 1, 1)
self.HostLine = QtGui.QLineEdit(self.centralwidget)
self.HostLine.setObjectName(_fromUtf8("HostLine"))
self.gridLayout_2.addWidget(self.HostLine, 1, 1, 1, 1)
self.ChangeLine = QtGui.QLineEdit(self.centralwidget)
self.ChangeLine.setObjectName(_fromUtf8("ChangeLine"))
self.gridLayout_2.addWidget(self.ChangeLine, 1, 2, 1, 1)
self.label_4 = QtGui.QLabel(self.centralwidget)
self.label_4.setAlignment(QtCore.Qt.AlignCenter)
self.label_4.setObjectName(_fromUtf8("label_4"))
self.gridLayout_2.addWidget(self.label_4, 2, 0, 1, 1)
self.gridLayout = QtGui.QGridLayout()
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.label_6 = QtGui.QLabel(self.centralwidget)
self.label_6.setAlignment(QtCore.Qt.AlignCenter)
self.label_6.setObjectName(_fromUtf8("label_6"))
self.gridLayout.addWidget(self.label_6, 0, 0, 1, 2)
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.label_8 = QtGui.QLabel(self.centralwidget)
self.label_8.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.label_8.setObjectName(_fromUtf8("label_8"))
self.verticalLayout.addWidget(self.label_8)
self.label_7 = QtGui.QLabel(self.centralwidget)
self.label_7.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.label_7.setObjectName(_fromUtf8("label_7"))
self.verticalLayout.addWidget(self.label_7)
self.label_9 = QtGui.QLabel(self.centralwidget)
self.label_9.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.label_9.setObjectName(_fromUtf8("label_9"))
self.verticalLayout.addWidget(self.label_9)
self.gridLayout.addLayout(self.verticalLayout, 1, 0, 1, 1)
self.verticalLayout_2 = QtGui.QVBoxLayout()
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.Cla = QtGui.QLabel(self.centralwidget)
self.Cla.setText("")
self.Cla.setObjectName(_fromUtf8("Cla"))
self.verticalLayout_2.addWidget(self.Cla)
self.Sla = QtGui.QLabel(self.centralwidget)
self.Sla.setText("")
self.Sla.setObjectName(_fromUtf8("Sla"))
self.verticalLayout_2.addWidget(self.Sla)
self.Hla = QtGui.QLabel(self.centralwidget)
self.Hla.setText("")
self.Hla.setObjectName(_fromUtf8("Hla"))
self.verticalLayout_2.addWidget(self.Hla)
self.gridLayout.addLayout(self.verticalLayout_2, 1, 1, 1, 1)
self.gridLayout_2.addLayout(self.gridLayout, 2, 1, 3, 1)
self.label_5 = QtGui.QLabel(self.centralwidget)
self.label_5.setAlignment(QtCore.Qt.AlignCenter)
self.label_5.setObjectName(_fromUtf8("label_5"))
self.gridLayout_2.addWidget(self.label_5, 2, 2, 1, 1)
self.userName = QtGui.QLineEdit(self.centralwidget)
self.userName.setObjectName(_fromUtf8("userName"))
self.gridLayout_2.addWidget(self.userName, 3, 0, 1, 1)
self.passWord = QtGui.QLineEdit(self.centralwidget)
self.passWord.setEchoMode(QtGui.QLineEdit.Password)
self.passWord.setObjectName(_fromUtf8("passWord"))
self.gridLayout_2.addWidget(self.passWord, 3, 2, 1, 1)
spacerItem = QtGui.QSpacerItem(109, 52, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.gridLayout_2.addItem(spacerItem, 4, 0, 2, 1)
spacerItem1 = QtGui.QSpacerItem(109, 52, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.gridLayout_2.addItem(spacerItem1, 4, 2, 2, 1)
self.Run = QtGui.QPushButton(self.centralwidget)
self.Run.setObjectName(_fromUtf8("Run"))
self.gridLayout_2.addWidget(self.Run, 5, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.ScriptLine, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.Sla.setText)
QtCore.QObject.connect(self.HostLine, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.Hla.setText)
QtCore.QObject.connect(self.ChangeLine, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.Cla.setText)
QtCore.QObject.connect(self.Run, QtCore.SIGNAL(_fromUtf8("released()")), MainWindow.run)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.Script.setText(_translate(_fromUtf8("MainWindow"), "Script", None))
self.Hosts.setText(_translate(_fromUtf8("MainWindow"), "Hosts", None))
self.Change.setText(_translate(_fromUtf8("MainWindow"), "Change", None))
self.label_4.setText(_translate(_fromUtf8("MainWindow"), "Use Name", None))
self.label_6.setText(_translate(_fromUtf8("MainWindow"), "Files to be used:", None))
self.label_8.setText(_translate(_fromUtf8("MainWindow"), "Change:", None))
self.label_7.setText(_translate(_fromUtf8("MainWindow"), "Script:", None))
self.label_9.setText(_translate(_fromUtf8("MainWindow"), "Hosts:", None))
self.label_5.setText(_translate(_fromUtf8("MainWindow"), "Password", None))
self.Run.setText(_translate(_fromUtf8("MainWindow"), "Run", None))
#self.ScriptLine.setPlaceholderText(_translate("MainWindow", "Enter script file name", None))
#self.HostLine.setPlaceholderText(_translate("MainWindow", "Enter Host file name", None))
#self.ChangeLine.setPlaceholderText(_translate("MainWindow", "Enter Change file name", None))
class MyMainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.runText = ""
self.scriptText = ""
self.changeText = ""
pass
def run(self):
self.runText = "{0}:{1}@{2}".format(str(self.ui.userName.text()),str(self.ui.passWord.text()),str(self.ui.HostLine.text()))
self.scriptText = str(self.ui.ScriptLine.text())
self.changeText = str(self.ui.ChangeLine.text())
my_function(self.runText, self.scriptText, self.changeText)
self.ui.ScriptLine.clear()
self.ui.HostLine.clear()
self.ui.ChangeLine.clear()
def my_function(*args):
print "Function args:"
for arg in args:
print arg
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
myMainWindow = MyMainWindow()
myMainWindow.show()
sys.exit(app.exec_())
答案 1 :(得分:0)
您可以将自己的功能连接到这样的信号:
QtCore.QObject.connect(self.Run, QtCore.SIGNAL(_fromUtf8("released()")), self.yourNewFunction)
之后你可以调用lineEdits的值:
def yourNewFunction(self):
print self.lineEdit.text()
顺便说一句,你使用pyqt5不再支持的旧式信号。再加上新风格更容易阅读:
self.Run.pressed.connect(self.yourNewFunction)