1

This code works:

from PyQt4 import QtGui
import sys

app=QtGui.QApplication(sys.argv)


window1=QtGui.QWidget()
window1.show()

window2=QtGui.QWidget()
window2.show()

But this doesnt:

from PyQt4.QtGui import *
import sys

class window(QMainWindow):
    def __init__(self):
        super(window, self).__init__()
        self.w=QWidget()
        self.w.show()

app=QApplication(sys.argv)
window()
window()

How can i create 2 windows by instancing the class window? I don't get it with Qt, with Tkinter it is very easy to figure out...

EDIT: The question above is meant for creating some windows by clicking on a button in the systray. As you can see when executing the code below, it works, but there is just one window shown at any time, e.g. if i clicked twice the context menu of the systray icon to create two windows. I don't see where it comes from...

import sys
from PyQt4.QtGui import *


class Note(QMainWindow):
    def __init__(self):
        super(Note,self).__init__()
        self.w=QWidget()
        self.setWindowTitle("Note")
        self.setCentralWidget(self.w)

class main():
    def __init__(self):
        self.app = QApplication(sys.argv)

        self.trayIcon = QSystemTrayIcon(QIcon("J:\\python\\SimpleNotes.ico"), self.app)
        self.menu = QMenu()

        self.newWindow = self.menu.addAction("new Note")
        self.separator = self.menu.addSeparator()
        self.exitAction = self.menu.addAction("Exit")

        self.exitAction.triggered.connect(self.close)
        self.newWindow.triggered.connect(self.newNote)
        self.trayIcon.setContextMenu(self.menu)
        self.trayIcon.show()

        self.app.exec()

    def newNote(self):
        print("Create new note entry has been clicked")
        self.note=Note()
        self.note.show()

    def close(self):
        self.trayIcon.hide()
        self.app.exit()
        print("Exit menu entry has been clicked")

main()

EDIT2: Got it! The problem can be solved by coding it this way:

class main():
    def __init__(self):
        self.notes=[]
        ...

    def newNote(self):
        note=Note()
        note.show()
        self.notes.append(note)

Though still I don't know why now it works, or even no window occurs if you delete the line "self.notes.append(note)". But hah, it works!

4

1 回答 1

1

尝试这样的事情:

from PyQt4.QtGui import *
import sys

class window(QMainWindow):
    def __init__(self):
        super(window, self).__init__()
        self.w=QWidget()
        self.setCentralWidget(self.w)

app=QApplication(sys.argv)
w1 = window()
w1.show()
w2 = window()
w2.show()
app.exec()

您需要显示顶级容器,而不是内部小部件。而且您可能希望该小部件显示在主窗口内,而不是作为独立窗口显示。

您更新代码的问题是,当再次单击“添加注释”时,您将成员替换self.note为新窗口。因此,在调用之后的任何地方都不会引用前一个窗口,并且会被销毁。
如果要保持多个窗口打开,则需要在需要时对所有窗口进行处理。

警告:我实际上并不了解 python,所以简单地使用列表可能不是一个好主意——我不知道。

尝试这个:

class main():
    def __init__(self):
        self.app = QApplication(sys.argv)
        self.app.setQuitOnLastWindowClosed(False);
        self.notes = []
        ...

    def newNote(self):
        print("Create new note entry has been clicked")
        note=Note()
        note.show()
        self.notes.append(note)

setQuitOnLastWindowClosed部分在您的用例中是必需的,否则app.exec一旦您关闭所有笔记窗口就会退出,并且您的应用程序将在此时退出 - 看起来这不是您想要发生的事情。

注意:这不是QMainWindow. 这是一个“重”类,通常用作“完整”GUI 应用程序的唯一主窗口,带有菜单、工具栏、状态栏等。使用简单QWidget的 aQTextEdit和几个按钮听起来更好这个用例。实际上,您可能会摆脱NoteQTextEdit并实现上下文菜单的方式。

于 2013-05-24T11:37:43.947 回答