Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This Python code implements a basic Colour Picker.
- # Colours are specified in RGB format. Hex "#rrggbb" is generated.
- # It is a demo of the GUI guizero (with Slider command)
- #
- # guizero works with tkinter but simpler with extra widgets eg. Slider
- # https://lawsie.github.io/guizero/slider/
- # nb. Slider contains tkinter.Scale
- from guizero import App, Box, Text, TextBox, Slider, Drawing
- app = App( height=680, width=500)
- #-------------------- -------------------
- def getHexColorString(iRed, iGreen, iBlue): # PREcondition: 0..255 (x00..xFF)
- # gigo .. allow to fail ! No range check.
- xRed = "{0:02X}".format(iRed)
- xGreen = "{0:02X}".format(iGreen)
- xBlue = "{0:02X}".format(iBlue)
- return '#' + xRed + xGreen + xBlue
- #----------------------------------------
- def colorChange(): # 0..255 (xFF)
- # values from Slider with (end=255)
- #-------------------
- # get full color code "#rrggbb"
- newColor = getHexColorString( redSlider.value, greenSlider.value, blueSlider.value )
- # -----------------
- ################
- # notify Views #
- # ------------ #
- # set color in pane
- colorPane.bg = newColor
- # notify hex values (to Views)
- colorHexText.value = newColor
- colorHexTextCOPY.value = newColor
- # extract color-component hex values from color-string using a slice [:] for sub-strings
- redHex.value = newColor[1:3]
- greenHex.value = newColor[3:5]
- blueHex.value = newColor[5:7]
- #---------------
- # add widgets ..
- h1 = Text(app, text="Colour Picker", size=26, font="Times New Roman")
- ## ----
- colorWidget = Box(app, border=1)
- #-------------------------------
- colorControls = Box(colorWidget)
- #------------------
- redControl = Box(colorControls, height="fill", align="left")
- redHex = Text(redControl)
- redHex.text_color = "red"
- redSlider = Slider(redControl, end=255, command= colorChange, horizontal=False)
- redSlider.bg = "red"
- redSlider.text_color = "white"
- #-----------------
- greenControl = Box(colorControls, height="fill", align="left")
- greenHex = Text(greenControl)
- greenHex.text_color = "green"
- greenSlider = Slider(greenControl, end=255, command= colorChange, horizontal=False)
- greenSlider.bg = "green"
- greenSlider.text_color = "white"
- #-----------------
- blueControl = Box(colorControls, height="fill", align="left")
- blueHex = Text(blueControl)
- blueHex.text_color = "blue"
- blueSlider = Slider(blueControl, end=255, command= colorChange, horizontal=False)
- blueSlider.bg = "blue"
- blueSlider.text_color = "white"
- #-------------------------------
- # other Views ..
- colorHexText = Text(colorWidget)
- colorPane = Drawing(colorWidget, width=500, height="fill")
- colorHexCOPYlabel = Text(colorWidget, text="To COPY 'color-text' use: [Ctrl A] .. then .. [Ctrl C]")
- colorHexTextCOPY = TextBox(colorWidget)
- colorHexTextCOPY.width = 8 # chars
- colorHexTextCOPY.bg="white"
- colorHexTextCOPY.text_size = 18
- #-------------------------
- #set initial RGB Slider values ..
- redSlider.value = 75
- greenSlider.value = 255
- blueSlider.value = 146
- colorChange()
- colorHexTextCOPY.focus() # ready to COPY
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement