如何在pyqt中的单个窗口中打开多个页面?

时间:2017-07-17 08:23:52

标签: python

我创建了两个窗口

第一个窗口
from PyQt5 import QtCore, QtGui, QtWidgets
from dial import Ui_dial
from chat import Ui_Chat

class Ui_Form(object):

def setupUi(self, Form):
    Form.setObjectName("Form")
    Form.resize(764, 719)
    font = QtGui.QFont()
    font.setKerning(False)
    Form.setFont(font)
    Form.setStyleSheet()
 self.call_button = QtWidgets.QPushButton(Form)
    self.call_button.setGeometry(QtCore.QRect(320, 500, 111, 71))
    font = QtGui.QFont()
    font.setFamily("Times New Roman")
    font.setPointSize(10)
    font.setBold(True)
    font.setWeight(75)
    self.call_button.setFont(font)
    self.call_button.setStyleSheet("")
    self.call_button.setText("")
    icon2 = QtGui.QIcon()
    icon2.addPixmap(QtGui.QPixmap(":/images/Images/call.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    self.call_button.setIcon(icon2)
    self.call_button.setIconSize(QtCore.QSize(58, 58))
    self.call_button.setCheckable(True)
    self.call_button.setAutoExclusive(True)
    self.call_button.setObjectName("call_button")
    self.call_button.setShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Space))
    self.call_button.clicked.connect(self.dialwindow)
 self.ilabel = QtWidgets.QLabel(Form)
    self.ilabel.setGeometry(QtCore.QRect(210, 60, 331, 191))
    font = QtGui.QFont()
    font.setFamily("Sylfaen")
    font.setPointSize(18)
    self.ilabel.setFont(font)
    self.ilabel.setStyleSheet("background-image: url(:/images/Images/label_back1.jpg);")
    self.ilabel.setObjectName("ilabel")
 self.retranslateUi(Form)
    QtCore.QMetaObject.connectSlotsByName(Form)

def retranslateUi(self, Form):
    _translate = QtCore.QCoreApplication.translate
    Form.setWindowTitle(_translate("Form", "Form"))
import icons_rc


if __name__ == "__main__":   
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QDialog()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
第二个窗口
from PyQt5 import QtCore, QtGui, QtWidgets
import dial_rc

class Ui_dial(object):

def setupUi(self, dial):
    dial.setObjectName("dial")
    dial.resize(739, 712)
    dial.setStyleSheet()
self.dialpage_label = QtWidgets.QLabel(dial)
    self.dialpage_label.setGeometry(QtCore.QRect(240, 120, 301, 511))
    self.dialpage_label.setObjectName("dialpage_label")
    self.callend_button = QtWidgets.QPushButton(dial)
    self.callend_button.setGeometry(QtCore.QRect(340, 520, 91, 71))
    self.callend_button.setText("")
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap(":/images/Images/end.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    self.callend_button.setIcon(icon)
    self.callend_button.setIconSize(QtCore.QSize(45, 45))
    self.callend_button.setObjectName("callend_button")
    self.callend_button.clicked.connect(self.endcall)
def retranslateUi(self, dial):
    _translate = QtCore.QCoreApplication.translate
    dial.setWindowTitle(_translate("dial", "Dialog"))
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    dial = QtWidgets.QDialog()
    ui = Ui_dial()
    ui.setupUi(dial)
    dial.show()
    sys.exit(app.exec_())

如何在单个窗口中显示这个不同的窗口?当点击按钮时,新窗口打开,所以当我点击第二个窗口在同一个屏幕上打开的按钮时我想显示。所以我需要关于这个问题的专业知识

1 个答案:

答案 0 :(得分:0)

首先修改python脚本,以便该类继承自QtWidgets.QMainWindow 由

class Ui_Form(QtWidgets.Qdialog):

然后编写 init 函数,以便可以实例化您的类,即可以创建对象。完成后,您可以丢弃QtWidgets.QDialog()对象,该对象通过脚本中的名称拨号传递给您的setupUI()。然后用self替换这个传递的对象。

总体而言,您的代码将如下所示。

class Ui_dial(QtWidgets.Qdialog):

def __init__(self):
    super().__init__()//inheriting from the object.
def setupUi(self, dial):
    self.setObjectName("dial")
    self.resize(739, 712)
    self.setStyleSheet()
    self.dialpage_label = QtWidgets.QLabel(dial)
    self.dialpage_label.setGeometry(QtCore.QRect(240, 120, 301, 511))
    self.dialpage_label.setObjectName("dialpage_label")
    self.callend_button = QtWidgets.QPushButton(dial)
    self.callend_button.setGeometry(QtCore.QRect(340, 520, 91, 71))
    self.callend_button.setText("")
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap(":/images/Images/end.png"),
def retranslateUi(self):
    _translate = QtCore.QCoreApplication.translate
    self.setWindowTitle(_translate("dial", "Dialog"))

对其他脚本执行相同操作,然后创建这些类的对象并将其称为

First_window = Ui_Form()
First_window.show()
Second_window = Ui_dial()
Second_window.show()

您可能需要使用resize()调整两个窗口并根据需要调整它们。