Python 3 pyQt4 使用来自多个模块/类的变量更新 GUI [英] Python 3 pyQt4 updating GUI with variables from multiple modules/classes

查看:58
本文介绍了Python 3 pyQt4 使用来自多个模块/类的变量更新 GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个包含嵌套类/线程和多个模块的大型程序.我现在想添加一个简单的 GUI 和一些标签来显示一些变量.但是,变量分散在整个模块和类中.我正在寻找一种方法将这些变量更新到 GUI 中而不改变现在的代码太多了.

I've written a large program with nested classes/threads and multiple modules. I would now like to add a simple GUI and some Labels to display some variables. However, the variables are scattered throughout the modules and classes. I'm looking for a way to update these variables into the GUI without altering the current code too much.

我对 Pyqt4 有基本的了解(我也会接受 tkinter 的答案).

I have a rudimentary understanding of Pyqt4 (i will accept tkinter answers also).

我尝试不使用信号/发射,因为据我所知发射必须从 Qthread 发送,这意味着对我的代码进行彻底检查,更改类和线程转移到 Qthreads.如果可能的话,我想避免这样做.这是我尝试过的一个例子.

I've tried not to use signals/emits because to my knowledge emits must be sent from a Qthread which would mean a complete overhaul of my code, changing classes and threads over to Qthreads. Id like to avoid needing to do this if possible. Here is one example I've attempted.

test.py

class Update(Thread): 
    def __init__(self): 
        Thread.__init__(self) 
    def run(self): 

        for i in range(10): 
            time.sleep(2) 
            import test 
            wa.label.setText(str(i)) 

class MyWindow(QWidget):  
    def __init__(self, *args):  
        QWidget.__init__(self, *args) 

        self.label = QLabel(" ") 
        layout = QVBoxLayout() 
        layout.addWidget(self.label) 
        self.setLayout(layout) 

        Update1 = Update() 
        Update1.start() 
        Update1.refresh1 = 'ba' 

        self.label.setText(Update1.refresh1) 


if __name__ == "__main__":  
    app = QApplication(sys.argv)  
    wa = MyWindow()  
    wa.show()  
    sys.exit(app.exec_()) 

此代码有效,但我的变量需要从其他模块/类或线程更新.我将类更新"移动到像这样的新模块的那一刻:

This code works, but my variables need to update from other modules/classes or threads. The moment I move 'class Update' into a new module like THIS:

test.py

import test2 


class MyWindow(QWidget):  
    def __init__(self, *args):  
        QWidget.__init__(self, *args) 

        self.label = QLabel(" ") 
        layout = QVBoxLayout() 
        layout.addWidget(self.label) 
        self.setLayout(layout) 

        Update1 = test2.Update() 
        Update1.start() 
        Update1.refresh1 = 'ba' 

        self.label.setText(Update1.refresh1) 


if __name__ == "__main__":  
    app = QApplication(sys.argv)  
    wa = MyWindow()  
    wa.show()  
    sys.exit(app.exec_()) 

test2.py #updates GUI

test2.py #updates GUI

class Update(Thread): 
    def __init__(self): 
        Thread.__init__(self) 
    def run(self): 

        for i in range(10): 
            time.sleep(2) 
            import test 
            test.wa.label.setText(str(i)) 

我得到:AttributeError: 'module' object has no attribute 'wa'

此外,我还考虑将类 Update() 放入 Qthread,从已更新变量的任何模块/类运行它,并使用 Update() 中的发射函数.这将解决必须将我当前的类/线程更改为 Qthreads 的问题.

Also, I was also considering putting class Update() into a Qthread, running it from any module/class where a variable has been updated and using the emit function inside Update(). this would solve having to change my current classes/threads to be Qthreads.

如果有人知道一种简单的方法,我可以通过调用类似 update() 之类的类来更新我的 GUI,我们将不胜感激

If anyone knows of a simple way I could update my GUI simply by calling a class like update() an example would be appreciated

推荐答案

因为 wa 仅在 __name__ == "__main__" 时设置,并且仅在 时发生>test.py 是主文件.

Because wa is only set when __name__ == "__main__" and that only happens when test.py is the main file.

当您执行 import test 时,您正在运行 test.py 文件的另一个实例,它不是主脚本,因此它具有 __name__ == 'test' 不是 __main__.因此,即使设置了 wa,您也会更改它的另一个实例.

When you do import test, you are running another instance of the test.py file which is not the main script so it has __name__ == 'test' not __main__. So, even if wa was set, you would be changing another instance of it.

可能的解决方案:

您可以获得对 __main__ 模块的引用并在 test2.py 模块上进行设置:

You can get a reference to the __main__ module and set on the test2.py module:

test.py 上:

import test2
test2.parent = sys.modules[__name__]

现在,在 test2.py 上(不要导入 test,但要确保 test 导入 test2):

Now, on test2.py (do not import test but make sure test imports test2):

parent.wa.label.setText('Blablabla')

这篇关于Python 3 pyQt4 使用来自多个模块/类的变量更新 GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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