Advertisement
JDpaste

Python GUI guizero: Text Edit with Colour Picker Box pop-up

Oct 15th, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.66 KB | None | 0 0
  1. import string
  2. from guizero import App, Text, TextBox, PushButton, Box, Combo, Slider, Drawing, ButtonGroup
  3. app = App( height=680, width=800, title="Jedi Text Editor") # J edi(tor) - author JD.
  4.  
  5. #-----------------------
  6. def isHexString( thisStr ):
  7.     if len( thisStr ) < 1 : return False
  8.     for c in thisStr :
  9.         if not c in string.hexdigits :
  10.             return False
  11.     return True
  12.  
  13. #--------------------
  14. # COLOR functions
  15. #--------------------
  16. #-----------------------------------
  17. def getListFromColorString( colorStr ): # PREcondition: full color code "#rrggbb"
  18.     # extract color-component hex values from color-string using a slice [:] for sub-strings
  19.     # PREcondition: len(colorStr) == 7 and colorStr[0] == '#'
  20.     # gigo .. allow to fail ! No range check.
  21.    
  22.     red   = int( colorStr[1:3], 16) # parse int from hexadecimal (sub)string
  23.     green = int( colorStr[3:5], 16)
  24.     blue  = int( colorStr[5:7], 16)
  25.  
  26.     return ( red, green, blue ) # a tuple .. <3 int list> values in 0..255 (x00..xFF)
  27. #-----------------------------
  28. def getHexColorString( rgbList ): # PREcondition: <3 int list> values in 0..255 (x00..xFF)
  29.     # gigo .. allow to fail ! No range check.
  30.    
  31.     xRed   = "{0:02X}".format( rgbList[0] )  # create hexadecimal 2-digit string from int (in list)
  32.     xGreen = "{0:02X}".format( rgbList[1] )
  33.     xBlue  = "{0:02X}".format( rgbList[2] )
  34.  
  35.     return '#' + xRed + xGreen + xBlue  # full color code "#rrggbb"
  36.  
  37. #------------------------
  38. # FILE Handling functions
  39. #------------------------
  40. # function for reading files
  41. def open_file():
  42.     if ( not (file_name.value)):
  43.         app.error("NO filename provided", "Please provide a filename to Open ..")
  44.     else:
  45.         with open(file_name.value, "r") as f:
  46.             editPane.value = f.read()
  47.         # close ?
  48.  
  49. # function for writing files
  50. def save_file():
  51.     if ( not (file_name.value)):
  52.         app.error("NO filename provided", "Please provide a filename to Save to ..")
  53.     else:
  54.         with open(file_name.value, "w") as f:
  55.             f.write(editPane.value)
  56.         # close ?
  57.  
  58. #----------------------------------------
  59. # GLOBALS
  60. appChrome = "#424242"
  61.  
  62.  
  63. #-----------------------------------
  64. ###  COLOR PICKER widget methods ###
  65.  
  66. def loadColorSliders( colorValues ):
  67.     # load RGB color-component values into Sliders
  68.     # PREcondition: <3 int list> values in 0..255 (x00..xFF)
  69.     redSlider.value   = colorValues[0]
  70.     greenSlider.value = colorValues[1]
  71.     blueSlider.value  = colorValues[2]
  72. #----------------------------------------
  73. def bg_fgChange():
  74.     # load chosen color into Sliders
  75.     if ( bg_fg.value_text == "Background" ):
  76.         loadColorSliders( getListFromColorString( edit_bg ))
  77.     else:
  78.         loadColorSliders( getListFromColorString( edit_fg ))
  79. #----------------------------------------
  80. def colorChange():
  81.     global edit_bg  # ref Model value
  82.     global edit_fg
  83.     # values from Slider with (end=255) # 0..255 (xFF)
  84.     #---------------------------------
  85.     # snapshot the 3 RGB Slider values into a tuple
  86.     colorTuple = redSlider.value, greenSlider.value, blueSlider.value  
  87.     #
  88.     ##########################
  89.     # notify Views in widget #
  90.     # ---------------------- #
  91.    
  92.     colorPane.bg = colorTuple  # set color in pane
  93.    
  94.     colorStr   = getHexColorString( colorTuple )  # get full color code "#rrggbb"
  95.     # notify hex values (to Views)
  96.     colorHexText.value = colorStr
  97.     # extract color-component hex values from color-string using a slice [:] for sub-strings
  98.     redHex.value   = colorStr[1:3]
  99.     greenHex.value = colorStr[3:5]
  100.     blueHex.value  = colorStr[5:7]
  101.  
  102.     colorHexText_input.value = colorStr
  103.     ##############
  104.     # update app #
  105.     # ---------- #
  106.  
  107.     # color is applied to a widget property, as selected ..
  108.     if ( bg_fg.value == "Background"):  # radio selector
  109.         #
  110.         edit_bg = colorStr   # Model
  111.         editPane.bg = edit_bg  # View
  112.     else:
  113.         #
  114.         edit_fg = colorStr
  115.         editPane.text_color = edit_fg
  116. #----------------------------------------
  117.        
  118. #-----------
  119. def set_font(): # editPane attributes
  120.     editPane.font = font_combo.value
  121. #------------------
  122. def setEditFontSize(): # editPane attribute
  123.  
  124.     editPane.text_size = editTxtSizer.value # an int .. for strings .. int( size_combo.value )
  125.    
  126.     # not sure the following resize is necessary .. maybe if declared in sequence (no align )
  127.     editPane.resize(1, 1)
  128.     editPane.resize("fill", "fill")
  129.  
  130. def clear_text():
  131.     if ( app.yesno("RESET editor text", "Clear ALL editor text ?") ): editPane.value = ""
  132.  
  133. def popupColorPicker():
  134.     btn_colorPick.visible = False
  135.     colorWidget.visible   = True
  136.     colorHexText_input.focus()
  137.  
  138. def closeColorPicker():
  139.     btn_colorPick.visible = True
  140.     colorWidget.visible   = False
  141.     editPane.focus()
  142.  
  143. def loadColorString():
  144.     thisColor = colorHexText_input.value  # get value from TextBox
  145.     if len( thisColor ) != 7 or thisColor[0] != '#' : return
  146.     if isHexString( thisColor[1:] ) :
  147.         loadColorSliders( getListFromColorString( thisColor ))
  148.         #colorChange()
  149. #-------------------------------------
  150. ###################
  151. # declare Widgets #
  152. # --------------- #
  153.  
  154. #------------------------------------------------------
  155. # File controls #
  156. # - span the entire width of the app
  157. file_controls = Box(app, align="top", width="fill")
  158. file_controls.bg = appChrome
  159. file_controls.text_color = "#FFFFFF"
  160. #------------
  161. # New (Clear) button
  162. btn_clear = PushButton(file_controls, command=clear_text, text='New',align='left')  # reset text
  163. btn_clear.bg = "#FFFF00"
  164. btn_clear.text_color = "#000000"
  165. # Save button : uses the save_file function
  166. save_button = PushButton(file_controls, text="Save",  align="right", command = save_file )
  167.  
  168. save_button.bg = "#FF0000"
  169. save_button.text_color = "#000000"
  170. # Open button : uses the open_file function
  171. open_button = PushButton(file_controls, text="Open",  align="right", command = open_file )
  172.  
  173. open_button.bg = "#00FF00"
  174. open_button.text_color = "#000000"
  175. # File name
  176. file_name = TextBox(file_controls, text="text_file.txt", width=50, align="right")
  177. file_name.text_size= 14
  178. file_name.bg = "#FFFFFF"
  179. file_name.text_color = "#000000"
  180. #---------------------------------------------------
  181. # Font controls #
  182. # - span the entire width of the app
  183. font_controls = Box(app, align="bottom", width="fill")
  184. font_controls.bg = appChrome
  185. #------------
  186. font_text  = Text( font_controls, text = " View options: ", align = "left")
  187. font_text.text_color = 'white'
  188. fontOptions = ["Courier", "Helvetica", "Times New Roman", "Verdana"]
  189. font_combo = Combo(font_controls, align="left", width= 20, options= fontOptions, selected="Courier", command= set_font)
  190. font_combo.bg = "#44F397"
  191.  
  192. editTxtSizer = Slider(font_controls, align="left", start= 8, end= 42, command= setEditFontSize )
  193. editTxtSizer.bg = "#44F397"
  194. editTxtSizer.value = 22
  195. # nb. it looks like setting the .value triggers the command= <event-handler function>
  196.  
  197. btn_colorPick = PushButton(font_controls, text="Colors ..", align="left", command= popupColorPicker )
  198. btn_colorPick.bg = "#44F397"
  199. #----------------------------
  200. ###  COLOR PICKER widget  ###
  201. #----------------------------
  202. # pop-up Box: initially NOT VISIBLE
  203. colorWidget = Box( font_controls, border=1, width=300, height=380, align="bottom", visible=False )
  204.  
  205. colorTitleBox = Box( colorWidget, width="fill", align="top")
  206. colorTitle = Text(colorTitleBox, text='Color Select', height="fill", width= "fill", align="left" )
  207. colorTitle.bg = "#123456"
  208. colorTitle.text_color = 'white'
  209. # hide Box button .. [X] @ top-right
  210. btn_closeColor = PushButton(colorTitleBox,padx=0,pady=0, width=3, command= closeColorPicker, text='X', align="right")
  211. btn_closeColor.bg = 'black'
  212. btn_closeColor.text_color = 'white'
  213. btn_closeColor.text_size = 14
  214. #-------------
  215. # radio
  216. bg_fg = ButtonGroup(colorWidget, command= bg_fgChange, options=["Background", "Foreground"], selected="Foreground",horizontal=True, align="top")
  217. bg_fg.bg = "white"
  218. #------------------
  219. colorControls = Box(colorWidget, align="right", height="fill")
  220. colorControls.bg = '#89FF67'
  221.  
  222. #------------------
  223. colorHexText = Text(colorControls)
  224.  
  225. colorSliders   = Box(colorControls, height="fill", align="top")
  226. redControl   = Box(colorSliders, height="fill", align="left")
  227. redHex = Text(redControl)
  228. redHex.text_color = "red"
  229. redSlider = Slider(redControl, end=255, command= colorChange, horizontal=False, height=190)
  230. redSlider.bg = "red"
  231. redSlider.text_color = "white"
  232. #-----------------
  233. greenControl = Box(colorSliders, height="fill", align="left")
  234. greenHex = Text(greenControl)
  235. greenHex.text_color = "green"
  236. greenSlider = Slider(greenControl, end=255, command= colorChange, horizontal=False, height=190)
  237. greenSlider.bg = "green"
  238. greenSlider.text_color = "white"
  239. #-----------------
  240. blueControl  = Box(colorSliders, height="fill", align="left")
  241. blueHex = Text(blueControl)
  242. blueHex.text_color = "blue"
  243. blueSlider = Slider(blueControl, end=255, command= colorChange, horizontal=False, height=190)
  244. blueSlider.bg = 0,0,255 #(0,0,255) #[0,0,255] #"blue" # "#0000FF" ALL WORK
  245. blueSlider.text_color = "white"
  246. #-------------------------------
  247.  
  248. colorHexText_input = TextBox(colorControls)
  249. colorHexText_input.width = 8 # chars
  250. colorHexText_input.bg="white"
  251. colorHexText_input.text_size = 18
  252. # Save button : uses the save_file function
  253. loadColorTxt = PushButton( colorControls, text="Load color", command = loadColorString )
  254. loadColorTxt.bg = "#006200"
  255. loadColorTxt.text_color = "#FFFFFF"
  256. # other Views ..
  257. colorPane = Drawing(colorWidget, width=500, height="fill", align="left")
  258.  
  259. ###  COLOR PICKER - END  ###
  260. #---------------------------
  261.  
  262. #=======================
  263. # finally, the EDIT area .. take all available space. Maybe align='bottom' to stack on top of viewOptions Box
  264. startTxt = "Template text for initial styling.\n\n" +\
  265.            "After choosing View options below, please click [New] or [Open] to continue .."
  266. # declare the EDIT Pane - a TextBox in the app that fills the rest of the GUI
  267. editPane = TextBox(app, multiline=True, scrollbar=True, height="fill", width="fill", text=startTxt )
  268.  
  269.  
  270.  
  271. #=================
  272. # tweak widgets .. use event handler functions to apply initial settings ..
  273.  
  274. #----------------------------------------
  275. editPane.text_color = "#603E7B"  # start value to set colors from initial RGB values
  276. editPane.bg = "#F07FF0" # set colors from initial RGB values "red" #
  277.  
  278. # copy initial RGB values to Model
  279. # MODEL: Globals to hold color values for Foreground and Background
  280. edit_fg = editPane.text_color
  281. edit_bg = editPane.bg
  282.  
  283. bg_fgChange() # set initial RGB Slider values from Model
  284. set_font()    #
  285.  
  286. editPane.focus()
  287. #----------
  288. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement