问题描述
首先,我想说明我的问题与此非常相似:从 GUI 类外部访问 GUI 元素 .但由于我是 pyQt 的新手,我需要更多帮助.
First of all, I'd like to state that my question is very similar to this: Accessing GUI elements from outside the GUI class . But since I am a newby in pyQt, I would like some more help.
我有从 Qt Designer 导出的 design.py 文件
I have the design.py file, exported from Qt Designer
# -*- coding: utf-8 -*-
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(150, 94)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.lineEdit = QtGui.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(20, 10, 113, 27))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(30, 40, 85, 27))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "PushButton", None))
导入到主类中:
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
import design
class ExampleApp(QtGui.QMainWindow, design.Ui_MainWindow):
def __init__(self, parent=None):
super(ExampleApp, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.button_function)
def button_function(self):
self.lineEdit.setText('results')
def main():
app = QtGui.QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()
到目前为止,一切都很好.我创建了一个窗口,在该窗口中按下按钮,并且在 editLine 中出现一条消息.
So far, so good. I have created a window in which I press a pushButton and a message appears in the editLine.
这就是我想要实现的:我希望按钮触发类之外的函数(easy),并且该函数将消息传递到 edilTLine (hard).像这样:
This is what I want to achieve: I want the pushButton to trigger a function outside the class (easy), and that function to deliver the message to the edilTLine (hard). Something like this:
def button_function(self):
calculations()
def calculations():
# somehow ...
self.lineEdit.setText('results')
def main():
app = QtGui.QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()
任何想法将不胜感激.
推荐答案
这就是你所需要的
def button_function(self):
calculations(self)
def calculations(obj):
obj.lineEdit.setText('results')
这篇关于PyQt:从类外部访问元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!