Python 显示没有主窗口的Qwidget窗口

Python 显示没有主窗口的Qwidget窗口,python,pyqt,qwidget,Python,Pyqt,Qwidget,我在显示QWidget窗口以供用户输入一些数据时遇到问题 我的脚本没有GUI,但我只想显示这个小QWidget窗口 我使用QtDesigner创建了窗口,现在我尝试如下方式显示QWidget窗口: from PyQt4 import QtGui from input_data_window import Ui_Form class childInputData(QtGui.QWidget ): def __init__(self, parent=None): supe

我在显示QWidget窗口以供用户输入一些数据时遇到问题

我的脚本没有GUI,但我只想显示这个小QWidget窗口

我使用QtDesigner创建了窗口,现在我尝试如下方式显示QWidget窗口:

from PyQt4 import QtGui
from input_data_window import Ui_Form

class childInputData(QtGui.QWidget ):

    def __init__(self, parent=None):
        super(childInputData, self).__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.setFocus(True)
        self.show()
import sys
from PyQt4 import QtGui
from input_data_window import Ui_Form

class childInputData(QtGui.QWidget):
  def __init__(self, parent=None):
    super(childInputData, self).__init__(parent)
    self.ui = Ui_Form()
    self.ui.setupUi(self)
    self.setFocus(True)

if __name__ == "__main__":
 app = QtGui.QApplication(sys.argv)
 win = childInputData()
 win.show()
 sys.exit(app.exec_())
然后,在我的主课上,我是这样做的:

class myMainClass():

    childWindow = childInputData()
这给了我一个错误:

QWidget: Must construct a QApplication before a QPaintDevice
所以现在我在我的主课上做:

class myMainClass():

    app = QtGui.QApplication(sys.argv)
    childWindow = childInputData() 
现在没有错误,但窗口显示两次,脚本不会等到输入数据,它只显示窗口,不等待继续


这里出了什么问题?

您不需要
myMainClass
…执行以下操作:

from PyQt4 import QtGui
from input_data_window import Ui_Form

class childInputData(QtGui.QWidget ):

    def __init__(self, parent=None):
        super(childInputData, self).__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.setFocus(True)
        self.show()
import sys
from PyQt4 import QtGui
from input_data_window import Ui_Form

class childInputData(QtGui.QWidget):
  def __init__(self, parent=None):
    super(childInputData, self).__init__(parent)
    self.ui = Ui_Form()
    self.ui.setupUi(self)
    self.setFocus(True)

if __name__ == "__main__":
 app = QtGui.QApplication(sys.argv)
 win = childInputData()
 win.show()
 sys.exit(app.exec_())

显示窗口并继续执行脚本是完全正常的:您从未告诉脚本等待用户回答。你刚刚告诉它要展示一扇窗户

您希望脚本停止,直到用户完成并关闭窗口

这里有一种方法:

from PyQt4 import QtGui,QtCore
import sys

class childInputData(QtGui.QWidget):

    def __init__(self, parent=None):
        super(childInputData, self).__init__()
        self.show()

class mainClass():

    def __init__(self):
        app=QtGui.QApplication(sys.argv)
        win=childInputData()
        print("this will print even if the window is not closed")
        app.exec_()
        print("this will be print after the window is closed")

if __name__ == "__main__":
    m=mainClass()
exec()
方法”进入主事件循环,并等待exit()被调用“():
在窗口关闭之前,脚本将被阻止在
app.exec\行()

注意:当窗口关闭时,使用
sys.exit(app.exec())
将导致脚本结束


另一种方法是使用
QDialog
而不是
QWidget
。然后将
self.show()
替换为
self.exec()
,这将阻止脚本

从:

int QDialog::exec()

将对话框显示为模式对话框,直到用户关闭为止



最后,一个相关问题的建议是不要使用
exec
,而是使用
win.setwindowmodial(QtCore.Qt.ApplicationModal)
设置窗口模态。但是,这在这里不起作用:它会阻止其他窗口中的输入,但不会阻止脚本。

谢谢jramm,我知道我可以做到,我一直在这样做我的GUI,但在我的问题中,我的意思是我想从我的类中显示小部件窗口,这是不可能的?我不明白如果你将
win.show()
移动到类的
\uuuu init\uuuuu
方法,为什么它不起作用……但是它有什么区别呢?再次感谢你,没有区别,只是想知道这是否可能在窗口外显示两次,我看不出为什么在你发布的代码中。你确定你没有在代码中的其他地方调用
show()
吗?是的,我是:S…..我的错谢谢这正是我想要的行为不。这两个页面都将在qwidget案例中打开。也许你在qdialog案上是对的