このコードは機能します:
from PyQt4 import QtGui
import sys
app=QtGui.QApplication(sys.argv)
window1=QtGui.QWidget()
window1.show()
window2=QtGui.QWidget()
window2.show()
しかし、これはしません:
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()
クラスウィンドウをインスタンス化して2つのウィンドウを作成するにはどうすればよいですか? Qtではわかりませんが、Tkinterでは簡単に理解できます...
編集:上記の質問は、システムトレイのボタンをクリックしていくつかのウィンドウを作成するためのものです。以下のコードを実行するとわかるように、動作しますが、いつでも表示されるウィンドウは 1 つだけです。たとえば、systray アイコンのコンテキスト メニューを 2 回クリックして 2 つのウィンドウを作成した場合などです。どこから来たのかわかりません...
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:わかりました!この問題は、次のようにコーディングすることで解決できます。
class main():
def __init__(self):
self.notes=[]
...
def newNote(self):
note=Note()
note.show()
self.notes.append(note)
なぜ今はうまくいくのか、「self.notes.append(note)」という行を削除するとウィンドウが表示されないのかはまだわかりません。しかし、はぁ、それはうまくいきます!