PyQt4中的多个Windows [英] Multiple Windows in PyQt4

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

问题描述

我有一个PyQt程序用于可视化一些python对象。我想在自己的窗口中显示多个对象。



在PyQt4中实现多窗口应用程序的最佳方法是什么?



目前我有以下内容:

 来自PyQt4 import QtGui 

class MainWindow(QtGui.QMainWindow):
windowList = []

def __init __(自我,动物):
传递

def addwindow(自我,动物)
win = MainWindow(动物)
windowList.append(win)

if __name __ ==__ main__:
import sys

app = QtGui.QApplication(sys.argv)

win = QMainWindow(dog)
win.addWindow(fish)
win.addWindow(cat)

app.exec_()

然而,这种方法并不令人满意,因为当我尝试在其自己的类中分解MultipleWindows部件时遇到问题。例如:

  class MultiWindows(QtGui.QMainWindow):
windowList = []

def __init __(self,param):
raise NotImplementedError()

def addwindow(self,param)
win = MainWindow(param)#如何调用初始化程序从这里的子类?
windowList.append(win)

类PlanetApp(MultiWindows):
def __init __(自我,行星):
通过

类AnimalApp(MultiWindows):
def __init __(self,planet):
传递

如果__name __ ==__ main__:
import sys

app = QtGui.QApplication(sys.argv)

win = PlanetApp(水银)
win.addWindow(venus)
win.addWindow(jupiter)

app.exec_()

上面的代码将调用MainWindow类的初始化程序,而不是相应的子类,因此将抛出异常。



如何调用子类的初始值设定项?有更优雅的方法吗?

解决方案

为什么不使用对话框?在Qt中,您不需要使用主窗口,除非您想使用停靠点等。使用对话框将具有相同的效果。



我还可以在你的逻辑中看到一个问题,即你希望你的超类能够调用其子类的构造函数,这当然可以是任何类型。我建议你重写它如下:

  class MultiWindows(QtGui.QMainWindow) :

def __init __(self,param):
self .__ windows = []

def addwindow(self,window):
self .__ windows .append(窗口)

def show():
for current_child_window in self .__ windows:
current_child_window.exec_()#pow show将做同样的技巧

类PlanetApp(QtGui.QDialog):
def __init __(自我,父母,星球):
QtGui.QDialog .__ init __(自我,父母)
#在这里做很酷的东西

类AnimalApp(QtGui.QDialog):
def __init __(自我,父,动物):
QtGui.QDialog .__ init __(self,parent)
#do cool东西在这里

如果__name __ ==__ main__:
import sys#真的需要这个吗?

app = QtGui.QApplication(sys.argv)

jupiter = PlanetApp(无,jupiter)
venus = PlanetApp(无,venus)
windows = MultiWindows()
windows.addWindow(jupiter)
windows.addWindow(venus)

windows.show()
app.exec_( )


期待这个不是一个好主意超类知道要在其子类的 init 中使用的参数,因为很难确保所有构造函数都是相同的(可能动物对话框/窗口采用diff参数)。 / p>

希望有所帮助。


I have a PyQt program used to visualize some python objects. I would like to do display multiple objects, each in its own window.

What is the best way to achieve multi-window applications in PyQt4?

Currently I have the following:

from PyQt4 import QtGui

class MainWindow(QtGui.QMainWindow):
    windowList = []

    def __init__(self, animal):
        pass

    def addwindow(self, animal)
        win = MainWindow(animal)
        windowList.append(win)

if __name__=="__main__":
    import sys

    app = QtGui.QApplication(sys.argv)

    win = QMainWindow(dog)
    win.addWindow(fish)
    win.addWindow(cat)

    app.exec_()

However, this approach is not satisfactory, as I am facing problems when I try to factor out the MultipleWindows part in its own class. For example:

class MultiWindows(QtGui.QMainWindow):
    windowList = []

    def __init__(self, param):
        raise NotImplementedError()

    def addwindow(self, param)
        win = MainWindow(param) # How to call the initializer of the subclass from here?
        windowList.append(win)

class PlanetApp(MultiWindows):
    def __init__(self, planet):
        pass

class AnimalApp(MultiWindows):
    def __init__(self, planet):
        pass

if __name__=="__main__":
    import sys

    app = QtGui.QApplication(sys.argv)

    win = PlanetApp(mercury)
    win.addWindow(venus)
    win.addWindow(jupiter)

    app.exec_()

The above code will call the initializer of the MainWindow class, rather than that of the appropriate subclass, and will thus throw an exception.

How can I call the initializer of the subclass? Is there a more elegant way to do this?

解决方案

Why not using dialogs? In Qt you do not need to use the main window unless you want to use docks etc.. Using dialogs will have the same effect.

I can also see a problem in your logic regarding the fact that you want your super class to be calling the constructor of its children, which of course can be any type. I recommend you rewrite it like the following:

class MultiWindows(QtGui.QMainWindow):

    def __init__(self, param):
        self.__windows = []

    def addwindow(self, window):
        self.__windows.append(window)

    def show():
        for current_child_window in self.__windows:
             current_child_window.exec_() # probably show will do the same trick

class PlanetApp(QtGui.QDialog):
    def __init__(self, parent, planet):
       QtGui.QDialog.__init__(self, parent)
       # do cool stuff here

class AnimalApp(QtGui.QDialog):
    def __init__(self, parent, animal):
       QtGui.QDialog.__init__(self, parent)
       # do cool stuff here

if __name__=="__main__":
    import sys # really need this here??

    app = QtGui.QApplication(sys.argv)

    jupiter = PlanetApp(None, "jupiter")
    venus = PlanetApp(None, "venus")
    windows = MultiWindows()
    windows.addWindow(jupiter)
    windows.addWindow(venus)

    windows.show()
    app.exec_()

It is not a nice idea to expect the super class to know the parameter to be used in the init of its subclasses since it is really hard to ensure that all the constructor will be the same (maybe the animal dialog/window takes diff parameters).

Hope it helps.

这篇关于PyQt4中的多个Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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