简体   繁体   English

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

[英]how to open multiple pages in single window in pyqt?

I created two windows 我创建了两个窗口

First window 第一个窗口
 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_()) 
Second Window 第二视窗
 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_()) 

How to show this different windows in single window?when click on button new window is open ,so i want show when i click on the button that second window open in same screen.so i need expertise on this problem 当单击按钮打开新窗口时,如何在单个窗口中显示此不同窗口?因此,当我单击在同一屏幕上打开第二个窗口的按钮时,我想显示。所以我需要有关此问题的专业知识

First modify your python script so that the class inherits from QtWidgets.QMainWindow by 首先修改您的python脚本,以使该类继承自QtWidgets.QMainWindow

class Ui_Form(QtWidgets.Qdialog):

Then write an init function so that your class can be instantiated, ie an object can be created. 然后编写一个初始化函数,以便可以实例化您的类,即可以创建一个对象。 Once that is done, you can chuck out the QtWidgets.QDialog() object, the one that goes by the name dial in your script, being passed to your setupUI(). 完成此操作后,您可以删除QtWidgets.QDialog()对象,该对象通过脚本中名称拨号的方式传递给setupUI()。 Then replace this object being passed with self. 然后用self替换该对象。

Overall your code will look like this. 总体而言,您的代码将如下所示。

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"))

Do the same for your other script and then make objects of these classes and call them as, 对其他脚本执行相同的操作,然后创建这些类的对象,并将其称为,

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

You might need to use resize() to adjust the two windows and to accommodate them according to your needs. 您可能需要使用resize()来调整两个窗口并根据需要容纳它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM