質問

I am trying to design a GUI using PySide and I intend to do some processing and update the status bar. However, there's something wrong in my code. Can someone look at let me know what I am doing wrong? Especially the way I call process() method under SampleTab1 class.

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()
役に立ちましたか?

解決

You can acces the mainWindow via parent child hierarchy, add the following lines to your code: set mainWindow as parent of tabWidget (put self inside brackets)

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

Reach to the mainWindow using parent() method:

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

Here self.parent() gives you the tabWidget hence, self.parent().parent() gives you the mainWindow

I updated your posted code as:

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()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top