PyQt常用控件的组合使用-爱代码爱编程
写作思路
这次使用到的空间有以下几种:
1、QPushButton 是我们之前用的按钮,也可以当作Toggle用
2、QCheckBox
3、QSlider
4、QCalendarWidget
5、QProgressBar
效果如下,可以改变QFrame的颜色,只有选中了对应Toggle才能改变对应的颜色通道,CheckBox可以控制Slider是否显示,然后加入了日历QCalendarWidget,QProgressBar进度条满了就自动跳下一个月
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QWidget, QSlider,
QLabel, QApplication, QPushButton, QVBoxLayout, QCheckBox, QHBoxLayout, QFrame,
QCalendarWidget, QProgressBar)
from PyQt5.QtCore import Qt, pyqtSignal, QBasicTimer
from PyQt5.QtGui import QColor
class FrequentlyTogether(QWidget):
colorChangeSignal = pyqtSignal(int, float)
colorIsSelectedSignal = pyqtSignal(int, bool)
def __init__(self):
super().__init__()
self.redValue = 0
self.greenValue = 0
self.blueValue = 0
self.redShow = False
self.greenShow = False
self.blueShow = False
self.col = QColor(0, 0, 0)
self.step = 0
self.timer = QBasicTimer()
self.colorChangeSignal.connect(self.OnColorChange)
self.colorIsSelectedSignal.connect(self.OnColorSelectedChange)
self.initUI()
def initUI(self):
vlayoutOutSide = QVBoxLayout()
hlayout = QHBoxLayout()
vlayout = QVBoxLayout()
vlayout.addLayout(self.singelModel("红色", 0))
vlayout.addLayout(self.singelModel("绿色", 1))
vlayout.addLayout(self.singelModel("蓝色", 2))
vlayout.setSpacing(20)
self.square = QFrame(self)
self.square.setGeometry(150, 20, 100, 100)
self.square.setStyleSheet("QWidget { background-color: %s }" % self.col.name())
hlayout.addLayout(vlayout)
hlayout.addWidget(self.square)
vlayoutOutSide.addLayout(hlayout) #第一层
self.showCalendar()
vlayoutOutSide.addWidget(self.calendar)
# 进度条
self.pbar = QProgressBar(self)
self.timer.start(20, self)
vlayoutOutSide.addWidget(self.pbar)
vlayoutOutSide.addWidget(self.lbl)
self.setLayout(vlayoutOutSide)
self.setGeometry(300, 300, 500, 170)
self.setWindowTitle('颜色混合器')
self.show()
def singelModel(self, name, index):
def isShowSlider(press):
if press:
sld.setHidden(False)
else:
sld.setHidden(True)
colorSignal = self.colorChangeSignal
enableSignal = self.colorIsSelectedSignal
def changeColor(value):
colorSignal.emit(index, value)
def colorChangeEnable(checked):
enableSignal.emit(index, checked)
vlayout = QVBoxLayout()
hlayout = QHBoxLayout()
btn = QPushButton(name)
btn.setCheckable(True)
btn.setMaximumSize(50, 50)
btn.clicked.connect(colorChangeEnable)
chechbox = QCheckBox('是否显示细节条', self)
chechbox.toggle()
chechbox.setCheckState(Qt.CheckState.Unchecked)
chechbox.stateChanged.connect(isShowSlider)
sld = QSlider(Qt.Horizontal, self)
sld.setFocusPolicy(Qt.NoFocus)
sld.setMaximumSize(250, 10)
sld.setHidden(True)
sld.valueChanged.connect(changeColor)
hlayout.setSpacing(50)
hlayout.addWidget(btn)
hlayout.addWidget(chechbox)
vlayout.setSpacing(0)
vlayout.addLayout(hlayout)
vlayout.addWidget(sld)
return vlayout
def OnColorChange(self, index, value):
color = int(value/100*255)
if index == 0:
if self.redShow is True:
self.col.setRed(color)
else:
self.col.setRed(0)
elif index == 1:
if self.greenShow is True:
self.col.setGreen(color)
else:
self.col.setGreen(0)
else:
if self.blueShow is True:
self.col.setBlue(color)
else:
self.col.setGreen(0)
self.square.setStyleSheet("QFrame { background-color: %s }" % self.col.name())
def OnColorSelectedChange(self, index, canShow):
if index == 0:
self.redShow = canShow
if canShow is False:
self.col.setRed(0)
elif index == 1:
self.greenShow = canShow
if canShow is False:
self.col.setGreen(0)
else:
self.blueShow = canShow
if canShow is False:
self.col.setBlue(0)
self.square.setStyleSheet("QFrame { background-color: %s }" % self.col.name())
def showCalendar(self):
# 日历
self.calendar = QCalendarWidget(self)
self.calendar.setGridVisible(True)
self.calendar.clicked.connect(self.showDate)
# calendar.setSelectedDate(datetime.date)
self.lbl = QLabel(self)
date = self.calendar.selectedDate()
self.lbl.setText("当前时间: %s" % date.toString())
def timerEvent(self, e):
if self.step >= 100:
self.step = 0
self.calendar.showNextMonth()
return
self.step = self.step + 1
self.pbar.setValue(self.step)
def showDate(self, date):
self.lbl.setText("当前时间: %s" % date.toString())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FrequentlyTogether()
sys.exit(app.exec_())
代码的难度不高,这里就不做详细解释了,可能要注意以下的就是下图这部分的代码
也就是singelModel这个函数,为了多次调用不写重复代码,并且能让事件发出来,用了pyqtSignal作为事件传播
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/weixin_40301728/article/details/111063292