python3+PyQt5+Qt Designer实现堆叠窗口部件
作者:basisworker 发布时间:2023-12-30 01:21:55
标签:python3,PyQt5,窗口
本文是对《Python Qt GUI快速编程》的第9章的堆叠窗口例子Vehicle Rental用Python3+PyQt5+Qt Designer进行改写。
第一部分无借用Qt Designer,完全用代码实现。
第二部分则借用Qt Designer,快速实现。
第一部分:
import sys
from PyQt5.QtCore import (Qt)
from PyQt5.QtWidgets import (QApplication, QComboBox, QDialog,
QDialogButtonBox, QFrame, QGridLayout, QHBoxLayout, QLabel,
QSpinBox, QStackedWidget, QVBoxLayout, QWidget)
class VehicleRentalDlg(QDialog):
def __init__(self, parent=None):
super(VehicleRentalDlg, self).__init__(parent)
vehicleLabel = QLabel("&Vehicle Type:")
self.vehicleComboBox = QComboBox()
vehicleLabel.setBuddy(self.vehicleComboBox)
self.vehicleComboBox.addItems(["Car", "Van"])
colorLabel = QLabel("Co&lor:")
self.colorComboBox = QComboBox()
colorLabel.setBuddy(self.colorComboBox)
self.colorComboBox.addItems(["Black", "Blue", "Green", "Red",
"Silver", "White", "Yellow"])
seatsLabel = QLabel("&Seats:")
self.seatsSpinBox = QSpinBox()
seatsLabel.setBuddy(self.seatsSpinBox)
self.seatsSpinBox.setRange(2, 12)
self.seatsSpinBox.setValue(4)
self.seatsSpinBox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
weightLabel = QLabel("&Weight:")
self.weightSpinBox = QSpinBox()
weightLabel.setBuddy(self.weightSpinBox)
self.weightSpinBox.setRange(1, 8)
self.weightSpinBox.setValue(1)
self.weightSpinBox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
self.weightSpinBox.setSuffix(" tons")
volumeLabel = QLabel("Volu&me")
self.volumeSpinBox = QSpinBox()
volumeLabel.setBuddy(self.volumeSpinBox)
self.volumeSpinBox.setRange(4, 22)
self.volumeSpinBox.setValue(10)
self.volumeSpinBox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
self.volumeSpinBox.setSuffix(" cu m")
mileageLabel = QLabel("Max. Mileage")
self.mileageLabel = QLabel("1000 miles")
self.mileageLabel.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
self.mileageLabel.setFrameStyle(QFrame.StyledPanel|QFrame.Sunken)
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|
QDialogButtonBox.Cancel)
self.stackedWidget = QStackedWidget()
carWidget = QWidget()
carLayout = QGridLayout()
carLayout.addWidget(colorLabel, 0, 0)
carLayout.addWidget(self.colorComboBox, 0, 1)
carLayout.addWidget(seatsLabel, 1, 0)
carLayout.addWidget(self.seatsSpinBox, 1, 1)
carWidget.setLayout(carLayout)
self.stackedWidget.addWidget(carWidget)
vanWidget = QWidget()
vanLayout = QGridLayout()
vanLayout.addWidget(weightLabel, 0, 0)
vanLayout.addWidget(self.weightSpinBox, 0, 1)
vanLayout.addWidget(volumeLabel, 1, 0)
vanLayout.addWidget(self.volumeSpinBox, 1, 1)
vanWidget.setLayout(vanLayout)
self.stackedWidget.addWidget(vanWidget)
topLayout = QHBoxLayout()
topLayout.addWidget(vehicleLabel)
topLayout.addWidget(self.vehicleComboBox)
bottomLayout = QHBoxLayout()
bottomLayout.addWidget(mileageLabel)
bottomLayout.addWidget(self.mileageLabel)
layout = QVBoxLayout()
layout.addLayout(topLayout)
layout.addWidget(self.stackedWidget)
layout.addLayout(bottomLayout)
layout.addWidget(self.buttonBox)
self.setLayout(layout)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
self.vehicleComboBox.currentIndexChanged[str].connect(self.setWidgetStack)
self.weightSpinBox.valueChanged[int].connect(self.weightChanged)
self.setWindowTitle("Vehicle Rental")
def setWidgetStack(self, text):
if text == "Car":
self.stackedWidget.setCurrentIndex(0)
self.mileageLabel.setText("1000 miles")
else:
self.stackedWidget.setCurrentIndex(1)
self.weightChanged(self.weightSpinBox.value())
def weightChanged(self, amount):
self.mileageLabel.setText("{0} miles".format(8000 / amount))
app = QApplication(sys.argv)
form = VehicleRentalDlg()
form.show()
app.exec_()
第二部分:
/home/yrd/eric_workspace/Vehicle/Ui_vehiclerentaldlg.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '/home/yrd/eric_workspace/Vehicle/vehiclerentaldlg.ui'
#
# Created by: PyQt5 UI code generator 5.7
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_VehicleRentalDlg(object):
def setupUi(self, VehicleRentalDlg):
VehicleRentalDlg.setObjectName("VehicleRentalDlg")
VehicleRentalDlg.resize(206, 246)
self.gridlayout = QtWidgets.QGridLayout(VehicleRentalDlg)
self.gridlayout.setContentsMargins(9, 9, 9, 9)
self.gridlayout.setSpacing(6)
self.gridlayout.setObjectName("gridlayout")
self.buttonBox = QtWidgets.QDialogButtonBox(VehicleRentalDlg)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.gridlayout.addWidget(self.buttonBox, 4, 0, 1, 1)
spacerItem = QtWidgets.QSpacerItem(188, 16, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridlayout.addItem(spacerItem, 3, 0, 1, 1)
self.hboxlayout = QtWidgets.QHBoxLayout()
self.hboxlayout.setContentsMargins(0, 0, 0, 0)
self.hboxlayout.setSpacing(6)
self.hboxlayout.setObjectName("hboxlayout")
self.label_6 = QtWidgets.QLabel(VehicleRentalDlg)
self.label_6.setObjectName("label_6")
self.hboxlayout.addWidget(self.label_6)
self.mileageLabel = QtWidgets.QLabel(VehicleRentalDlg)
self.mileageLabel.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.mileageLabel.setFrameShadow(QtWidgets.QFrame.Sunken)
self.mileageLabel.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.mileageLabel.setObjectName("mileageLabel")
self.hboxlayout.addWidget(self.mileageLabel)
self.gridlayout.addLayout(self.hboxlayout, 2, 0, 1, 1)
self.stackedWidget = QtWidgets.QStackedWidget(VehicleRentalDlg)
self.stackedWidget.setObjectName("stackedWidget")
self.page_2 = QtWidgets.QWidget()
self.page_2.setObjectName("page_2")
self.gridlayout1 = QtWidgets.QGridLayout(self.page_2)
self.gridlayout1.setContentsMargins(9, 9, 9, 9)
self.gridlayout1.setSpacing(6)
self.gridlayout1.setObjectName("gridlayout1")
self.colorComboBox = QtWidgets.QComboBox(self.page_2)
self.colorComboBox.setObjectName("colorComboBox")
self.colorComboBox.addItem("")
self.colorComboBox.addItem("")
self.colorComboBox.addItem("")
self.colorComboBox.addItem("")
self.colorComboBox.addItem("")
self.colorComboBox.addItem("")
self.colorComboBox.addItem("")
self.gridlayout1.addWidget(self.colorComboBox, 0, 1, 1, 1)
self.label_4 = QtWidgets.QLabel(self.page_2)
self.label_4.setObjectName("label_4")
self.gridlayout1.addWidget(self.label_4, 0, 0, 1, 1)
self.label_5 = QtWidgets.QLabel(self.page_2)
self.label_5.setObjectName("label_5")
self.gridlayout1.addWidget(self.label_5, 1, 0, 1, 1)
self.seatsSpinBox = QtWidgets.QSpinBox(self.page_2)
self.seatsSpinBox.setAlignment(QtCore.Qt.AlignRight)
self.seatsSpinBox.setMinimum(2)
self.seatsSpinBox.setMaximum(12)
self.seatsSpinBox.setProperty("value", 4)
self.seatsSpinBox.setObjectName("seatsSpinBox")
self.gridlayout1.addWidget(self.seatsSpinBox, 1, 1, 1, 1)
self.stackedWidget.addWidget(self.page_2)
self.page = QtWidgets.QWidget()
self.page.setObjectName("page")
self.gridlayout2 = QtWidgets.QGridLayout(self.page)
self.gridlayout2.setContentsMargins(9, 9, 9, 9)
self.gridlayout2.setSpacing(6)
self.gridlayout2.setObjectName("gridlayout2")
self.weightSpinBox = QtWidgets.QSpinBox(self.page)
self.weightSpinBox.setAlignment(QtCore.Qt.AlignRight)
self.weightSpinBox.setMinimum(1)
self.weightSpinBox.setMaximum(8)
self.weightSpinBox.setObjectName("weightSpinBox")
self.gridlayout2.addWidget(self.weightSpinBox, 0, 1, 1, 1)
self.label_3 = QtWidgets.QLabel(self.page)
self.label_3.setObjectName("label_3")
self.gridlayout2.addWidget(self.label_3, 1, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.page)
self.label_2.setObjectName("label_2")
self.gridlayout2.addWidget(self.label_2, 0, 0, 1, 1)
self.volumeSpinBox = QtWidgets.QSpinBox(self.page)
self.volumeSpinBox.setAlignment(QtCore.Qt.AlignRight)
self.volumeSpinBox.setMinimum(4)
self.volumeSpinBox.setMaximum(22)
self.volumeSpinBox.setProperty("value", 10)
self.volumeSpinBox.setObjectName("volumeSpinBox")
self.gridlayout2.addWidget(self.volumeSpinBox, 1, 1, 1, 1)
self.stackedWidget.addWidget(self.page)
self.gridlayout.addWidget(self.stackedWidget, 1, 0, 1, 1)
self.hboxlayout1 = QtWidgets.QHBoxLayout()
self.hboxlayout1.setContentsMargins(0, 0, 0, 0)
self.hboxlayout1.setSpacing(6)
self.hboxlayout1.setObjectName("hboxlayout1")
self.label = QtWidgets.QLabel(VehicleRentalDlg)
self.label.setObjectName("label")
self.hboxlayout1.addWidget(self.label)
self.vehicleComboBox = QtWidgets.QComboBox(VehicleRentalDlg)
self.vehicleComboBox.setObjectName("vehicleComboBox")
self.vehicleComboBox.addItem("")
self.vehicleComboBox.addItem("")
self.hboxlayout1.addWidget(self.vehicleComboBox)
self.gridlayout.addLayout(self.hboxlayout1, 0, 0, 1, 1)
self.label_4.setBuddy(self.colorComboBox)
self.label_5.setBuddy(self.seatsSpinBox)
self.label_3.setBuddy(self.volumeSpinBox)
self.label_2.setBuddy(self.seatsSpinBox)
self.label.setBuddy(self.vehicleComboBox)
self.retranslateUi(VehicleRentalDlg)
self.stackedWidget.setCurrentIndex(0)
self.vehicleComboBox.currentIndexChanged['int'].connect(self.stackedWidget.setCurrentIndex)
self.buttonBox.accepted.connect(VehicleRentalDlg.accept)
self.buttonBox.rejected.connect(VehicleRentalDlg.reject)
QtCore.QMetaObject.connectSlotsByName(VehicleRentalDlg)
def retranslateUi(self, VehicleRentalDlg):
_translate = QtCore.QCoreApplication.translate
VehicleRentalDlg.setWindowTitle(_translate("VehicleRentalDlg", "Vehicle Rental"))
self.label_6.setText(_translate("VehicleRentalDlg", "Max. Mileage:"))
self.mileageLabel.setText(_translate("VehicleRentalDlg", "1000 miles"))
self.colorComboBox.setItemText(0, _translate("VehicleRentalDlg", "Black"))
self.colorComboBox.setItemText(1, _translate("VehicleRentalDlg", "Blue"))
self.colorComboBox.setItemText(2, _translate("VehicleRentalDlg", "Green"))
self.colorComboBox.setItemText(3, _translate("VehicleRentalDlg", "Red"))
self.colorComboBox.setItemText(4, _translate("VehicleRentalDlg", "Silver"))
self.colorComboBox.setItemText(5, _translate("VehicleRentalDlg", "White"))
self.colorComboBox.setItemText(6, _translate("VehicleRentalDlg", "Yellow"))
self.label_4.setText(_translate("VehicleRentalDlg", "Co&lor:"))
self.label_5.setText(_translate("VehicleRentalDlg", "&Seats:"))
self.weightSpinBox.setSuffix(_translate("VehicleRentalDlg", " tons"))
self.label_3.setText(_translate("VehicleRentalDlg", "Volu&me:"))
self.label_2.setText(_translate("VehicleRentalDlg", "&Weight:"))
self.volumeSpinBox.setSuffix(_translate("VehicleRentalDlg", " cu m"))
self.label.setText(_translate("VehicleRentalDlg", "&Vehicle Type:"))
self.vehicleComboBox.setItemText(0, _translate("VehicleRentalDlg", "Car"))
self.vehicleComboBox.setItemText(1, _translate("VehicleRentalDlg", "Van"))
/home/yrd/eric_workspace/Vehicle/vehiclerentaldlg.py
# -*- coding: utf-8 -*-
"""
Module implementing VehicleRentalDlg.
"""
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QDialog,QApplication
from Ui_vehiclerentaldlg import Ui_VehicleRentalDlg
class VehicleRentalDlg(QDialog, Ui_VehicleRentalDlg):
"""
Class documentation goes here.
"""
def __init__(self, parent=None):
"""
Constructor
@param parent reference to the parent widget
@type QWidget
"""
super(VehicleRentalDlg, self).__init__(parent)
self.setupUi(self)
self.vehicleComboBox.setFocus()
@pyqtSlot(int)
def on_weightSpinBox_valueChanged(self, amount):
self.mileageLabel.setText("{0} miles".format(8000 / amount))
@pyqtSlot(str)
def on_vehicleComboBox_currentIndexChanged(self, text):
if text == "Car":
self.mileageLabel.setText("1000 miles")
else:
self.on_weightSpinBox_valueChanged(
self.weightSpinBox.value())
if __name__ == "__main__":
app = QApplication(sys.argv)
form = VehicleRentalDlg()
form.show()
app.exec_()
运行结果:
来源:https://blog.csdn.net/xiaoyangyang20/article/details/54580650


猜你喜欢
- 在产品开发中,由UED发起的项目越来越多,但是现在的问题是很难为其设定商业价值的目标。如果没有明确的商业价值目标,很多公司根本没办法花大成本
- opencv图像处理(深度学习中常用的)改变色彩空间: cv.cvtColor()cv.cvtColor(img, flag)img:原图像
- 实例如下所示:>>>from compiler.ast import flatten>>>Xmatrix
- 前言Go 数组的长度不可改变,在特定场景中这样的集合就不太适用,Go中提供了一种灵活,功能强悍的内置类型切片("动态数组"
- 尽管 xml.etree.ElementTree 库通常用来做解析工作,其实它也可以创建XML文档。 例如,考虑如下这个函数:from xm
- asin()方法返回x的反正弦,以弧度表示。语法以下是asin()方法语法:asin(x)注意:此函数是无法直接访问的,所以我们
- 使用工具:pexpect库pexpect可以理解为Linux下expect(不知道的可以百度下linux expect)的python封装。
- 一、发送电子邮件Python标准库提供了smtplib,用于实现SMTP协议发送邮件。标准库还提供email模块帮助我们构建邮件格式。SMT
- 最近一句话后门不断升级大家注意防范,基本上多事字符替换过护卫神PHP一句话作者:小东 <?php $a = str_replace(x
- 正则表达式是处理字符串的强大工具。作为一个概念而言,正则表达式对于Python来说并不是独有的。但是,Python中的正则表达式在实际使用过
- 额额,标题已经很醒目了,通过中间件去实现,其他方法也可以实现浏览器前端传来的请求,必须通过中间件,才能到后面路由,视图函数,所以我们在中间件
- Oracle的out参数实例详解一 概念1、一般来讲,存储过程和存储函数的区别在于存储函数可以有一个返回值;而存储过程没有返回值。2、过程和
- 对于内容驱动的网站,设计好坏的关键是关系型数据库。在这个教程中,我们已经使用了MySQL关系型数据库管理系统(RDBMS)建立了我们的数据库
- 所谓定时器,是指间隔特定时间执行特定任务的机制。几乎所有的编程语言,都有定时器的实现。比如,Java有util.Timer和util.Tim
- 个人使用环境WIN10x64系统,Python3.8,PyCharm2020.01.03安装过程一、安装Python3.8(自己参考其他教程
- python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误。你可以使用该功能来调试python程序。异常处理: 本站
- 现有问题当前的项目中包括一个6200万行、500多列的表。其中的数据来自SQL Server以外,它们到达的表中有一个标识主键,所有剩下的列
- 方法一、使用在父模板中使用{include file="child.tpl"}直接将子模板包含进来优点:1、有利于模块的
- 下面的查询选择所有 date_col 值在最后 30 天内的记录。 mysql> SELECT something FROM tbl_
- 合理使用装饰器可以简化开发,并且使得代码更加清晰。下面我们分别介绍两种装饰器,不带参数的装饰器和带参数的装饰器。一、不带参数的装饰器我们用一