2016-04-19 70 views
0

首先,我想說我的問題與此非常相似:Accessing GUI elements from outside the GUI class。但是因爲我是pyQt中的新手,我希望有更多的幫助。PyQt:訪問類之外的元素

我有design.py文件,從Qt設計

# -*- 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()  

到目前爲止,一切都很好。我創建了一個窗口,在該窗口中按下pushButton,並在editLine中顯示一條消息。

這就是我想要達到的目標:我想按鈕觸發類(容易)之外的功能,而且功能將郵件傳遞到edilTLine (硬)。類似這樣的:

​​

任何想法將不勝感激。

回答

1

這就是你需要

def button_function(self): 
     calculations(self) 

def calculations(obj): 
    obj.lineEdit.setText('results') 
+0

這正是我問。比你非常。 – user3060854