开发者

PyQT - modifying main window widgets from other functions

开发者 https://www.devze.com 2023-03-06 03:38 出处:网络
A PyQT beginner question.I\'m wondering how to do something like the following -开发者_StackOverflow中文版 modify widgets in the main window from outside the main window class.Like so:

A PyQT beginner question. I'm wondering how to do something like the following -开发者_StackOverflow中文版 modify widgets in the main window from outside the main window class. Like so:

class MainWindow(QtGui.QMainWindow):    

    def __init__(self, parent=None):

        super(MainWindow,self).__init__(parent)

        self.ui = Ui_MainWindow()        
        self.ui.setupUi(self)

        self.ui.progressBar.setMaximum(100)
        self.ui.progressBar.setMinimum(0)
        self.ui.progressBar.setValue(0)

        self.connect(self.ui.pushButton, QtCore.SIGNAL('clicked()'), self.slotDoStuff)

    def slotDoStuff(self):
        AnotherFunction()


def AnotherFunction():    
    modify progress bar here...

Is there a way to do something like this? I'd like to subclass the event handlers for various main window actions and not have them all in the MainWindow class. Thanks!


First, there's a much better way to connect signals to slots on PyQt:

self.button.clicked.connect(self.method)

You can use lambda functions to pass extra arguments to methods.

def do_stuff(arg)
     #do stuff with arg

Then you call

self.button1.clicked.connect(lambda : do_stuff('btn one'))
self.button2.clicked.connect(lambda : do_stuff('btn two'))

You can pass whatever you want, including your MainWindow instance to be modified

0

精彩评论

暂无评论...
验证码 换一张
取 消