PySide:如何从另一个类更改状态栏文本?

时间:2014-04-28 16:47:29

标签: python-2.7 user-interface pyqt pyside statusbar

我正在尝试使用PySide设计GUI,我打算进行一些处理并更新状态栏。但是,我的代码中出现了问题。有人可以看看让我知道我做错了什么吗?特别是我在SampleTab1类下调用process()方法的方式。

import sys
from PySide import QtGui

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        # Setup the window
        self.resize(750, 550)
        self.myGUI()

    def myGUI(self):
        # create tab widget
        self.mytabs_widget = QtGui.QTabWidget()
        self.mytabs_widget.addTab(SampleTab1(self.mytabs_widget), "Tab 1")

        # create the layout area for tab widget
        self.mylayout = QtGui.QHBoxLayout()
        self.mylayout.addWidget(self.mytabs_widget)

        # create content area widget for padding
        self.mycontent_widget = QtGui.QWidget()
        self.mycontent_widget.setContentsMargins(5, 5, 5, 0)
        self.mycontent_widget.setLayout(self.mylayout)

        # set the central widget
        self.setCentralWidget(self.mycontent_widget)
        self.setWindowTitle("Tab Example")

        # Create a status bar with the progress information.    
        self.statusText = QtGui.QLabel("Ready")
        self.statusBar().addWidget(self.statusText, 1) 


class SampleTab1(QtGui.QWidget):
    def __init__(self, parent=None):
        super(SampleTab1, self).__init__(parent)
        label = QtGui.QLabel('Sample tab 1', self)
        label.move(15, 10)
        self.show()
        self.process()

    def process(self):
        MainWindow.statusText.setText("Processing")


def main():    
    try:
        app = QtGui.QApplication(sys.argv) 
    except:
        app = QtGui.QApplication.instance()            
    app.aboutToQuit.connect(app.deleteLater) 
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:3)

您可以通过父子层次结构访问mainWindow,将以下行添加到您的代码中: 将mainWindow设置为tabWidget的父级(将self放在括号内)

...
self.mytabs_widget = QtGui.QTabWidget(self)
...

使用parent()方法转到mainWindow:

...
def process(self):
    self.parent().parent().statusBar().showMessage("Processing")
...

这里self.parent()给你tabWidget,因此self.parent()。parent()给你mainWindow

我将您发布的代码更新为:

import sys
from PySide import QtGui

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        # Setup the window
        self.resize(750, 550)
        self.myGUI()

    def myGUI(self):
        # create tab widget
        self.mytabs_widget = QtGui.QTabWidget(self)
        self.mytabs_widget.addTab(SampleTab1(self.mytabs_widget), "Tab 1")

        # create the layout area for tab widget
        self.mylayout = QtGui.QHBoxLayout()
        self.mylayout.addWidget(self.mytabs_widget)

        # create content area widget for padding
        self.mycontent_widget = QtGui.QWidget()
        self.mycontent_widget.setContentsMargins(5, 5, 5, 0)
        self.mycontent_widget.setLayout(self.mylayout)

        # set the central widget
        self.setCentralWidget(self.mycontent_widget)
        self.setWindowTitle("Tab Example")

        # Create a status bar with the progress information.    
        self.statusText = QtGui.QLabel("Ready")
        self.statusBar().addWidget(self.statusText, 1) 


class SampleTab1(QtGui.QWidget):
    def __init__(self, parent=None):
        super(SampleTab1, self).__init__(parent)
        label = QtGui.QLabel('Sample tab 1', self)
        label.move(15, 10)
        self.show()
        self.process()

    def process(self):
        self.parent().parent().statusBar().showMessage("Processing")


def main():    
    try:
        app = QtGui.QApplication(sys.argv) 
    except:
        app = QtGui.QApplication.instance()            
    app.aboutToQuit.connect(app.deleteLater) 
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()