如何在Python中调用Main类的GUI函数?

时间:2016-01-12 18:06:21

标签: python user-interface progress-bar call

my_app中需要ProgressBar小部件,当我点击pushButton时,我无法在def call_progressBar2(self,value1)中呼叫def download (self)。 任何人都可以帮助我吗?

main.py

from g import Ui_MainWindow
from PyQt4 import QtCore, QtGui
import sys
import time
from b import classb

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        instance_b=classb()
        tvt= self.call_progressBar2(self)
        QtCore.QObject.connect(self.ui.pushButton1, QtCore.SIGNAL("clicked()"), \
                       lambda: instance_b.download(tvt))## I need  help here.


    def call_progressBar2(self,value1): 
        while True:
            time.sleep(0.05)
            value = self.ui.progressBar.value() + 1
            self.ui.progressBar.setValue(value)
            QtGui.qApp.processEvents()
            if (not self.ui._active or
                value >= self.progressBar.maximum()):
                if value==value1:
                    break
        self.ui._active = False            

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MainWindow()
    myapp.show()
    sys.exit(app.exec_())

b.py

import time
from PyQt4 import QtCore, QtGui

class classb (object):
    def __init__(self, parent=None):
       self.parent=parent

#I need call def call_progressBar2() here.
    def download(self):
       from main import MainWindow 
       instance_m= MainWindow
       print 'a'
       instance_m.call_progressBar2(20)
       print 'b'
       instance_m.call_progressBar2(40)
       print 'c'
       instance_m.call_progressBar2(60)
       print 'd'
       instance_m.call_progressBar2(100)

2 个答案:

答案 0 :(得分:1)

为什么不在连接信号时将当前MainWindow实例传递给download函数?或者只是在连接信号之前将其传递给classb初始化器时将其传递给download初始化器?或者指定MainWindow以便进行回调,并在连接信号时将call_progressBar2当前实例的$xlChartType=[Microsoft.Office.Interop.Excel.XLChartType] $XlLegendPosition=[Microsoft.Office.Interop.Excel.XlLegendPosition] $chart=$WorksheetTool2.Shapes.AddChart().Chart $chart.chartType=$xlChartType::xlLineMarkers $chart.SeriesCollection().NewSeries.Invoke() $chart.SeriesCollection(1).Values = $WorksheetTool1.Range("D10:D11","D20:D24") $chart.SeriesCollection(1).Name = "=""First"" 方法作为回调传递?

答案 1 :(得分:1)

不要在MainWindow方法中创建classb.download的实例,只需在连接信号时将其作为参数传递,这样:

#tvt= self.call_progressBar2(self) #No need for tvt
QtCore.QObject.connect(self.ui.pushButton1, QtCore.SIGNAL("clicked()"), \
                       lambda: instance_b.download(self)) #self here is the MainWindow Instance

然后在classb.download方法中:

def download(self, main_window):
    print 'a'
    main_window.call_progressBar2(20)
    print 'b'
    main_window.call_progressBar2(40)
    print 'c'
    main_window.call_progressBar2(60)
    print 'd'
    main_window.call_progressBar2(100)