【问题标题】:PySide: How to change the status bar text from another class?PySide:如何从另一个类更改状态栏文本?
【发布时间】:2014-06-14 07:57:39
【问题描述】:

我正在尝试使用 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()

【问题讨论】:

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


    【解决方案1】:

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

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

    使用parent() 方法到达主窗口:

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

    【讨论】:

    • 感谢您的回复。这真的很有帮助。
    猜你喜欢
    • 2018-08-01
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2020-03-25
    • 2019-10-06
    • 1970-01-01
    相关资源
    最近更新 更多