Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string
- from guizero import App, Text, TextBox, PushButton, Box, Combo, Slider, Drawing, ButtonGroup
- app = App( height=680, width=800, title="Jedi Text Editor") # J edi(tor) - author JD.
- #-----------------------
- def isHexString( thisStr ):
- if len( thisStr ) < 1 : return False
- for c in thisStr :
- if not c in string.hexdigits :
- return False
- return True
- #--------------------
- # COLOR functions
- #--------------------
- #-----------------------------------
- def getListFromColorString( colorStr ): # PREcondition: full color code "#rrggbb"
- # extract color-component hex values from color-string using a slice [:] for sub-strings
- # PREcondition: len(colorStr) == 7 and colorStr[0] == '#'
- # gigo .. allow to fail ! No range check.
- red = int( colorStr[1:3], 16) # parse int from hexadecimal (sub)string
- green = int( colorStr[3:5], 16)
- blue = int( colorStr[5:7], 16)
- return ( red, green, blue ) # a tuple .. <3 int list> values in 0..255 (x00..xFF)
- #-----------------------------
- def getHexColorString( rgbList ): # PREcondition: <3 int list> values in 0..255 (x00..xFF)
- # gigo .. allow to fail ! No range check.
- xRed = "{0:02X}".format( rgbList[0] ) # create hexadecimal 2-digit string from int (in list)
- xGreen = "{0:02X}".format( rgbList[1] )
- xBlue = "{0:02X}".format( rgbList[2] )
- return '#' + xRed + xGreen + xBlue # full color code "#rrggbb"
- #------------------------
- # FILE Handling functions
- #------------------------
- # function for reading files
- def open_file():
- if ( not (file_name.value)):
- app.error("NO filename provided", "Please provide a filename to Open ..")
- else:
- with open(file_name.value, "r") as f:
- editPane.value = f.read()
- # close ?
- # function for writing files
- def save_file():
- if ( not (file_name.value)):
- app.error("NO filename provided", "Please provide a filename to Save to ..")
- else:
- with open(file_name.value, "w") as f:
- f.write(editPane.value)
- # close ?
- #----------------------------------------
- # GLOBALS
- appChrome = "#424242"
- #-----------------------------------
- ### COLOR PICKER widget methods ###
- def loadColorSliders( colorValues ):
- # load RGB color-component values into Sliders
- # PREcondition: <3 int list> values in 0..255 (x00..xFF)
- redSlider.value = colorValues[0]
- greenSlider.value = colorValues[1]
- blueSlider.value = colorValues[2]
- #----------------------------------------
- def bg_fgChange():
- # load chosen color into Sliders
- if ( bg_fg.value_text == "Background" ):
- loadColorSliders( getListFromColorString( edit_bg ))
- else:
- loadColorSliders( getListFromColorString( edit_fg ))
- #----------------------------------------
- def colorChange():
- global edit_bg # ref Model value
- global edit_fg
- # values from Slider with (end=255) # 0..255 (xFF)
- #---------------------------------
- # snapshot the 3 RGB Slider values into a tuple
- colorTuple = redSlider.value, greenSlider.value, blueSlider.value
- #
- ##########################
- # notify Views in widget #
- # ---------------------- #
- colorPane.bg = colorTuple # set color in pane
- colorStr = getHexColorString( colorTuple ) # get full color code "#rrggbb"
- # notify hex values (to Views)
- colorHexText.value = colorStr
- # extract color-component hex values from color-string using a slice [:] for sub-strings
- redHex.value = colorStr[1:3]
- greenHex.value = colorStr[3:5]
- blueHex.value = colorStr[5:7]
- colorHexText_input.value = colorStr
- ##############
- # update app #
- # ---------- #
- # color is applied to a widget property, as selected ..
- if ( bg_fg.value == "Background"): # radio selector
- #
- edit_bg = colorStr # Model
- editPane.bg = edit_bg # View
- else:
- #
- edit_fg = colorStr
- editPane.text_color = edit_fg
- #----------------------------------------
- #-----------
- def set_font(): # editPane attributes
- editPane.font = font_combo.value
- #------------------
- def setEditFontSize(): # editPane attribute
- editPane.text_size = editTxtSizer.value # an int .. for strings .. int( size_combo.value )
- # not sure the following resize is necessary .. maybe if declared in sequence (no align )
- editPane.resize(1, 1)
- editPane.resize("fill", "fill")
- def clear_text():
- if ( app.yesno("RESET editor text", "Clear ALL editor text ?") ): editPane.value = ""
- def popupColorPicker():
- btn_colorPick.visible = False
- colorWidget.visible = True
- colorHexText_input.focus()
- def closeColorPicker():
- btn_colorPick.visible = True
- colorWidget.visible = False
- editPane.focus()
- def loadColorString():
- thisColor = colorHexText_input.value # get value from TextBox
- if len( thisColor ) != 7 or thisColor[0] != '#' : return
- if isHexString( thisColor[1:] ) :
- loadColorSliders( getListFromColorString( thisColor ))
- #colorChange()
- #-------------------------------------
- ###################
- # declare Widgets #
- # --------------- #
- #------------------------------------------------------
- # File controls #
- # - span the entire width of the app
- file_controls = Box(app, align="top", width="fill")
- file_controls.bg = appChrome
- file_controls.text_color = "#FFFFFF"
- #------------
- # New (Clear) button
- btn_clear = PushButton(file_controls, command=clear_text, text='New',align='left') # reset text
- btn_clear.bg = "#FFFF00"
- btn_clear.text_color = "#000000"
- # Save button : uses the save_file function
- save_button = PushButton(file_controls, text="Save", align="right", command = save_file )
- save_button.bg = "#FF0000"
- save_button.text_color = "#000000"
- # Open button : uses the open_file function
- open_button = PushButton(file_controls, text="Open", align="right", command = open_file )
- open_button.bg = "#00FF00"
- open_button.text_color = "#000000"
- # File name
- file_name = TextBox(file_controls, text="text_file.txt", width=50, align="right")
- file_name.text_size= 14
- file_name.bg = "#FFFFFF"
- file_name.text_color = "#000000"
- #---------------------------------------------------
- # Font controls #
- # - span the entire width of the app
- font_controls = Box(app, align="bottom", width="fill")
- font_controls.bg = appChrome
- #------------
- font_text = Text( font_controls, text = " View options: ", align = "left")
- font_text.text_color = 'white'
- fontOptions = ["Courier", "Helvetica", "Times New Roman", "Verdana"]
- font_combo = Combo(font_controls, align="left", width= 20, options= fontOptions, selected="Courier", command= set_font)
- font_combo.bg = "#44F397"
- editTxtSizer = Slider(font_controls, align="left", start= 8, end= 42, command= setEditFontSize )
- editTxtSizer.bg = "#44F397"
- editTxtSizer.value = 22
- # nb. it looks like setting the .value triggers the command= <event-handler function>
- btn_colorPick = PushButton(font_controls, text="Colors ..", align="left", command= popupColorPicker )
- btn_colorPick.bg = "#44F397"
- #----------------------------
- ### COLOR PICKER widget ###
- #----------------------------
- # pop-up Box: initially NOT VISIBLE
- colorWidget = Box( font_controls, border=1, width=300, height=380, align="bottom", visible=False )
- colorTitleBox = Box( colorWidget, width="fill", align="top")
- colorTitle = Text(colorTitleBox, text='Color Select', height="fill", width= "fill", align="left" )
- colorTitle.bg = "#123456"
- colorTitle.text_color = 'white'
- # hide Box button .. [X] @ top-right
- btn_closeColor = PushButton(colorTitleBox,padx=0,pady=0, width=3, command= closeColorPicker, text='X', align="right")
- btn_closeColor.bg = 'black'
- btn_closeColor.text_color = 'white'
- btn_closeColor.text_size = 14
- #-------------
- # radio
- bg_fg = ButtonGroup(colorWidget, command= bg_fgChange, options=["Background", "Foreground"], selected="Foreground",horizontal=True, align="top")
- bg_fg.bg = "white"
- #------------------
- colorControls = Box(colorWidget, align="right", height="fill")
- colorControls.bg = '#89FF67'
- #------------------
- colorHexText = Text(colorControls)
- colorSliders = Box(colorControls, height="fill", align="top")
- redControl = Box(colorSliders, height="fill", align="left")
- redHex = Text(redControl)
- redHex.text_color = "red"
- redSlider = Slider(redControl, end=255, command= colorChange, horizontal=False, height=190)
- redSlider.bg = "red"
- redSlider.text_color = "white"
- #-----------------
- greenControl = Box(colorSliders, height="fill", align="left")
- greenHex = Text(greenControl)
- greenHex.text_color = "green"
- greenSlider = Slider(greenControl, end=255, command= colorChange, horizontal=False, height=190)
- greenSlider.bg = "green"
- greenSlider.text_color = "white"
- #-----------------
- blueControl = Box(colorSliders, height="fill", align="left")
- blueHex = Text(blueControl)
- blueHex.text_color = "blue"
- blueSlider = Slider(blueControl, end=255, command= colorChange, horizontal=False, height=190)
- blueSlider.bg = 0,0,255 #(0,0,255) #[0,0,255] #"blue" # "#0000FF" ALL WORK
- blueSlider.text_color = "white"
- #-------------------------------
- colorHexText_input = TextBox(colorControls)
- colorHexText_input.width = 8 # chars
- colorHexText_input.bg="white"
- colorHexText_input.text_size = 18
- # Save button : uses the save_file function
- loadColorTxt = PushButton( colorControls, text="Load color", command = loadColorString )
- loadColorTxt.bg = "#006200"
- loadColorTxt.text_color = "#FFFFFF"
- # other Views ..
- colorPane = Drawing(colorWidget, width=500, height="fill", align="left")
- ### COLOR PICKER - END ###
- #---------------------------
- #=======================
- # finally, the EDIT area .. take all available space. Maybe align='bottom' to stack on top of viewOptions Box
- startTxt = "Template text for initial styling.\n\n" +\
- "After choosing View options below, please click [New] or [Open] to continue .."
- # declare the EDIT Pane - a TextBox in the app that fills the rest of the GUI
- editPane = TextBox(app, multiline=True, scrollbar=True, height="fill", width="fill", text=startTxt )
- #=================
- # tweak widgets .. use event handler functions to apply initial settings ..
- #----------------------------------------
- editPane.text_color = "#603E7B" # start value to set colors from initial RGB values
- editPane.bg = "#F07FF0" # set colors from initial RGB values "red" #
- # copy initial RGB values to Model
- # MODEL: Globals to hold color values for Foreground and Background
- edit_fg = editPane.text_color
- edit_bg = editPane.bg
- bg_fgChange() # set initial RGB Slider values from Model
- set_font() #
- editPane.focus()
- #----------
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement