简体   繁体   English

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

[英]How do I call an GUI function from Main class in Python?

I need ProgressBar widget in my_app ,When I click on the pushButton , I can not call def call_progressBar2(self,value1) in def download (self) . 我需要my_app中的ProgressBar小部件,单击pushButton ,无法在def download (self)调用def call_progressBar2(self,value1) def download (self) Can anyone help me? 谁能帮我?

main.py 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 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)

Why don't you just pass the current MainWindow instance to your download function when you connect the signal ? 当连接信号时,为什么不将当前的MainWindow实例传递给download函数呢? Or just pass it to classb initialiser when you instanciate it just before you connect the signal ? 还是在连接信号之前实例化它时将其传递给classb初始化程序? Or specify download so it takes a callback, and pass the MainWindow current instance's call_progressBar2 method as callback when you connect the signal ? 还是指定download以便进行回调,并在连接信号时将MainWindow当前实例的call_progressBar2方法作为回调传递?

Instead of creating instance of MainWindow in classb.download method, just pass it as parameter when you connect the signal, this way: 不用在classb.download方法中创建MainWindow实例,而是在连接信号时将其作为参数传递,方法是:

#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

Then in classb.download method: 然后在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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM