0

我正在使用 Eric4 和 PyQt4 创建一个应用程序。

我有两个对话框,一个作为线程运行,另一个是带有标签的标准对话框,我想将其更改为图像。

每次主窗口线程运行时,我希望它将对话框中显示的当前图像更改为新图像。一切正常,除了每次线程运行时它都会创建一个包含新图像的新对话框 - 我希望它更改当前打开的对话框中的图像。

带有内部图像的对话框:

class SubWindow(QDialog, Ui_subWindow):
    def __init__(self, parent = None):
        QDialog.__init__(self, parent)
        self.setupUi(self)
        self.show()

    def main(self, img):
        pic = self.imgView
        pic.setPixmap(QtGui.QPixmap(os.getcwd() + img))

更改图像的线程:

class MainWindow(QDialog, Ui_MainWindow,  threading.Thread):
    def __init__(self, parent = None):
        threading.Thread.__init__(self)
        QDialog.__init__(self, parent)
        self.setupUi(self)
        self.show()
        #some code here which does some stuff then calls changeImg()

    def changeImg(self):
        img = SubWindow()
        img.main(img)

我没有包括我所有的代码,只有相关的位。

4

1 回答 1

0

看起来问题是SubWindow每次您希望更改图像时都在创建一个新图像。我建议在函数中创建SubWindow作为属性:MainWindowMainWindiw.__init__

class MainWindow(QDialog, Ui_MainWindow,  threading.Thread):

    def __init__(self, parent = None):
        threading.Thread.__init__(self)
        QDialog.__init__(self, parent)
        self.setupUi(self)
        self.show()
        self.img = SubWindow() # Create SubWindow once here

    def changeImg(self):
        self.img.main(self.img) # Only change the image, no new SubWindow
于 2012-05-18T22:53:03.293 回答