Guest User

Untitled

a guest
Feb 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. from PyQt4 import QtGui, QtCore
  2. import sys
  3. #===============================================================================
  4. # MyCheckBox
  5. #===============================================================================
  6. #className
  7. class MyCheckBox(QtGui.QCheckBox):
  8. #|-----------------------------------------------------------------------------|
  9. # class Variables
  10. #|-----------------------------------------------------------------------------|
  11. #no classVariables
  12.  
  13. #|-----------------------------------------------------------------------------|
  14. # Constructor
  15. #|-----------------------------------------------------------------------------|
  16. def __init__(self, *args, **kwargs):
  17. QtGui.QCheckBox.__init__(self, *args, **kwargs)
  18. self.setStyleSheet("background-color: rgb(0, 0, 0);n" +
  19. "color: rgb(255, 255, 255);n")
  20. #set default check as True
  21. self.setChecked(True)
  22. #set default enable as True
  23. # if it set to false will always remain on/off
  24. # here it is on as setChecked is True
  25. self.setEnabled(True)
  26. self._enable = True
  27. #|--------------------------End of Constructor---------------------------------|
  28. #|-----------------------------------------------------------------------------|
  29. # mousePressEvent
  30. #|-----------------------------------------------------------------------------|
  31. #overrite
  32. def mousePressEvent(self, *args, **kwargs):
  33. #tick on and off set here
  34. if self.isChecked():
  35. self.setChecked(False)
  36. else:
  37. self.setChecked(True)
  38. return QtGui.QCheckBox.mousePressEvent(self, *args, **kwargs)
  39. #|--------------------------End of mousePressEvent-----------------------------|
  40.  
  41. #|-----------------------------------------------------------------------------|
  42. # paintEvent
  43. #|-----------------------------------------------------------------------------|
  44. def paintEvent(self,event):
  45.  
  46. #just setting some size aspects
  47. self.setMinimumHeight(40)
  48. self.setMinimumWidth(100)
  49. self.setMaximumHeight(50)
  50. self.setMaximumWidth(150)
  51.  
  52. self.resize(self.parent().width(),self.parent().height())
  53. painter = QtGui.QPainter()
  54. painter.begin(self)
  55.  
  56. #for the black background
  57. brush = QtGui.QBrush(QtGui.QColor(0,0,0),style=QtCore.Qt.SolidPattern)
  58. painter.fillRect(self.rect(),brush)
  59.  
  60.  
  61. #smooth curves
  62. painter.setRenderHint(QtGui.QPainter.Antialiasing)
  63.  
  64. #for the on off font
  65. font = QtGui.QFont()
  66. font.setFamily("Courier New")
  67. font.setPixelSize(28)
  68. painter.setFont(font)
  69.  
  70. #change the look for on/off
  71. if self.isChecked():
  72. #blue fill
  73. brush = QtGui.QBrush(QtGui.QColor(50,50,255),style=QtCore.Qt.SolidPattern)
  74. painter.setBrush(brush)
  75.  
  76. #rounded rectangle as a whole
  77. painter.drawRoundedRect(0,0,self.width()-2,self.height()-2,
  78. self.height()/2,self.height()/2)
  79.  
  80. #white circle/button instead of the tick mark
  81. brush = QtGui.QBrush(QtGui.QColor(255,255,255),style=QtCore.Qt.SolidPattern)
  82. painter.setBrush(brush)
  83. painter.drawEllipse(self.width()-self.height(),0,self.height(),self.height())
  84.  
  85. #on text
  86. painter.drawText(self.width()/4,self.height()/1.5, "On")
  87.  
  88. else:
  89. #gray fill
  90. brush = QtGui.QBrush(QtGui.QColor(50,50,50),style=QtCore.Qt.SolidPattern)
  91. painter.setBrush(brush)
  92.  
  93. #rounded rectangle as a whole
  94. painter.drawRoundedRect(0,0,self.width()-2,self.height()-2,
  95. self.height()/2,self.height()/2)
  96.  
  97. #white circle/button instead of the tick but in different location
  98. brush = QtGui.QBrush(QtGui.QColor(255,255,255),style=QtCore.Qt.SolidPattern)
  99. painter.setBrush(brush)
  100. painter.drawEllipse(0,0,self.height(),self.height())
  101.  
  102. #off text
  103. painter.drawText(self.width()/2,self.height()/1.5, "Off")
  104.  
  105.  
  106. #|-----------------------End of paintEvent-------------------------------------|
  107. if __name__ == '__main__':
  108. app = QtGui.QApplication(sys.argv)
  109. wgt = QtGui.QWidget()
  110. wgt.setStyleSheet("background-color: rgb(0, 0, 0);n")
  111. cb = MyCheckBox()
  112. cb.setParent(wgt)
  113. layout = QtGui.QHBoxLayout()
  114. layout.addWidget(cb)
  115. wgt.resize(200,100)
  116. wgt.show()
  117. sys.exit(app.exec_())
  118.  
  119. slider = QSlider(Qt.Horizontal)
  120. slider.setStyleSheet('border: 1px solid #999999;height: 8px;)
Add Comment
Please, Sign In to add comment