[英]Python PyQt4 open from QDialog new QWidget window
By pressing a QPushButton in my QDialog window I want to open a new QWidget window. 通过在我的QDialog窗口中按下QPushButton,我想打开一个新的QWidget窗口。 My code:
我的代码:
from PyQt4 import QtGui
import sys
class MainWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle("Main Window")
class FirstWindow(QtGui.QDialog):
def __init__(self, parent=None):
super(FirstWindow, self).__init__(parent)
self.createWindow()
def createWindow(self):
btn = QtGui.QPushButton('Open New Window', self)
btn.move(10, 10)
self.openNewWindow = MainWindow(self)
btn.clicked.connect(self.openMainWin)
self.setGeometry(250,250, 150,50)
self.setWindowTitle("First Window")
self.show()
def openMainWin(self):
self.openNewWindow.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
firstwin = FirstWindow()
sys.exit(app.exec_())
When I run the code nothing happens by pressing the button. 当我运行代码时,按下按钮不会发生任何事情。
But when I change the class from class MainWindow(QtGui.QWidget)
to class MainWindow(QtGui.QDialog)
or class MainWindow(QtGui.QMainWindow)
it works! 但是,当我将类从
class MainWindow(QtGui.QWidget)
更改为class MainWindow(QtGui.QWidget)
class MainWindow(QtGui.QDialog)
或class MainWindow(QtGui.QMainWindow)
它将起作用!
What am I doing wrong?! 我究竟做错了什么?! Please assist me.
请帮我。
When you instantiate MainWindow
you pass in a parent. 实例化
MainWindow
您传入一个父级。 Qwidget
only makes a new window if you don't specify a parent. 如果您未指定父项,则
Qwidget
只会创建一个新窗口。
This is of course deliberate. 这当然是故意的。 If
QWidgets
with parents were shown in new windows, then you could never build a GUI. 如果带有父项的
QWidgets
显示在新窗口中,则您将永远无法构建GUI。 Imagine having every widget in it's own window! 想象一下,每个小部件都在自己的窗口中!
QMainWindow
and QDialog
are specifically designed to both have a parent, and create a new window. QMainWindow
和QDialog
专门设计为都有一个父级,并创建一个新窗口。 You should use them. 您应该使用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.