仍然是Python的新手,所以我试图让QGraphicsScene进入QMainWindow。现在,我编码的方式是两个窗口会出现,但我只想要一个窗口出现QGainWindow“内部”的QGraphicsScene。这是我的代码:
import sys
from PyQt4 import QtGui, QtCore
class MyView(QtGui.QGraphicsView):
def __init__(self):
QtGui.QGraphicsView.__init__(self)
self.scene = QtGui.QGraphicsScene(self)
self.item = QtGui.QGraphicsRectItem(300,400,100,100)
self.scene.addItem(self.item)
self.setScene(self.scene)
class Window(QtGui.QMainWindow):
def __init__(self):
#This initializes the main window or form
super(Window,self).__init__()
self.setGeometry(50,50,900,700)
self.setWindowTitle("Pre-Alignment system")
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
view = MyView()
view.show()
sys.exit(app.exec_())
run()
非常感谢!
答案 0 :(得分:1)
Qt(和其他GUI工具包)相信养育子女。在另一个窗口或窗口小部件中设置其父窗口。请注意,您应该在外部调用show。
import sys
from PyQt4 import QtGui, QtCore
class MyView(QtGui.QGraphicsView):
def __init__(self, parent=None):
QtGui.QGraphicsView.__init__(self, parent=parent)
self.scene = QtGui.QGraphicsScene(self)
self.item = QtGui.QGraphicsRectItem(300,400,100,100)
self.scene.addItem(self.item)
self.setScene(self.scene)
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
#This initializes the main window or form
super(Window,self).__init__(parent=parent)
self.setGeometry(50,50,900,700)
self.setWindowTitle("Pre-Alignment system")
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
view = MyView(GUI)
GUI.show()
sys.exit(app.exec_())
run()
答案 1 :(得分:0)
您必须在QGraphicsScene
内创建QMainWindow
(就像您希望它出现一样)
然后在主要内容中,您只创建Window
的实例并显示它。
class Window(QtGui.QMainWindow):
def __init__(self):
#This initializes the main window or form
super(Window,self).__init__()
self.setGeometry(50,50,900,700)
self.setWindowTitle("Pre-Alignment system")
#create the view
self.view=MyView()
self.setCentralWidget(self.view)
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())