将点击事件连接到 pyqt 的功能
Posted
技术标签:
【中文标题】将点击事件连接到 pyqt 的功能【英文标题】:Connecting click events to function for pyqt 【发布时间】:2014-07-31 09:35:57 【问题描述】:这无疑是一个简单的问题,但我似乎无法理解。我想要做的就是单击按钮运行一个功能。我创建了一个 .ui 并将其转换为 .py 文件并将其作为 Ui_Dialog 导入。 gui 本身可以正常打开,我的 pushButton 称为 btn_browseDEM,lineEdit 称为 lineEdit_DEM。帮忙??
class MyDialog(QDialog):
def __init__(self):
QDialog.__init__(self)
# Set up the user interface from Designer.
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.btn_browseDEM.clicked.connect(self.btn_browseDEM_clicked)
def btn_browseDEM_clicked(self):
self.ui.lineEdit_DEM.setText('hello')
app = QApplication(sys.argv)
window = QDialog()
gui = MyDialog()
gui.ui.setupUi(window)
window.show()
sys.exit(app.exec_())
转换后的ui文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'C:\Python27\Scripts\PyQT\Example1.ui'
#
# Created: Thu Jul 31 20:40:38 2014
# by: PyQt4 UI code generator 4.10.3
#
# WARNING! All changes made in this file will be lost!
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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(592, 640)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(230, 600, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.tabWidget = QtGui.QTabWidget(Dialog)
self.tabWidget.setGeometry(QtCore.QRect(20, 20, 551, 571))
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
self.tab = QtGui.QWidget()
self.tab.setObjectName(_fromUtf8("tab"))
self.lineEdit_DEM = QtGui.QLineEdit(self.tab)
self.lineEdit_DEM.setGeometry(QtCore.QRect(50, 83, 301, 20))
self.lineEdit_DEM.setObjectName(_fromUtf8("lineEdit_DEM"))
self.checkBox_2 = QtGui.QCheckBox(self.tab)
self.checkBox_2.setGeometry(QtCore.QRect(50, 250, 141, 17))
self.checkBox_2.setObjectName(_fromUtf8("checkBox_2"))
self.label = QtGui.QLabel(self.tab)
self.label.setGeometry(QtCore.QRect(20, 200, 61, 16))
font = QtGui.QFont()
font.setPointSize(9)
font.setBold(False)
font.setWeight(50)
self.label.setFont(font)
self.label.setLineWidth(2)
self.label.setObjectName(_fromUtf8("label"))
self.checkBox = QtGui.QCheckBox(self.tab)
self.checkBox.setGeometry(QtCore.QRect(50, 220, 151, 17))
self.checkBox.setObjectName(_fromUtf8("checkBox"))
self.checkBox_3 = QtGui.QCheckBox(self.tab)
self.checkBox_3.setGeometry(QtCore.QRect(50, 280, 141, 17))
self.checkBox_3.setObjectName(_fromUtf8("checkBox_3"))
self.label_4 = QtGui.QLabel(self.tab)
self.label_4.setGeometry(QtCore.QRect(50, 102, 371, 16))
self.label_4.setFrameShadow(QtGui.QFrame.Plain)
self.label_4.setObjectName(_fromUtf8("label_4"))
self.pushButton_2 = QtGui.QPushButton(self.tab)
self.pushButton_2.setGeometry(QtCore.QRect(360, 130, 75, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.label_3 = QtGui.QLabel(self.tab)
self.label_3.setGeometry(QtCore.QRect(50, 60, 371, 16))
self.label_3.setFrameShadow(QtGui.QFrame.Plain)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.btn_browseDEM = QtGui.QPushButton(self.tab)
self.btn_browseDEM.setGeometry(QtCore.QRect(360, 80, 75, 23))
self.btn_browseDEM.setObjectName(_fromUtf8("btn_browseDEM"))
self.lineEdit_2 = QtGui.QLineEdit(self.tab)
self.lineEdit_2.setGeometry(QtCore.QRect(50, 131, 301, 20))
self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.line_7 = QtGui.QFrame(self.tab)
self.line_7.setGeometry(QtCore.QRect(10, 210, 521, 16))
self.line_7.setLineWidth(2)
self.line_7.setFrameShape(QtGui.QFrame.HLine)
self.line_7.setFrameShadow(QtGui.QFrame.Sunken)
self.line_7.setObjectName(_fromUtf8("line_7"))
self.line_8 = QtGui.QFrame(self.tab)
self.line_8.setGeometry(QtCore.QRect(10, 300, 521, 16))
self.line_8.setFrameShape(QtGui.QFrame.HLine)
self.line_8.setFrameShadow(QtGui.QFrame.Sunken)
self.line_8.setObjectName(_fromUtf8("line_8"))
self.line_9 = QtGui.QFrame(self.tab)
self.line_9.setGeometry(QtCore.QRect(10, 52, 521, 16))
self.line_9.setLineWidth(2)
self.line_9.setFrameShape(QtGui.QFrame.HLine)
self.line_9.setFrameShadow(QtGui.QFrame.Sunken)
self.line_9.setObjectName(_fromUtf8("line_9"))
self.line_10 = QtGui.QFrame(self.tab)
self.line_10.setGeometry(QtCore.QRect(10, 152, 521, 16))
self.line_10.setFrameShape(QtGui.QFrame.HLine)
self.line_10.setFrameShadow(QtGui.QFrame.Sunken)
self.line_10.setObjectName(_fromUtf8("line_10"))
self.label_20 = QtGui.QLabel(self.tab)
self.label_20.setGeometry(QtCore.QRect(20, 40, 61, 16))
font = QtGui.QFont()
font.setPointSize(9)
font.setBold(False)
font.setWeight(50)
self.label_20.setFont(font)
self.label_20.setObjectName(_fromUtf8("label_20"))
self.tabWidget.addTab(self.tab, _fromUtf8(""))
self.tab_3 = QtGui.QWidget()
self.tab_3.setObjectName(_fromUtf8("tab_3"))
self.label_10 = QtGui.QLabel(self.tab_3)
self.label_10.setGeometry(QtCore.QRect(80, 40, 351, 16))
self.label_10.setObjectName(_fromUtf8("label_10"))
self.lineEdit_7 = QtGui.QLineEdit(self.tab_3)
self.lineEdit_7.setGeometry(QtCore.QRect(40, 39, 31, 20))
self.lineEdit_7.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
self.lineEdit_7.setObjectName(_fromUtf8("lineEdit_7"))
self.label_11 = QtGui.QLabel(self.tab_3)
self.label_11.setGeometry(QtCore.QRect(40, 70, 481, 31))
self.label_11.setWordWrap(True)
self.label_11.setObjectName(_fromUtf8("label_11"))
self.label_12 = QtGui.QLabel(self.tab_3)
self.label_12.setGeometry(QtCore.QRect(30, 10, 491, 16))
self.label_12.setObjectName(_fromUtf8("label_12"))
self.checkBox_8 = QtGui.QCheckBox(self.tab_3)
self.checkBox_8.setGeometry(QtCore.QRect(20, 140, 341, 17))
self.checkBox_8.setObjectName(_fromUtf8("checkBox_8"))
self.lineEdit_8 = QtGui.QLineEdit(self.tab_3)
self.lineEdit_8.setGeometry(QtCore.QRect(40, 100, 81, 20))
self.lineEdit_8.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
self.lineEdit_8.setObjectName(_fromUtf8("lineEdit_8"))
self.label_17 = QtGui.QLabel(self.tab_3)
self.label_17.setGeometry(QtCore.QRect(50, 190, 351, 16))
self.label_17.setObjectName(_fromUtf8("label_17"))
self.label_15 = QtGui.QLabel(self.tab_3)
self.label_15.setGeometry(QtCore.QRect(100, 271, 351, 16))
self.label_15.setObjectName(_fromUtf8("label_15"))
self.lineEdit_9 = QtGui.QLineEdit(self.tab_3)
self.lineEdit_9.setGeometry(QtCore.QRect(60, 241, 301, 21))
self.lineEdit_9.setObjectName(_fromUtf8("lineEdit_9"))
self.comboBox = QtGui.QComboBox(self.tab_3)
self.comboBox.setGeometry(QtCore.QRect(80, 360, 261, 22))
self.comboBox.setObjectName(_fromUtf8("comboBox"))
self.comboBox.addItem(_fromUtf8(""))
self.comboBox.addItem(_fromUtf8(""))
self.comboBox.addItem(_fromUtf8(""))
self.label_14 = QtGui.QLabel(self.tab_3)
self.label_14.setGeometry(QtCore.QRect(60, 221, 461, 16))
self.label_14.setObjectName(_fromUtf8("label_14"))
self.pushButton_4 = QtGui.QPushButton(self.tab_3)
self.pushButton_4.setGeometry(QtCore.QRect(370, 240, 75, 23))
self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
self.lineEdit_10 = QtGui.QLineEdit(self.tab_3)
self.lineEdit_10.setGeometry(QtCore.QRect(60, 270, 31, 20))
self.lineEdit_10.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
self.lineEdit_10.setObjectName(_fromUtf8("lineEdit_10"))
self.label_13 = QtGui.QLabel(self.tab_3)
self.label_13.setGeometry(QtCore.QRect(20, 171, 351, 16))
self.label_13.setObjectName(_fromUtf8("label_13"))
self.lineEdit_12 = QtGui.QLineEdit(self.tab_3)
self.lineEdit_12.setGeometry(QtCore.QRect(60, 460, 301, 21))
self.lineEdit_12.setObjectName(_fromUtf8("lineEdit_12"))
self.label_19 = QtGui.QLabel(self.tab_3)
self.label_19.setGeometry(QtCore.QRect(60, 340, 261, 16))
self.label_19.setObjectName(_fromUtf8("label_19"))
self.label_16 = QtGui.QLabel(self.tab_3)
self.label_16.setGeometry(QtCore.QRect(60, 390, 461, 31))
self.label_16.setFrameShape(QtGui.QFrame.NoFrame)
self.label_16.setWordWrap(True)
self.label_16.setObjectName(_fromUtf8("label_16"))
self.lineEdit_11 = QtGui.QLineEdit(self.tab_3)
self.lineEdit_11.setGeometry(QtCore.QRect(60, 430, 301, 21))
self.lineEdit_11.setObjectName(_fromUtf8("lineEdit_11"))
self.label_18 = QtGui.QLabel(self.tab_3)
self.label_18.setGeometry(QtCore.QRect(50, 320, 351, 16))
self.label_18.setObjectName(_fromUtf8("label_18"))
self.line = QtGui.QFrame(self.tab_3)
self.line.setGeometry(QtCore.QRect(10, 330, 531, 20))
self.line.setLineWidth(2)
self.line.setFrameShape(QtGui.QFrame.HLine)
self.line.setFrameShadow(QtGui.QFrame.Sunken)
self.line.setObjectName(_fromUtf8("line"))
self.line_2 = QtGui.QFrame(self.tab_3)
self.line_2.setGeometry(QtCore.QRect(10, 180, 531, 20))
self.line_2.setLineWidth(2)
self.line_2.setFrameShape(QtGui.QFrame.HLine)
self.line_2.setFrameShadow(QtGui.QFrame.Sunken)
self.line_2.setObjectName(_fromUtf8("line_2"))
self.line_3 = QtGui.QFrame(self.tab_3)
self.line_3.setGeometry(QtCore.QRect(10, 20, 531, 20))
self.line_3.setLineWidth(2)
self.line_3.setFrameShape(QtGui.QFrame.HLine)
self.line_3.setFrameShadow(QtGui.QFrame.Sunken)
self.line_3.setObjectName(_fromUtf8("line_3"))
self.line_4 = QtGui.QFrame(self.tab_3)
self.line_4.setGeometry(QtCore.QRect(10, 150, 531, 20))
self.line_4.setLineWidth(1)
self.line_4.setFrameShape(QtGui.QFrame.HLine)
self.line_4.setFrameShadow(QtGui.QFrame.Sunken)
self.line_4.setObjectName(_fromUtf8("line_4"))
self.line_5 = QtGui.QFrame(self.tab_3)
self.line_5.setGeometry(QtCore.QRect(10, 480, 531, 20))
self.line_5.setLineWidth(1)
self.line_5.setFrameShape(QtGui.QFrame.HLine)
self.line_5.setFrameShadow(QtGui.QFrame.Sunken)
self.line_5.setObjectName(_fromUtf8("line_5"))
self.line_6 = QtGui.QFrame(self.tab_3)
self.line_6.setGeometry(QtCore.QRect(10, 290, 531, 20))
self.line_6.setLineWidth(1)
self.line_6.setFrameShape(QtGui.QFrame.HLine)
self.line_6.setFrameShadow(QtGui.QFrame.Sunken)
self.line_6.setObjectName(_fromUtf8("line_6"))
self.tabWidget.addTab(self.tab_3, _fromUtf8(""))
self.tab_2 = QtGui.QWidget()
self.tab_2.setObjectName(_fromUtf8("tab_2"))
self.lineEdit_3 = QtGui.QLineEdit(self.tab_2)
self.lineEdit_3.setGeometry(QtCore.QRect(24, 68, 31, 20))
self.lineEdit_3.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3"))
self.label_5 = QtGui.QLabel(self.tab_2)
self.label_5.setGeometry(QtCore.QRect(30, 40, 491, 16))
self.label_5.setObjectName(_fromUtf8("label_5"))
self.label_6 = QtGui.QLabel(self.tab_2)
self.label_6.setGeometry(QtCore.QRect(60, 70, 341, 16))
self.label_6.setObjectName(_fromUtf8("label_6"))
self.label_8 = QtGui.QLabel(self.tab_2)
self.label_8.setGeometry(QtCore.QRect(60, 130, 341, 16))
self.label_8.setObjectName(_fromUtf8("label_8"))
self.lineEdit_5 = QtGui.QLineEdit(self.tab_2)
self.lineEdit_5.setGeometry(QtCore.QRect(24, 128, 31, 20))
self.lineEdit_5.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
self.lineEdit_5.setObjectName(_fromUtf8("lineEdit_5"))
self.lineEdit_4 = QtGui.QLineEdit(self.tab_2)
self.lineEdit_4.setGeometry(QtCore.QRect(24, 99, 31, 20))
self.lineEdit_4.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
self.lineEdit_4.setObjectName(_fromUtf8("lineEdit_4"))
self.label_7 = QtGui.QLabel(self.tab_2)
self.label_7.setGeometry(QtCore.QRect(60, 101, 341, 16))
self.label_7.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
self.label_7.setObjectName(_fromUtf8("label_7"))
self.tabWidget.addTab(self.tab_2, _fromUtf8(""))
self.tab_4 = QtGui.QWidget()
self.tab_4.setObjectName(_fromUtf8("tab_4"))
self.checkBox_7 = QtGui.QCheckBox(self.tab_4)
self.checkBox_7.setGeometry(QtCore.QRect(30, 40, 241, 17))
self.checkBox_7.setObjectName(_fromUtf8("checkBox_7"))
self.label_9 = QtGui.QLabel(self.tab_4)
self.label_9.setGeometry(QtCore.QRect(30, 70, 471, 61))
self.label_9.setWordWrap(True)
self.label_9.setObjectName(_fromUtf8("label_9"))
self.lineEdit_6 = QtGui.QLineEdit(self.tab_4)
self.lineEdit_6.setGeometry(QtCore.QRect(30, 141, 301, 21))
self.lineEdit_6.setObjectName(_fromUtf8("lineEdit_6"))
self.pushButton_3 = QtGui.QPushButton(self.tab_4)
self.pushButton_3.setGeometry(QtCore.QRect(340, 140, 75, 23))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.tabWidget.addTab(self.tab_4, _fromUtf8(""))
self.retranslateUi(Dialog)
self.tabWidget.setCurrentIndex(0)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.checkBox_2.setText(_translate("Dialog", "Run depression areas?", None))
self.label.setText(_translate("Dialog", "TOOLS", None))
self.checkBox.setText(_translate("Dialog", "Run overland flow paths?", None))
self.checkBox_3.setText(_translate("Dialog", "Delineate catchments?", None))
self.label_4.setText(_translate("Dialog", "Set directory path where the results should be stored", None))
self.pushButton_2.setText(_translate("Dialog", "Browse", None))
self.label_3.setText(_translate("Dialog", "Set the path to your digital elevation model (must be in the Saga grid format)", None))
self.btn_browseDEM.setText(_translate("Dialog", "Browse", None))
self.label_20.setText(_translate("Dialog", "FOLDERS", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("Dialog", "Inputs", None))
self.label_10.setText(_translate("Dialog", "Set the minimum overland flow path length to be mapped (m)", None))
self.lineEdit_7.setText(_translate("Dialog", "50", None))
self.label_11.setText(_translate("Dialog", "The minimum area (m┬▓) required to initate an overland flow path. To run for multiple areas specify as comma delimited e.g \'1000, 2000, 3000\'", None))
self.label_12.setText(_translate("Dialog", "Overland flow path parameters", None))
self.checkBox_8.setText(_translate("Dialog", "Estimate flows for overland flow paths using the rational method?", None))
self.lineEdit_8.setText(_translate("Dialog", "10000", None))
self.label_17.setText(_translate("Dialog", "Runoff coefficient parameters", None))
self.label_15.setText(_translate("Dialog", "Default runoff coefficient (if grid is not specified)", None))
self.comboBox.setItemText(0, _translate("Dialog", "Ramser Kirpch, Waikato Regional Council", None))
self.comboBox.setItemText(1, _translate("Dialog", "Bransby Williams, Waikato Regional Council", None))
self.comboBox.setItemText(2, _translate("Dialog", "Bransby Williams, Christchurch City Council", None))
self.label_14.setText(_translate("Dialog", "Location of the runoff coefficient grid (must be in the saga grid format)", None))
self.pushButton_4.setText(_translate("Dialog", "Browse", None))
self.lineEdit_10.setText(_translate("Dialog", "0.35", None))
self.label_13.setText(_translate("Dialog", "Please fill in the boxes below if you want flows...", None))
self.lineEdit_12.setText(_translate("Dialog", "10,20,30,60,120,360,720,1440,2880,4320", None))
self.label_19.setText(_translate("Dialog", "Select method for calculating time of concentration", None))
self.label_16.setText(_translate("Dialog", "Rainfall intensities (mm/hr) and durations to be used in the flow calculation (these will be interpolated using the time of concentration to get a corrseponding rainfall intensity)", None))
self.lineEdit_11.setText(_translate("Dialog", "129,92.7,76.6,55.3,33.6,15.3,9.4,5.7,3.5,2.6", None))
self.label_18.setText(_translate("Dialog", "Time of concentration parameters", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("Dialog", "Overland Flow Paths", None))
self.lineEdit_3.setText(_translate("Dialog", "50", None))
self.label_5.setText(_translate("Dialog", "This module creates a lot of polygons, here you can specify filter parameters for the depression areas", None))
self.label_6.setText(_translate("Dialog", "Only depression areas with a volume exceeding xx m2 will be mapped", None))
self.label_8.setText(_translate("Dialog", "Only depression areas with a depth exceeding xx mm will be mapped", None))
self.lineEdit_5.setText(_translate("Dialog", "300", None))
self.lineEdit_4.setText(_translate("Dialog", "500", None))
self.label_7.setText(_translate("Dialog", "Only depression areas with an area exceeding xx m3 will be mapped", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("Dialog", "Depression Areas", None))
self.checkBox_7.setText(_translate("Dialog", "Use overland flow paths generated by script?", None))
self.label_9.setText(_translate("Dialog", "<html><head/><body><p>If you do not want to use overland flow paths created by the script for delineating catchments, then you will need to browse to a shape file. The attribute table of the shape file must have a field named \'X_2\' containing the X co-ordinate of the catchment outlet and a field named \'Y_2\' containing the Y co-ordinate of the cathment outlet</p></body></html>", None))
self.pushButton_3.setText(_translate("Dialog", "Browse", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_4), _translate("Dialog", "Catchments", None))
【问题讨论】:
没有 ui 文件,很难重现。但是,是否调用了btn_browseDEM_clicked方法,只有setText不知何故没有效果,还是连信号连接都不起作用? 这似乎是信号连接不起作用,如果我在我的函数中添加 print 'hello' 没有任何反应。此外,如果我将 self.ui.lineEdit_DEM.setText('hello') 线移动到 init 没有任何反应(即我的 lineEdit_DEM 中没有打印“hello”) 你应该给对话框一个父级,我猜想把窗口传递给它。而且我不确定您是否可以将对话框用作另一个对话框的窗口。请改用适当的应用程序窗口。请参阅***.com/questions/18196799/… 以获取正确的对话框示例。 【参考方案1】:您可能希望直接显示创建的 gui。
app = QApplication(sys.argv)
gui = MyDialog()
gui.show()
sys.exit(app.exec_())
【讨论】:
谢谢,这就是问题所在。就这样我理解了这个问题,我不需要 window = QDialog() 行,因为我有 QDialog.__init__(self) 吗? 是的,您不需要创建另一个 QDialog,因为您已经在 QDialog 中 inheritedMyDialog。以上是关于将点击事件连接到 pyqt 的功能的主要内容,如果未能解决你的问题,请参考以下文章