我編寫了一個程序,在系統托盤中創建一個圖標,然後單擊鼠標右鍵顯示3點的上下文菜單注意:通過按菜單創建新註釋窗體創建新註釋並退出應該出現,但並未出現。爲什麼?窗口不出現
from PyQt4 import QtCore, QtGui, uic
import sys
class NoteForm(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
uic.loadUi("note.ui", self)
def main():
app = QtGui.QApplication(sys.argv)
tray = QtGui.QSystemTrayIcon()
icon = app.style().standardIcon(QtGui.QStyle.SP_DesktopIcon)
tray.setIcon(icon)
tray.show()
CreateMenu(tray, app)
sys.exit(app.exec_())
def CreateMenu(tray, app):
m1 = QtGui.QMenu("Menu 1")
m2 = QtGui.QMenu("Notes", m1)
actNewNote=QtGui.QAction("Create new note", m1)
actNewNote.triggered.connect(viewNote)
#m1.addAction("Create new note", viewNote)
m1.addMenu(m2)
m1.addSeparator()
m1.addAction("Quit", app.quit)
tray.setContextMenu(m1)
def viewNote():
note=NoteForm()
note.show()
if __name__ == '__main__':
main()
note.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>WindowNote</class>
<widget class="QDialog" name="WindowNote">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>707</width>
<height>356</height>
</rect>
</property>
<property name="windowTitle">
<string>Заметка</string>
</property>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>671</width>
<height>331</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="3">
<widget class="QLineEdit" name="TitleNote"/>
</item>
<item row="1" column="0" colspan="3">
<widget class="QTextEdit" name="textNote"/>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="buttonOK">
<property name="text">
<string>OK</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="buttonCancel">
<property name="text">
<string>Отмена</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="buttonDeleteNote">
<property name="text">
<string>Удалить заметку</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
這是完全錯誤的。 OP已經在'main()'中啓動了主事件循環,並且這是菜單信號處理工作所必需的。 NoteForm類是一個QDialog,它有自己的事件循環。 – ekhumoro
爲什麼你不會竊取我的答案的其餘部分,並且在'note.exec _()'之前擺脫多餘的'note.show()'行? – ekhumoro
我沒有「偷」你的答案我回去並根據這個網站修復它,http://www.riverbankcomputing.com/static/Docs/PyQt4/html/qdialog.html#exec – enginefree