【问题标题】:Changing QLabel of PyQt from other python script从其他 python 脚本更改 PyQt 的 QLabel
【发布时间】:2019-10-11 10:53:28
【问题描述】:

Script1 有 Qlabel 并且 Script2 正在计算一些值。

我想要的是从脚本 2 中的编码更改 Qlabel 的内容。

比如我写的

self.button_A.clicked.connect(test)

我写的,

import script1

def test():
    self.label_A.setText("this is changed text")

关于脚本 2。

作为运行结果,self 没有被定义...

我不知道发生了什么。

【问题讨论】:

    标签: python user-interface pyqt pyqt5 uicontrol


    【解决方案1】:

    self 指的是从定义 button_A 的类创建的对象。

    例如如果代码如下:

    class MyWindow(QtGui.QMainWindow):
    
        def __init__(self):
            super().__init__()
            self.button_A = QtGui.QPushButton("Button A", self)
            self.button_A.clicked.connect(test)
    

    然后你实例化了对象:

    my_window = MyWindow()
    

    那么您必须在脚本 2 中引用 my_window 并使用以下命令调用命令:

    from script1 import my_window
    
    my_window.label_A.setText("this is changed text")
    

    【讨论】:

    • 运行更改后的脚本 2 时不会出现同样的错误,因为其中没有 self
    猜你喜欢
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2015-03-28
    • 2019-08-24
    相关资源
    最近更新 更多