2017-04-21 68 views
2

我的问题是: 如何从包中更改标签(或其他图形元素)? 这个想法是减轻我的主要方案。 谢谢!PyQt:如何从包中更改标签文本

前主程序:

#../mainprogram.py 
#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

from PyQt5 import QtWidgets 
from ui import Ui_MainWindow 
from package import update 

class MainWindow(QtWidgets.QMainWindow): 
    def __init__(self): 
     super(MainWindow, self).__init__() 
     self.ui = Ui_MainWindow() 
     self.ui.setupUi(self) 

     # label from .ui -> .py 
     self.ui.label_1.setText("need to change this") 

    def update_label(self): 
     self.update = update.label_update() 

前包:

#../package/update.py 
#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

def label_update(): 
    self.ui.label_1.setText("no problem") 

回答

1

什么,你需要做的是通过对函数传递对象的实例。考虑:

def label_update(): 
    self.ui.label_1.setText("no problem") 

在这个范围内,我们不知道self是什么,因为它没有被定义。但是,如果通过self

#../mainprogram.py 
class MainWindow(QtWidgets.QMainWindow): 
    def update_label(self): 
     self.update = update.label_update(self) 


#../package/update.py 
def label_update(obj): #obj is the object self 
    obj.ui.label_1.setText("no problem") 

我们正在更新已传递到函数的对象。

+0

它很好用,谢谢! 在我的情况下,我需要存储上述过程,将像素图从绿色变为红色。 这是减轻主程序的最佳方式吗? – cheetOos