欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-03 18:17:22
本文是对《python qt gui快速编程》的第9章的堆叠窗口例子vehicle rental用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实现堆叠窗口部件

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。