PyQT - modifying main window widgets from other functions

A PyQT beginner question. I'm wondering how to do something like the following - 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!

Answer1:

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

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:PyQT - modifying main window widgets from other functions