从 GUI 类外部访问 GUI 元素 [英] Accessing GUI elements from outside the GUI class

查看:30
本文介绍了从 GUI 类外部访问 GUI 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm hoping someone can help me with a Qt designer question. I'm trying to modify GUI elements from outside the class calling the GUI file. 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.

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 项目.我也尝试过发送信号,但仍然无法获得正确的语法.

I'm trying to get func2 to work, since I don't want my whole program to be under the StartQT4 class. I've tried many variations of that line, but can't seem to access GUI items from outside of this class. I've tried sending signals as well, but still can't get the syntax right.

我的结构可能是假的,这就是我发布大部分内容的原因.本质上,我有一个由 Designer 创建的 .py 文件,以及导入它的主程序文件.主程序文件有一个类来启动 GUI,(每个单独的窗口都有一个类).它在这个类中有信号,调用类中的方法.这些方法从我的主程序或我创建的其他类调用函数.程序末尾有 if __name__ == "__main__" 代码,用于启动 GUI.这个结构是假的吗?我在网上阅读了很多教程,都是不同的或过时的.

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. The main program file has a class to initiate the GUI, (and a class for each separate window). 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. Is this structure bogus? I've read many tutorials online, all different, or outdated.

推荐答案

你的 func1 方法是一种方法 - 因为 uiStartQT4 中的一个字段 类,你应该只在同一个类中直接操作它的数据.您在一个类中拥有一个小部件的所有用户界面功能并没有错 - 如果您的代码中只有两个类,这不是什么大问题,但是有几个类直接引用这些字段对于维护来说是潜在的噩梦(什么如果您更改 statusbar 小部件的名称?).

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

但是,如果你真的想从func2编辑它,那么你需要将StartQT4对象的引用传递给它,因为你需要指定什么<需要更改状态栏消息的强>实例窗口.

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.

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

这篇关于从 GUI 类外部访问 GUI 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆