当前位置:网站首页>基于Pyqt5工具栏按钮可实现界面切换-2
基于Pyqt5工具栏按钮可实现界面切换-2
2022-07-02 22:13:00 【疯狂的豆包】
Pyqt5是Python中一个可视化超级好用的库,接下来就跟着一起看一下如何实现工具栏按钮切换界面。本章主要介绍代码如何实现,话不多说,开始介绍。
1. 使用pyUIC工具将mainwindow.ui转换为“.py”文件,代码如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(10, 10, 781, 501))
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
self.widget.setPalette(palette)
self.widget.setAutoFillBackground(True)
self.widget.setObjectName("widget")
self.widget_2 = QtWidgets.QWidget(self.widget)
self.widget_2.setGeometry(QtCore.QRect(160, 110, 451, 231))
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
self.widget_2.setPalette(palette)
self.widget_2.setAutoFillBackground(True)
self.widget_2.setObjectName("widget_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.toolBar = QtWidgets.QToolBar(MainWindow)
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.action_1 = QtWidgets.QAction(MainWindow)
self.action_1.setObjectName("action_1")
self.action_2 = QtWidgets.QAction(MainWindow)
self.action_2.setObjectName("action_2")
self.toolBar.addAction(self.action_1)
self.toolBar.addAction(self.action_2)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
self.action_1.setText(_translate("MainWindow", "打开界面1"))
self.action_2.setText(_translate("MainWindow", "打开界面2"))
2. 在main.py中输入以下代码:
import sys
from mainwindow import Ui_MainWindow
from PyQt5 import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QHeaderView, QTableWidgetItem, QHBoxLayout
class MainUI(Ui_MainWindow, QMainWindow):
def __init__(self, parent=None):
super(MainUI, self).__init__(parent)
self.setupUi(self)
def openM(self):
self.widget.show()
self.widget_2.hide()
def openC(self):
self.widget_2.show()
self.widget.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainUI()
main.show()
main.action_1.triggered.connect(main.openM)
main.action_2.triggered.connect(main.openC)
sys.exit(app.exec_())
3.实现的界面如下,打开界面1,界面1出现,界面2隐藏,打开界面2,界面1隐藏,界面2出现。
总结
经过两章详细的介绍,实现了同一窗体下,两个界面的切换,同时按钮是在工具栏中放置的,整洁美观。
边栏推荐
- Alibaba cloud award winning experience: how to use polardb-x
- Value sequence < detailed explanation of daily question >
- Lambda expression: an article takes you through
- Troubleshooting the cause of the crash when STM32 serial port dam receives 253 bytes
- STM32之ADC
- SharedPreferences 保存List<Bean> 到本地并解决com.google.gson.internal.LinkedTreeMap cannot be cast to异常
- Generics and reflection, this is enough
- Learning Websites commonly used by circuit designers
- Call vs2015 with MATLAB to compile vs Project
- Catalogue of digital image processing experiments
猜你喜欢
C#中Linq用法汇集
Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs
Niuke network: maximum submatrix
PotPlayer设置最小化的快捷键
[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
为什么RTOS系统要使用MPU?
Prometheus deployment
Win11自动关机设置在哪?Win11设置自动关机的两种方法
Detailed explanation and application of merging and sorting
深度剖析数据在内存中的存储----C语言篇
随机推荐
Easyclick, EC Quanlang network verification source code
[hardware] origin of standard resistance value
泛型与反射,看这篇就够了
Loss function~
C#中Linq用法汇集
Doorplate making C language
潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
SQL进阶语法
“一个优秀程序员可抵五个普通程序员!”
严守工期,确保质量,这家AI数据标注公司做到了!
Detailed explanation and application of merging and sorting
Realize the linkage between bottomnavigationview and navigation
PotPlayer设置最小化的快捷键
LINQ usage collection in C #
Go multithreaded data search
STM32串口DAM接收253字节就死机原因排查
数字图像处理实验目录
AES高級加密協議的動機闡述
Methods to solve the tampering of Chrome browser and edeg browser homepage
海思 VI接入视频流程