我用Qt设计师创建了两个窗口。现在在第三个文件中,我正在尝试导入类并通过单击另一个中的QPushButton
来打开其中一个类。
第一个main_gui.py
,就像这样:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
...
...
...
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
第二个single_plot_gui.py
具有相同的序言,它看起来像这样:
class Ui_Form_single_plot(object):
def setupUi(self, Form_single_plot):
Form_single_plot.setObjectName(_fromUtf8("Form_single_plot"))
...
...
...
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form_single_plot = QtGui.QWidget()
ui = Ui_Form_single_plot()
ui.setupUi(Form_single_plot)
Form_single_plot.show()
sys.exit(app.exec_())
现在我的第三个档案中有:
from main_gui import Ui_Form
from single_plot_gui import Ui_Form_single_plot
class SinglePlotWindow(QtGui.QMainWindow, Ui_Form_single_plot):
def __init__(self, parent=None):
super(SinglePlotWindow, self).__init__(parent)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setupUi(self)
class Main_Window(QtGui.QWidget, Ui_Form):
def __init__(self, parent=None):
super(Main_Window, self).__init__(parent)
self.setupUi(self)
self.open_single_plotButton.clicked.connect(self.OpenSinglePlot)
def OpenSinglePlot(self):
window = SinglePlotWindow(self)
window.show()
但是当我按下open_single_plotButton
时,SinglePlotWindow
会显示在主窗口的顶部,而不是显示新窗口。
我做错了什么?
在这个answer他们做了一些非常相似的事情,但我没有到达那里......
答案 0 :(得分:1)
您需要创建第二个窗口而不父级,并在显示之后保留对它的引用。所以你的按钮处理程序应如下所示:
def OpenSinglePlot(self):
self.plotWindow = SinglePlotWindow()
self.plotWindow.show()