• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞代码 4年前 (2022-01-09) 35次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了python3+PyQt5+Qt Designer实现堆叠窗口部件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文是对《Python Qt GUI快速编程》的第9章的堆叠窗口例子Vehicle Rental用Python3+PyQt5+Qt Designer进行改写。
第一部分无借用Qt Designer,完全用代码实现。
第二部分则借用Qt Designer,快速实现。

第一部分:

import sysfrom 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.<a style="color:transparent">本文来源gao($daima.com搞@代@#码(网5</a>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_()

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python3+PyQt5+Qt Designer实现堆叠窗口部件
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址