python3+PyQt5+Qt Designer实现堆叠窗口部件
这篇文章主要为大家详细介绍了python3+PyQt5+Qt Designer实现堆叠窗口部件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文是对《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_()
运行结果:
相关推荐:
python3+PyQt5+Qt Designer实现扩展对话框
以上是python3+PyQt5+Qt Designer实现堆叠窗口部件的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

win7窗口全屏快捷键是什么?我们在使用win7系统的时候,有时候开启软件的时候,它的运行窗口不是全屏的,这个时候我们需要通过鼠标去最大化窗口。那么有什么快捷键可以快速去切换最大化窗口显示呢?接下来和大家分享win7窗口全屏快捷键教程。一、Alt+Enter一般程序,包括大部分游戏在内的没有自带全屏快捷键的程序。二、F11几乎所有的浏览器,都可以用这个快捷键进行全屏。三、Alt+V+U包括word文档在内的大部分文档工具都可以用这个快捷键进行全屏。四、Ctrl+F/Space很多播放器软件,让视

谷歌浏览器怎么设置每次打开都是新的窗口?狠毒用户喜欢使用谷歌浏览器办公或者学习,这款浏览器具有安全、快速、便捷的特点,不同的用户使用浏览器的喜好不同,一些用户喜欢打开谷歌浏览器就是新的标窗口,方便快速搜索,那么如何设置呢。接下来小编就给大家带来谷歌浏览器每次打开都是新的窗口设置教程,感兴趣的朋友快来学习一下吧。谷歌浏览器每次打开都是新的窗口设置教程1、将电脑桌面上的谷歌浏览器双击打开之后,点击右上角的【三个点】的图标位置进行单击。2、找到【设置】这个选项进入到页面中(如图所示)。3、进入到谷歌浏

Win7任务栏显示在桌面右侧窗口怎么办?一般情况下任务栏默认是在屏幕底部的,但近期有Win7用户打开电脑发现任务栏跑到屏幕的右侧窗口去了,那么有没有什么方法可以将其修改回来呢?很多小伙伴不知道怎么详细操作,小编下面整理了Win7任务栏显示在桌面右侧窗口解决步骤,如果你感兴趣的话,跟着小编一起往下看看吧!Win7任务栏显示在桌面右侧窗口解决步骤 1、首先我们右键点击桌面右侧窗口的任务栏,选择属性,如下图所示: 2、打开的任务栏和属性窗口中,在屏幕上的任务栏位置选择底部,并勾选锁定任务栏,点

很多网友在玩win10电脑时,相信都会遇到一些广告弹窗的骚扰,有时在玩游戏时可能会出现游戏广告弹窗导致体验不佳。那么win10电脑右下角闪烁游戏广告怎么办?如何关闭电脑游戏广告弹窗?以下小编就教你如何关闭电脑的游戏广告弹窗。关闭电脑游戏广告弹出窗口的方法。第一步:找到闪烁的弹出窗口进程名称一、闪烁弹窗出现时,先不要关闭,右键打开电脑底部的任务栏设置。2.在任务栏下找到通知区域,点击在任务栏上显示选择哪些图标。3.这里列出的图标曾经出现在你的任务栏上,所以即使你关闭了弹出窗口,这里还是有记录的。你

许多windows10客户感觉自己电脑默认的窗口颜色不好看,要想设成其他颜色,应当怎么设置呢?大家需要先进入设置面板,找到个性化进到,随后点击颜色,之后就会出现一个颜色面板,你可以在其中挑选想要的颜色,以后保存设置,这时候颜色就改好了。你随便开启一个窗口就能看到颜色早已改变了。windows10窗口颜色怎么设置:1、改窗口边框的颜色十分简单,首先进到系统配置。2、随后点击“个性化”,如图所示。3、在弹出的窗口中,挑选左边栏的“颜色”按键。4、然后在右侧的颜色列表中挑选需要的颜色即可,随后点击确定

4月28日消息,MicrosoftDesigner于2022年10月首次发布,是一款由生成式AI功能提供支持的新设计工具,包括OpenAI的DALL.E2。今天微软公司透露,MicrosoftDesigner现在可供任何人在完整的公共预览版中使用。如果你想要快速地做出一些专业品质的设计作品,比如社交媒体帖子、邀请函、数字明信片等,你可能会想到使用Photoshop、Illustrator等专业的设计软件。但是,这些软件的学习成本和使用难度可能会让人望而却步。而Designer是一款基于网页的应用

自Unity桌面引入Ubuntu以来,其简洁、实用的设计理念和美观、高效的用户界面越来越受到人们的欢迎。下面分享一下Unity桌面在切换应用程序窗口中使用快捷键的几个实用技巧。一、用Alt+Tab组合键在当前桌面的不同程序之间切换1、按Alt+Tab组合键,屏幕显示程序切换条;按住Alt键,然后反复点按Tab键,切换条中的程序会按从左到右的顺序显示白色光晕,当光晕到达目标程序时,释放按键,目标程序立即成为当前窗口。2、小技巧:1)先按住右边的Alt键,再按Tab键,这时如果打开的程序较多,目标程

如果您已打开飞行模式(也称为飞行模式),蓝牙、Wi-Fi 和互联网连接将自动关闭。因此,您不能使用任何依赖于互联网的应用程序。但是,尽管飞行模式已打开,您仍然可以手动打开蓝牙和Wi-Fi选项。有时,设备卡在飞行模式下,您无法在该特定设备上执行必要的操作。当这种情况发生时,人们会毫无头绪。但是在本文中,我们有不同的解决方案可以帮助您解决此问题。尝试一下,看看问题是否已解决。让我们开始吧!方法 1 – 使用 Windows 设置关闭飞行模式步骤 1 –同时使用Windows + I键打开窗口设置步骤
