Dup Ver Goto 📝

ColorDisplay

To
53 lines, 168 words, 1387 chars Page 'ColorDisplay' does not exist.

This has a simple line edit to type in a color name of some sort and, if valid, that color is displayed in a large rectangle. Basically an example of QLineEdit and its textEdited signal, and QColor.

#!/usr/bin/env python

from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from PySide6.QtNetwork import *
app = QApplication()

class Win( QWidget ):
  def __init__( self ):
    super().__init__()
    self.input = QLineEdit( self )
    self.input.move( 20, 20 )
    self.input.resize( 500, 50 )
    self.color = Qt.green
    self.resize( 640, 480 )
    self.input.textEdited.connect( self.handleChange )
  def handleChange( self, text ):
    if QColor.isValidColorName(text):
      color = QColor(text)
      self.color = color
      self.update()
  def paintEvent( self, e ):
    with QPainter( self ) as p:
      rect = self.rect()
      p.fillRect(rect,Qt.black)

      # checkerboard
      sqsize = 10
      grey = QColor("#777777")
      for x in range(rect.width()//sqsize):
        for y in range(rect.height()//sqsize):
          if ( x + y ) % 2 == 0:
            sqrect = QRect(x*sqsize,y*sqsize,sqsize,sqsize)
            p.fillRect(sqrect,grey)

      # color rect
      rect.adjust(20,80,-20,-20)
      p.fillRect(rect,self.color)

def main():
  win = Win()
  win.show()
  app.exec()

if __name__ == "__main__":
  main()