如何引用当前打开的对话框?

2024-09-30 01:24:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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))

改变图像的线程:

^{pr2}$

我没有包括我所有的代码,只有相关的部分。在


Tags: 图像self应用程序img标准initdef标签
1条回答
网友
1楼 · 发布于 2024-09-30 01:24:37

问题似乎是每次您希望更改图像时都会创建一个新的SubWindow。我建议在MainWindiw.__init__函数中将SubWindow创建为MainWindow的属性:

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

相关问题 更多 >

    热门问题