import sysfrom PyQt4.QtCore import *
from PyQt4.QtGui import *class SwitchBtn(QWidget):#信号checkedChanged = pyqtSignal(bool)def __init__(self,parent=None):super(QWidget, self).__init__(parent)self.checked = Falseself.bgColorOff = QColor(255, 255, 255)self.bgColorOn = QColor(0, 0, 0)self.sliderColorOff = QColor(100, 100, 100)self.sliderColorOn = QColor(100, 184, 255)self.textColorOff = QColor(143, 143, 143)self.textColorOn = QColor(255, 255, 255)self.textOff = "OFF"self.textOn = "ON"self.space = 2self.rectRadius = 5self.step = self.width() / 50self.startX = 0self.endX = 0self.timer = QTimer(self) # 初始化一个定时器self.timer.timeout.connect(self.updateValue) # 计时结束调用operate()方法#self.timer.start(5) # 设置计时间隔并启动self.setFont(QFont("Microsoft Yahei", 10))#self.resize(55,22)def updateValue(self):if self.checked:if self.startX < self.endX:self.startX = self.startX + self.stepelse:self.startX = self.endXself.timer.stop()else:if self.startX > self.endX:self.startX = self.startX - self.stepelse:self.startX = self.endXself.timer.stop()self.update()def mousePressEvent(self,event):self.checked = not self.checked#发射信号self.checkedChanged.emit(self.checked)# 每次移动的步长为宽度的50分之一self.step = self.width() / 50#状态切换改变后自动计算终点坐标if self.checked:self.endX = self.width() - self.height()else:self.endX = 0self.timer.start(5)def paintEvent(self, evt):#绘制准备工作, 启用反锯齿painter = QPainter()painter.begin(self)painter.setRenderHint(QPainter.Antialiasing)#绘制背景self.drawBg(evt, painter)#绘制滑块self.drawSlider(evt, painter)#绘制文字self.drawText(evt, painter)painter.end()def drawText(self, event, painter):painter.save()if self.checked:painter.setPen(self.textColorOn)painter.drawText(0, 0, self.width() / 2 + self.space * 2, self.height(), Qt.AlignCenter, self.textOn)else:painter.setPen(self.textColorOff)painter.drawText(self.width() / 2, 0,self.width() / 2 - self.space, self.height(), Qt.AlignCenter, self.textOff)painter.restore()def drawBg(self, event, painter):painter.save()painter.setPen(Qt.NoPen)if self.checked:painter.setBrush(self.bgColorOn)else:painter.setBrush(self.bgColorOff)rect = QRect(0, 0, self.width(), self.height())#半径为高度的一半radius = rect.height() / 2#圆的宽度为高度circleWidth = rect.height()path = QPainterPath()path.moveTo(radius, rect.left())path.arcTo(QRectF(rect.left(), rect.top(), circleWidth, circleWidth), 90, 180)path.lineTo(rect.width() - radius, rect.height())path.arcTo(QRectF(rect.width() - rect.height(), rect.top(), circleWidth, circleWidth), 270, 180)path.lineTo(radius, rect.top())painter.drawPath(path)painter.restore()def drawSlider(self, event, painter):painter.save()if self.checked:painter.setBrush(self.sliderColorOn)else:painter.setBrush(self.sliderColorOff)rect = QRect(0, 0, self.width(), self.height())sliderWidth = rect.height() - self.space * 2sliderRect = QRect(self.startX + self.space, self.space, sliderWidth, sliderWidth)painter.drawEllipse(sliderRect)painter.restore()class MainWindow(QMainWindow):def __init__(self,parent=None):super(MainWindow, self).__init__(parent)self.resize(400,200)self.switchBtn = SwitchBtn(self)self.switchBtn.setGeometry(10,10,60,30)self.switchBtn.checkedChanged.connect(self.getState)self.status = self.statusBar()self.status.showMessage("this is a example", 5000)self.setWindowTitle("PyQt")def getState(self,checked):print("checked=", checked)if __name__ == "__main__":app = QApplication(sys.argv)form = MainWindow()#form = SwitchBtn()form.show()sys.exit(app.exec_())
详细解决方案
PyQt switch开关切换
热度:30 发布时间:2023-12-24 13:30:43.0
相关解决方案
- switch()使用有关问题
- 关于aspx前台施用foreach根据条件遍历数据 关键字:aspx foreach switch
- |M| Switch 不记得如何用了 简单来看看
- switch 的这种用法你见过吗?小弟我还是头一回见到
- Switch 的有关问题大家帮忙啊
- C++ 关于 switch() 括号中表达式类型的简单有关问题
- jquerymobile-17 搜寻框、滑动按钮(Slider)、开关(Flip toggle switch)
- 在Myeclipse里运行程序是老是出现“Confirm Perspective Switch”对话框,怎 ...
- [求助]switch 语句为什么不能用?
- 为什么出现break outside switch or loop?
- 菜鸟笔记(3)switch
- Java switch 语句的用法
- 出现break outside switch or loop
- switch() case语句问题!
- switch ()语句的理解
- 新手求助(switch)
- 新手求助(switch)
- [求助]switch..!
- switch 输出报错!
- [求教]switch 如何设定
- Extjs4-退出store.each() unlabelled break must be inside loop or switch
- PJSIP + PYQT +VLC = SIP PHONE/IM 预备
- switch case 过多,不利于阅读,有没有简化方式?该怎么解决
- Switch Case,该怎么处理
- Android学习笔记-第四天-基础UI组件-RadioButton&CheckBox&ToggleButton&Switch-Button的子类
- android根本控件学习-ToggleButton&Switch
- switch-case的编译有关问题
- switch(expr1)中,expr1是一个整数表达式。因此传递给 switch 和 case 语句的参数应该是 int、 short、 char 或者 byte。long,string,该如何处理
- switch(),不是()里面的只能是int short byte char的么?解决思路
- enum(switch)的有关问题