Advertisement
JDpaste

Python GUI guizero: basic Colour Picker

Oct 1st, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. # This Python code implements a basic Colour Picker.
  2. # Colours are specified in RGB format. Hex "#rrggbb" is generated.
  3. # It is a demo of the GUI guizero (with Slider command)
  4. #
  5. # guizero works with tkinter but simpler with extra widgets eg. Slider
  6. # https://lawsie.github.io/guizero/slider/
  7. # nb. Slider contains tkinter.Scale
  8.  
  9. from guizero import App, Box, Text, TextBox, Slider, Drawing
  10. app = App( height=680, width=500)
  11. #-------------------- -------------------
  12. def getHexColorString(iRed, iGreen, iBlue): # PREcondition: 0..255 (x00..xFF)
  13.     # gigo .. allow to fail ! No range check.
  14.     xRed   = "{0:02X}".format(iRed)
  15.     xGreen = "{0:02X}".format(iGreen)
  16.     xBlue  = "{0:02X}".format(iBlue)
  17.  
  18.     return '#' + xRed + xGreen + xBlue
  19. #----------------------------------------
  20.  
  21. def colorChange(): # 0..255 (xFF)
  22.     # values from Slider with (end=255)
  23.     #-------------------
  24.     # get full color code "#rrggbb"
  25.     newColor =  getHexColorString( redSlider.value, greenSlider.value, blueSlider.value )
  26.     #           -----------------
  27.     ################
  28.     # notify Views #
  29.     # ------------ #
  30.     # set color in pane
  31.     colorPane.bg = newColor
  32.    
  33.     # notify hex values (to Views)
  34.     colorHexText.value     = newColor
  35.     colorHexTextCOPY.value = newColor
  36.     # extract color-component hex values from color-string using a slice [:] for sub-strings
  37.     redHex.value   = newColor[1:3]
  38.     greenHex.value = newColor[3:5]
  39.     blueHex.value  = newColor[5:7]
  40.  
  41. #---------------    
  42. # add widgets ..
  43. h1 = Text(app, text="Colour Picker", size=26, font="Times New Roman")
  44. ##   ----
  45. colorWidget = Box(app, border=1)
  46. #-------------------------------
  47. colorControls = Box(colorWidget)
  48. #------------------
  49. redControl   = Box(colorControls, height="fill", align="left")
  50. redHex = Text(redControl)
  51. redHex.text_color = "red"
  52. redSlider = Slider(redControl, end=255, command= colorChange, horizontal=False)
  53. redSlider.bg = "red"
  54. redSlider.text_color = "white"
  55. #-----------------
  56. greenControl = Box(colorControls, height="fill", align="left")
  57. greenHex = Text(greenControl)
  58. greenHex.text_color = "green"
  59. greenSlider = Slider(greenControl, end=255, command= colorChange, horizontal=False)
  60. greenSlider.bg = "green"
  61. greenSlider.text_color = "white"
  62. #-----------------
  63. blueControl  = Box(colorControls, height="fill", align="left")
  64. blueHex = Text(blueControl)
  65. blueHex.text_color = "blue"
  66. blueSlider = Slider(blueControl, end=255, command= colorChange, horizontal=False)
  67. blueSlider.bg = "blue"
  68. blueSlider.text_color = "white"
  69. #-------------------------------
  70. # other Views ..
  71. colorHexText = Text(colorWidget)
  72. colorPane = Drawing(colorWidget, width=500, height="fill")
  73.  
  74. colorHexCOPYlabel = Text(colorWidget, text="To COPY 'color-text' use: [Ctrl A] .. then .. [Ctrl C]")
  75. colorHexTextCOPY = TextBox(colorWidget)
  76. colorHexTextCOPY.width = 8 # chars
  77. colorHexTextCOPY.bg="white"
  78. colorHexTextCOPY.text_size = 18
  79. #-------------------------
  80. #set initial RGB Slider values ..
  81. redSlider.value   =  75
  82. greenSlider.value = 255
  83. blueSlider.value  = 146
  84. colorChange()
  85.  
  86. colorHexTextCOPY.focus()  # ready to COPY
  87. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement