国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python3+PyQt5+Qt Designer實現(xiàn)堆疊窗口部件

2020-02-22 23:45:34
字體:
供稿:網(wǎng)友

本文是對《Python Qt GUI快速編程》的第9章的堆疊窗口例子Vehicle Rental用Python3+PyQt5+Qt Designer進(jìn)行改寫。
第一部分無借用Qt Designer,完全用代碼實現(xiàn)。
第二部分則借用Qt Designer,快速實現(xiàn)。

第一部分:

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.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_()            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 岑巩县| 利辛县| 安泽县| 婺源县| 关岭| 大田县| 梅河口市| 浪卡子县| 静宁县| 万荣县| 岳西县| 文成县| 万全县| 聂荣县| 德格县| 富顺县| 通城县| 普定县| 长垣县| 遂川县| 冷水江市| 南部县| 玉林市| 瑞丽市| 班戈县| 百色市| 尉犁县| 凌云县| 方山县| 桂东县| 南康市| 夏邑县| 车致| 荥阳市| 海兴县| 沁水县| 邯郸市| 轮台县| 湘潭县| 永吉县| 惠水县|