python3+PyQt5+QtDesigner实现堆叠窗口部件

更新时间:2023-07-07 04:51:25 阅读: 评论:0

python3+PyQt5+QtDesigner实现堆叠窗⼝部件
本⽂是对《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__(lf, parent=None):
super(VehicleRentalDlg, lf).__init__(parent)
vehicleLabel = QLabel("&Vehicle Type:")
lf.vehicleComboBox = QComboBox()
vehicleLabel.tBuddy(lf.vehicleComboBox)
lf.vehicleComboBox.addItems(["Car", "Van"])
colorLabel = QLabel("Co&lor:")
colorLabel.lorComboBox)
"Silver", "White", "Yellow"])
atsLabel = QLabel("&Seats:")
lf.atsSpinBox = QSpinBox()
atsLabel.tBuddy(lf.atsSpinBox)
lf.atsSpinBox.tRange(2, 12)
lf.atsSpinBox.tValue(4)
lf.atsSpinBox.tAlignment(Qt.AlignRight|Qt.AlignVCenter)
weightLabel = QLabel("&Weight:")
lf.weightSpinBox = QSpinBox()
vidsweightLabel.tBuddy(lf.weightSpinBox)
lf.weightSpinBox.tRange(1, 8)
lf.weightSpinBox.tValue(1)
lf.weightSpinBox.tAlignment(Qt.AlignRight|Qt.AlignVCenter)
lf.weightSpinBox.tSuffix(" tons")
volumeLabel = QLabel("Volu&me")
lf.volumeSpinBox = QSpinBox()
volumeLabel.tBuddy(lf.volumeSpinBox)
lf.volumeSpinBox.tRange(4, 22)
lf.volumeSpinBox.tValue(10)
lf.volumeSpinBox.tAlignment(Qt.AlignRight|Qt.AlignVCenter)
lf.volumeSpinBox.tSuffix(" cu m")
mileageLabel = QLabel("Max. Mileage")
lf.mileageLabel = QLabel("1000 miles")
英语老师lf.mileageLabel.tAlignment(Qt.AlignRight|Qt.AlignVCenter)
lf.mileageLabel.tFrameStyle(QFrame.StyledPanel|QFrame.Sunken)
lf.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|
QDialogButtonBox.Cancel)
lf.stackedWidget = QStackedWidget()
carWidget = QWidget()
carLayout = QGridLayout()
carLayout.addWidget(colorLabel, 0, 0)
carLayout.lorComboBox, 0, 1)
carLayout.addWidget(atsLabel, 1, 0)
carLayout.addWidget(lf.atsSpinBox, 1, 1)
carWidget.tLayout(carLayout)
lf.stackedWidget.addWidget(carWidget)
vanWidget = QWidget()
vanLayout = QGridLayout()
vanLayout.addWidget(weightLabel, 0, 0)
vanLayout.addWidget(lf.weightSpinBox, 0, 1)
vanLayout.addWidget(volumeLabel, 1, 0)
vanLayout.addWidget(lf.volumeSpinBox, 1, 1)
vanWidget.tLayout(vanLayout)
lf.stackedWidget.addWidget(vanWidget)
topLayout = QHBoxLayout()
topLayout.addWidget(vehicleLabel)
topLayout.addWidget(lf.vehicleComboBox)
bottomLayout = QHBoxLayout()
bottomLayout.addWidget(mileageLabel)
bottomLayout.addWidget(lf.mileageLabel)
layout = QVBoxLayout()
layout.addLayout(topLayout)
layout.addWidget(lf.stackedWidget)
layout.addLayout(bottomLayout)
layout.addWidget(lf.buttonBox)
lf.tLayout(layout)
lf.t(lf.accept)
ject)
lf.vehicleComboBox.currentIndexChanged[str].connect(lf.tWidgetStack)
lf.weightSpinBox.valueChanged[int].connect(lf.weightChanged)
lf.tWindowTitle("Vehicle Rental")
def tWidgetStack(lf, text):
if text == "Car":
lf.stackedWidget.tCurrentIndex(0)
lf.mileageLabel.tText("1000 miles")
el:
lf.stackedWidget.tCurrentIndex(1)
lf.weightChanged(lf.weightSpinBox.value())
def weightChanged(lf, amount):
lf.mileageLabel.tText("{0} miles".format(8000 / amount))
app = QApplication(sys.argv)
form = VehicleRentalDlg()
form.show()
<_()
第⼆部分:african
/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 tupUi(lf, VehicleRentalDlg):
VehicleRentalDlg.tObjectName("VehicleRentalDlg")
lf.buttonBox = QtWidgets.QDialogButtonBox(VehicleRentalDlg)
lf.buttonBox.tOrientation(QtCore.Qt.Horizontal)
lf.buttonBox.tStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
lf.buttonBox.tObjectName("buttonBox")
spacerItem = QtWidgets.QSpacerItem(188, 16, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) lf.gridlayout.addItem(spacerItem, 3, 0, 1, 1)
lf.hboxlayout = QtWidgets.QHBoxLayout()
lf.hboxlayout.tContentsMargins(0, 0, 0, 0)
lf.hboxlayout.tSpacing(6)
lf.hboxlayout.tObjectName("hboxlayout")
lf.label_6 = QtWidgets.QLabel(VehicleRentalDlg)
lf.label_6.tObjectName("label_6")
lf.hboxlayout.addWidget(lf.label_6)
lf.mileageLabel = QtWidgets.QLabel(VehicleRentalDlg)
lf.mileageLabel.tFrameShape(QtWidgets.QFrame.StyledPanel)
lf.mileageLabel.tFrameShadow(QtWidgets.QFrame.Sunken)
lf.mileageLabel.tAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter) lf.mileageLabel.tObjectName("mileageLabel")
lf.hboxlayout.addWidget(lf.mileageLabel)
lf.stackedWidget = QtWidgets.QStackedWidget(VehicleRentalDlg)
lf.stackedWidget.tObjectName("stackedWidget")
lf.page_2 = QtWidgets.QWidget()
lf.page_2.tObjectName("page_2")
lf.label_4 = QtWidgets.QLabel(lf.page_2)
lf.label_4.tObjectName("label_4")
lf.label_5 = QtWidgets.QLabel(lf.page_2)
lf.label_5.tObjectName("label_5")
lf.atsSpinBox = QtWidgets.QSpinBox(lf.page_2)navigate
lf.atsSpinBox.tAlignment(QtCore.Qt.AlignRight)
lf.atsSpinBox.tMinimum(2)
lf.atsSpinBox.tMaximum(12)
lf.atsSpinBox.tProperty("value", 4)
lf.atsSpinBox.tObjectName("atsSpinBox")
lf.stackedWidget.addWidget(lf.page_2)
lf.page = QtWidgets.QWidget()
lf.page.tObjectName("page")
lf.weightSpinBox = QtWidgets.QSpinBox(lf.page)
lf.weightSpinBox.tAlignment(QtCore.Qt.AlignRight)
lf.weightSpinBox.tMinimum(1)
lf.weightSpinBox.tMaximum(8)
lf.weightSpinBox.tObjectName("weightSpinBox")
lf.label_3 = QtWidgets.QLabel(lf.page)
lf.label_3.tObjectName("label_3")
lf.label_2 = QtWidgets.QLabel(lf.page)
lf.label_2.tObjectName("label_2")
lf.volumeSpinBox = QtWidgets.QSpinBox(lf.page)
lf.volumeSpinBox.tAlignment(QtCore.Qt.AlignRight)
lf.volumeSpinBox.tMinimum(4)
lf.volumeSpinBox.tMaximum(22)
lf.volumeSpinBox.tProperty("value", 10)
lf.volumeSpinBox.tObjectName("volumeSpinBox")
lf.stackedWidget.addWidget(lf.page)
东莞外国语学校lf.hboxlayout1 = QtWidgets.QHBoxLayout()
lf.hboxlayout1.tContentsMargins(0, 0, 0, 0)
lf.hboxlayout1.tSpacing(6)
lf.hboxlayout1.tObjectName("hboxlayout1")
lf.label = QtWidgets.QLabel(VehicleRentalDlg)
lf.label.tObjectName("label")
lf.hboxlayout1.addWidget(lf.label)
lf.vehicleComboBox = QtWidgets.QComboBox(VehicleRentalDlg)
lf.vehicleComboBox.tObjectName("vehicleComboBox")
lf.vehicleComboBox.addItem("")
lf.vehicleComboBox.addItem("")
lf.hboxlayout1.addWidget(lf.vehicleComboBox)
lf.label_4.lorComboBox)
lf.label_5.tBuddy(lf.atsSpinBox)
lf.label_3.tBuddy(lf.volumeSpinBox)
lf.label_2.tBuddy(lf.atsSpinBox)
lf.label.tBuddy(lf.vehicleComboBox)
lf.stackedWidget.tCurrentIndex(0)
lf.vehicleComboBox.currentIndexChanged['int'].connect(lf.stackedWidget.tCurrentIndex) lf.t(VehicleRentalDlg.accept)
ject)
tSlotsByName(VehicleRentalDlg)
def retranslateUi(lf, VehicleRentalDlg):
_translate = anslate
VehicleRentalDlg.tWindowTitle(_translate("VehicleRentalDlg", "Vehicle Rental"))
lf.label_6.tText(_translate("VehicleRentalDlg", "Max. Mileage:"))
lf.mileageLabel.tText(_translate("VehicleRentalDlg", "1000 miles"))
德语培训价格
lf.label_4.tText(_translate("VehicleRentalDlg", "Co&lor:"))
lf.label_5.tText(_translate("VehicleRentalDlg", "&Seats:"))
lf.weightSpinBox.tSuffix(_translate("VehicleRentalDlg", " tons"))
lf.label_3.tText(_translate("VehicleRentalDlg", "Volu&me:"))
lf.label_2.tText(_translate("VehicleRentalDlg", "&Weight:"))
lf.volumeSpinBox.tSuffix(_translate("VehicleRentalDlg", " cu m"))
lf.label.tText(_translate("VehicleRentalDlg", "&Vehicle Type:"))
lf.vehicleComboBox.tItemText(0, _translate("VehicleRentalDlg", "Car"))
lf.vehicleComboBox.tItemText(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__(lf, parent=None):addict
"""
Constructor
@param parent reference to the parent widget
@type QWidget
"""
super(VehicleRentalDlg, lf).__init__(parent)
lf.tupUi(lf)
lf.vehicleComboBox.tFocus()
@pyqtSlot(int)
def on_weightSpinBox_valueChanged(lf, amount):
lf.mileageLabel.tText("{0} miles".format(8000 / amount))
@pyqtSlot(str)
def on_vehicleComboBox_currentIndexChanged(lf, text):
if text == "Car":
lf.mileageLabel.tText("1000 miles")
el:
<_weightSpinBox_valueChanged(
gary barlowlf.weightSpinBox.value())
soltek
if __name__ == "__main__":
app = QApplication(sys.argv)
form = VehicleRentalDlg()
form.show()
<_()网上辅导
运⾏结果:
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

本文发布于:2023-07-07 04:51:25,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/169608.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:实现   希望   有所
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图