简体   繁体   English

从GUI类外部访问GUI元素

[英]Accessing GUI elements from outside the GUI class

I'm hoping someone can help me with a Qt designer question. 我希望有人可以帮助我解决Qt设计师的问题。 I'm trying to modify GUI elements from outside the class calling the GUI file. 我正在尝试从调用GUI文件的类外部修改GUI元素。 I've set up example code showing the structure of my programs. 我已经设置了示例代码来显示程序的结构。 My goal is to get func2 , in the main program (or another class) to change the main window's statusbar. 我的目标是在主程序(或另一个类)中获取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_())

I'm trying to get func2 to work, since I don't want my whole program to be under the StartQT4 class. 我正在尝试使func2正常工作,因为我不希望整个程序都在StartQT4类下。 I've tried many variations of that line, but can't seem to access GUI items from outside of this class. 我已经尝试了该行的许多变体,但似乎无法从此类外部访问GUI项。 I've tried sending signals as well, but still can't get the syntax right. 我也尝试过发送信号,但是仍然无法正确获取语法。

It's possible that my structure is bogus, which is why I posted most of it. 我的结构可能是虚假的,这就是为什么我发布了大部分结构的原因。 Essentially I have a .py file created by Designer, and my main program file, which imports it. 本质上,我有一个由Designer创建的.py文件,还有一个导入它的主程序文件。 The main program file has a class to initiate the GUI, (and a class for each separate window). 主程序文件具有用于启动GUI的类(以及用于每个单独窗口的类)。 It has signals in this class, that call methods in the class. 它在此类中具有信号,该信号调用该类中的方法。 These methods call functions from my main program, or other classes I've created. 这些方法从我的主程序或我创建的其他类中调用函数。 The end of the program has the if __name__ == "__main__" code, to start the GUI. 该程序的末尾具有if __name__ == "__main__"代码,以启动GUI。 Is this structure bogus? 这个结构是假的吗? I've read many tutorials online, all different, or outdated. 我已经在线阅读了许多教程,它们全都不同,或者已经过时。

Your func1 method is a way to go - since ui is a field in StartQT4 class, you should directly manipulate with its data only within the same class. 您可以使用func1方法-由于uiStartQT4类中的一个字段, StartQT4仅应在同一类中直接使用其数据进行操作。 There is nothing wrong that you have all user interface functionality for one widget in one class - it is not a big issue if you have only two classes in your code, but having several classes to reference the fields directly is potential nightmare for maintentace (what if you change the name of statusbar widget?). 在一类中具有一个小部件的所有用户界面功能是没有错的-如果您的代码中只有两个类,这不是什么大问题,但是直接使用多个类来引用字段是维护的潜在噩梦(如果您更改statusbar小部件的名称?)。

However, if you actually want to edit it from func2 , then you need to pass the reference of StartQT4 object to it, because you need to specify for what instance of window you need to change status bar message. 但是,如果您实际上要从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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM