Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 PySide:如何从另一个类更改状态栏文本?_Python 2.7_User Interface_Pyqt_Pyside_Statusbar - Fatal编程技术网

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

Python 2.7 PySide:如何从另一个类更改状态栏文本?,python-2.7,user-interface,pyqt,pyside,statusbar,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__()

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

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

...
self.mytabs_widget = QtGui.QTabWidget(self)
...
使用父方法到达主窗口:

这里self.parent为您提供tabWidget,因此self.parent.parent为您提供主窗口

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

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

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

...
self.mytabs_widget = QtGui.QTabWidget(self)
...
使用父方法到达主窗口:

这里self.parent为您提供tabWidget,因此self.parent.parent为您提供主窗口

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

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

谢谢你的回复。这真的很有帮助。谢谢你的回复。这真的很有帮助。