PyQt4,生成同一个小部件的多个实例? [英] PyQt4, Generating multiple Instances of the same widget?

查看:48
本文介绍了PyQt4,生成同一个小部件的多个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 PyQt4 gui,它允许 qmainwindow 中的用户输入一些初始参数,然后单击启动按钮开始程序.当他们单击开始按钮时,会出现一个显示参数的新窗口,然后程序开始.我希望用户能够启动程序的多个实例.但是,当qmainwindow中的参数发生变化,再次点击开始按钮时,第一个程序窗口就消失了.

I am creating a PyQt4 gui that allows the user in a qmainwindow to input some initial parameters and then click a start button to begin a program. When they click the start button, a new window that displays the parameters appears, and the program begins. I would like the user to be able to initiate multiple instances of the program. However, when the parameters are changed in the qmainwindow and the start button is clicked a second time, the first program window disappears.

有没有办法让开始按钮调用与第一个窗口同时运行的第二个窗口?我想这会类似于线程,但从我读过的内容来看,PyQt4 似乎没有在应用程序中进行线程处理的方法.

Is there a way to have the start button call a second window that runs concurrently with the first window? I imagine this would be something like threading, but from what I have read, PyQt4 doesn't seem to have a method for threading within an application.

任何帮助将不胜感激.

推荐答案

我猜你是在同一个变量中保存对新创建的窗口的引用.如果您想创建多个窗口,请尝试将对该窗口的引用保存在一个单独的变量中,即每个窗口都应该有自己的引用变量.

I am guessing that you are saving the reference to newly created window in the same variable. If you want to create multiple windows, try to save the reference to that window in a separate variable, i.e each window should have it's own reference variable.

def showWindow(self):
    self.child = Window(self)
    self.child.show()

如果这是您的情况,第一个窗口将在第二次 showWindow() 执行后立即失去它的引用.因为self.child 会包含对第二个窗口的引用,导致关闭第一个窗口,因为第一个窗口没有引用引用.一旦小部件在 Qt 中失去引用,小部件就会被销毁.为了克服这个问题,维护一个变量列表:

If this is your situation, the first window will loose it's reference as soon as the second time showWindow() executes. Because the self.child will contain the reference to second window, resulting in closing the first window, because the first window has no reference reference. As soon as the widget looses reference in Qt, the widget will be destroyed. To overcome this problem maintain a list of variables:

# declare a list in __init__ as self.widgetList = []

def showWindow(self):
    win = Window(self):
    win.show()
    self.widgetList.append(win)

这篇关于PyQt4,生成同一个小部件的多个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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