我已经开始从example of Zetcode学习PySide并尝试编写具有两个窗口的应用程序:“原理图视图”,它是“布局视图”的父级,每个都有菜单栏。在开始时应该只是原理图窗口,并且应该通过菜单栏根目录中的switchtoLAYOUT启动布局获胜。
我的问题是:
守则:
import sys
from PySide import QtCore, QtGui
class schematicWindow(QtGui.QMainWindow):
def __init__(self):
super(schematicWindow, self).__init__()
self.defineSchWin()
def defineSchWin(self):
exitAction = QtGui.QAction('&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(self.close)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAction)
menubar.addMenu('&Edit')
menubar.addMenu('&Passives')
menubar.addMenu('&Descretes')
menubar.addMenu('&IC\'s')
swToLayMenu = menubar.addMenu('switchtoLAYOUT')
swToLayAction = QtGui.QAction(self)
swToLayAction.triggered.connect(self.layoutWindow)
swToLayMenu.addAction(swToLayAction) # open layoutWindow (if not exists)
# and set focus to layoutWindow
self.setGeometry(0, 300, 500, 300)
self.setWindowTitle('Schematic View')
self.show()
def layoutWindow(self):
window = QtGui.QMainWindow(self)
window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
window.statusBar()
menubar = window.menuBar()
switchtoSchMenu = menubar.addMenu('switchtoSCHEMATIC')
window.setGeometry(100, 600, 500, 300)
window.setWindowTitle('Layout View')
window.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = schematicWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
答案 0 :(得分:4)
您需要在班级中保留对布局窗口的引用(您应该将self.layout_window = None
放在__init__
中)。此函数现在检查窗口是否已初始化,如果尚未显示,则确保窗口可见,然后将新窗口设置为活动窗口。类似的东西:(这没有经过测试)
def layoutWindow(self):
if self.layout_window is None:
window = QtGui.QMainWindow(self)
self.layout_window = window
window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
window.statusBar()
menubar = window.menuBar()
switchtoSchMenu = menubar.addMenu('switchtoSCHEMATIC')
window.setGeometry(100, 600, 500, 300)
window.setWindowTitle('Layout View')
else:
window = self.layout_window
window.show()
window.activateWindow()
window.raise() # just to be sure it's on top
(doc)