python3+PyQt5+Qt Designer实现堆叠窗口部件

该博客介绍了如何利用Python3、PyQt5和Qt Designer来实现堆叠窗口部件。文章分为两个部分,第一部分纯代码实现,第二部分通过Qt Designer快速生成界面代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文是对《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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值