Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.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 从GUI类外部访问GUI元素_Python_Qt_Pyqt_Qt Designer - Fatal编程技术网

Python 从GUI类外部访问GUI元素

Python 从GUI类外部访问GUI元素,python,qt,pyqt,qt-designer,Python,Qt,Pyqt,Qt Designer,我希望有人能帮我解决一个问题。我试图从调用GUI文件的类外部修改GUI元素。我已经设置了显示程序结构的示例代码。我的目标是在主程序(或其他类)中获取func2,以更改主窗口的状态栏 from PyQt4 import QtCore, QtGui from main_gui import Ui_Main from about_gui import Ui_About #main_gui and about_gui are .py files generated by designer and pyu

我希望有人能帮我解决一个问题。我试图从调用GUI文件的类外部修改GUI元素。我已经设置了显示程序结构的示例代码。我的目标是在主程序(或其他类)中获取
func2
,以更改主窗口的状态栏

from PyQt4 import QtCore, QtGui
from main_gui import Ui_Main
from about_gui import Ui_About
#main_gui and about_gui are .py files generated by designer and pyuic

class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Main()
        self.ui.setupUi(self)

        self.ui.actionMyaction.triggered.connect(self.func1)
    #Signals go here, and call this class's methods, which call other methods.
        #I can't seem to call other methods/functions directly, and these won't take arguments.

    def func1(self):
    #Referenced by the above code. Can interact with other classes/functions.
        self.ui.statusbar.showMessage("This works!")


def func2(self):
   StartQT4.ui.statusbar.showMessage("This doesn't work!")
    #I've tried many variations of the above line, with no luck.

#More classes and functions not directly-related to the GUI go here; ie the most of the program.

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())
我正试图让
func2
工作,因为我不希望我的整个程序都在StartQT4类下。我尝试过该行的许多变体,但似乎无法从该类之外访问GUI项目。我也尝试过发送信号,但仍然无法正确理解语法


有可能我的结构是假的,这就是为什么我发布了大部分内容。基本上,我有一个由Designer创建的
.py
文件,以及导入它的主程序文件。主程序文件有一个类来启动GUI(每个单独的窗口有一个类)。它在这个类中有信号,调用类中的方法。这些方法从我的主程序或我创建的其他类调用函数。程序的末尾有
如果uuuu name\uuuuuu==“\uuuuuu main\uuuuuu”
代码来启动GUI。这个结构是假的吗?我在网上读过很多教程,都是不同的或过时的。

你的
func1
方法是一个不错的选择-因为
ui
StartQT4
类中的一个字段,你应该直接在同一个类中操作它的数据。在一个类中拥有一个小部件的所有用户界面功能没有什么错-如果代码中只有两个类,这不是一个大问题,但是有几个类直接引用字段可能是维护的噩梦(如果更改
statusbar
widget的名称怎么办?)

但是,如果确实要从
func2
编辑它,则需要将
StartQT4
对象的引用传递给它,因为您需要指定需要更改状态栏消息的窗口的实例

def func2(qtWnd): # Self should go here if func2 is beloning to some class, if not, then it is not necessary
   qtWnd.ui.statusbar.showMessage("This should work now!")

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    func2(myapp)
    sys.exit(app.exec_())

您的
func1
方法是一种方法-由于
ui
StartQT4
类中的一个字段,因此您应该仅在同一类中直接使用其数据进行操作。在一个类中拥有一个小部件的所有用户界面功能没有什么错-如果代码中只有两个类,这不是一个大问题,但是有几个类直接引用字段可能是维护的噩梦(如果更改
statusbar
widget的名称怎么办?)

但是,如果确实要从
func2
编辑它,则需要将
StartQT4
对象的引用传递给它,因为您需要指定需要更改状态栏消息的窗口的实例

def func2(qtWnd): # Self should go here if func2 is beloning to some class, if not, then it is not necessary
   qtWnd.ui.statusbar.showMessage("This should work now!")

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    func2(myapp)
    sys.exit(app.exec_())

非常感谢。您描述引用类实例的答案解决了我的问题。将func2中的“StartQT4”替换为“myapp”解决了这个问题。我还采纳了您关于将实例作为参数传递的建议。谢谢。您描述引用类实例的答案解决了我的问题。将func2中的“StartQT4”替换为“myapp”解决了这个问题。我还采纳了您关于将实例作为参数传递的建议。