【问题标题】:PyQT - modifying main window widgets from other functionsPyQT - 从其他功能修改主窗口小部件
【发布时间】:2011-08-26 20:50:57
【问题描述】:

一个 PyQT 初学者问题。我想知道如何执行以下操作 - 从主窗口类外部修改主窗口中的小部件。像这样:

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...

有没有办法做这样的事情?我想为各种主窗口操作子类化事件处理程序,而不是将它们全部放在 MainWindow 类中。谢谢!

【问题讨论】:

    标签: python pyqt


    【解决方案1】:

    首先,有一种更好的方法可以将信号连接到 PyQt 上的插槽:

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

    您可以使用 lambda 函数将额外的参数传递给方法。

    def do_stuff(arg)
         #do stuff with arg
    

    然后你打电话

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

    你可以传递任何你想要的东西,包括你要修改的 MainWindow 实例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多