PyQT学习之Stacked Widget控件

1. 打开PyCharm中:Tools->Externel Tools->Qt_Designer

PyQT学习之Stacked Widget控件_第1张图片

 

注:

Qt_Designer和PyUIC的设置:

File->Settings->Tools->External Tools

Qt_Designer:

      Name: Qt_Designer

      Program: Python安装地址\Lib\site-packages\QtDesigner\designer.exe

      Working Dirctory: $ProjectFileDir$

PyUIC:

      Name: PyUIC

      Program: Python安装地址\python.exe

     Arguments:-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py

      Working Dirctory: $FileDir$


2. QtDesigner中:文件->新建->Widget

在左侧栏中选择拖动“Push Button”和“Stacked Widget”控件到右侧

PyQT学习之Stacked Widget控件_第2张图片

为了验证两个页面的切换过程,在右侧对象查看器中选中page,然后拖动Label到UI中

PyQT学习之Stacked Widget控件_第3张图片

然后将此UI保存名为“test.ui”


3. 将test.ui生成test.py文件

选中test.ui,选择Tools->Externel Tools->PyUIC,生成test.py代码如下:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1053, 713)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(80, 90, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_1 = QtWidgets.QPushButton(Form)
        self.pushButton_1.setGeometry(QtCore.QRect(80, 180, 93, 28))
        self.pushButton_1.setObjectName("pushButton_1")
        self.stackedWidget = QtWidgets.QStackedWidget(Form)
        self.stackedWidget.setGeometry(QtCore.QRect(220, 80, 621, 461))
        self.stackedWidget.setObjectName("stackedWidget")
        self.page = QtWidgets.QWidget()
        self.page.setObjectName("page")
        self.label = QtWidgets.QLabel(self.page)
        self.label.setGeometry(QtCore.QRect(120, 80, 421, 181))
        font = QtGui.QFont()
        font.setFamily("Adobe Arabic")
        font.setPointSize(72)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.stackedWidget.addWidget(self.page)
        self.page_2 = QtWidgets.QWidget()
        self.page_2.setObjectName("page_2")
        self.label_2 = QtWidgets.QLabel(self.page_2)
        self.label_2.setGeometry(QtCore.QRect(110, 140, 361, 171))
        font = QtGui.QFont()
        font.setFamily("Adobe Arabic")
        font.setPointSize(72)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.stackedWidget.addWidget(self.page_2)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "页面1"))
        self.pushButton_1.setText(_translate("Form", "页面2"))
        self.label.setText(_translate("Form", "页面1"))
        self.label_2.setText(_translate("Form", "页面2"))

4. 新建main.py文件

# encoding=utf-8
import sys
from PyQt5.QtWidgets import *

import test


class UI_Test(QWidget, test.Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.pushButton_func)
        self.pushButton_1.clicked.connect(self.pushButton_1_func)

    def pushButton_func(self):
        self.stackedWidget.setCurrentIndex(0)

    def pushButton_1_func(self):
        self.stackedWidget.setCurrentIndex(1)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui_win = UI_Test()
    ui_win.show()
    sys.exit(app.exec_())

运行代码:

PyQT学习之Stacked Widget控件_第4张图片

 

 

你可能感兴趣的:(pyqt,python)