Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This Python code implements a prototype Text Editor with basic Colour Picker.
- # Colours are specified in RGB format. Text can be written to / read from file.
- # 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/
- # This code is refactored / fixed by JD.
- from guizero import *
- app = App( width=700, height=600, title='Chochi Chode')
- #----------------------------------
- def showColor():
- # get current values from Sliders
- red = c1Slider.value
- green = c2Slider.value
- blue = c3Slider.value
- # notify Views ..
- colorSelect.value = "("+str(red)+", "+str(green)+", "+str(blue)+")"
- nombre = [red,green,blue] # specify color in list format [ iR, iG, iB ]
- colorBox.bg = nombre
- txt_archive.text_color = nombre # set color of font in edit area
- #----------------------------------
- def open_file():
- if ( not (txt_name.value)):
- app.error("NO filename provided", "Please provide a filename to Open ..")
- else:
- with open(txt_name.value, 'r') as f:
- txt_archive.value = f.read()
- def save_file():
- if ( not (txt_name.value)):
- app.error("NO filename provided", "Please provide a filename to Save to ..")
- else:
- with open(txt_name.value, 'w') as f:
- f.write(txt_archive.value)
- #----------------------------------------
- editFontSize = 14 # global value in Model : font size initial value
- def incFontSize(): # increment + 2
- global editFontSize
- editFontSize += 2
- # notify View ..
- txt_archive.text_size = editFontSize
- def decFontSize(): # decrement - 2
- global editFontSize
- editFontSize -= 2
- if (editFontSize < 6 ): editFontSize = 6 # min limit
- # notify View ..
- txt_archive.text_size = editFontSize
- #----------------------------------------
- """ REPLACED WITH CODE ABOVE.
- # CALLED FROM PushButton: command = myFunctionName
- # .. NO ARGUMENT is passed.
- # myFunctionName evaluates to a "reference to the function" so named. This ref can then be used to call the function later eg. on event ..
- # myFunctionName() is a direct call to execute the function and evaluates to the return value of the function.
- #-----------------
- def fontSize(font):
- print("in fontSize") # debug
- if font == 'b':
- txt_archive.text_size = archive_txt_size+2
- else:
- txt_archive.text_size = archive_txt_size-2
- """
- def fontColor():
- colors.visible = True
- def clear_text():
- if ( app.yesno("RESET editor text", "Clear ALL editor text ?") ): txt_archive.value = ""
- ###################
- # declare Widgets #
- # --------------- #
- colors = Window( app, title='Color Select', width=300, height=300, visible=False ) # pop-up: initially NOT VISIBLE
- colorSelect = Text(colors, size=16, align='top', text='El color creado se vera aqui')
- colorBox = Box(colors, width=100,height=100)
- t1Slider = Text(colors, text='Red',align='left')
- c1Slider = Slider(colors,start=0,end=255,horizontal=False,align='left',command=showColor)
- t2Slider = Text(colors, text='Green',align='left')
- c2Slider = Slider(colors,start=0,end=255,horizontal=False,align='left',command=showColor)
- t3Slider = Text(colors, text='Blue',align='left')
- c3Slider = Slider(colors,start=0,end=255,horizontal=False,align='left',command=showColor)
- #----------------
- caja_header = Box(app,align='top',width='fill')
- label_name = Text(caja_header, text='Nombre del archivo - FILE PATH', align='left')
- txt_name = TextBox(caja_header, align='left',width=20)
- btn_open = PushButton(caja_header, command=open_file, text='Abrir - OPEN',align='right')
- btn_save = PushButton(caja_header, command=save_file, text='Guardar - SAVE',align='right')
- btn_clear = PushButton(caja_header, command=clear_text, text='Clear ALL',align='right') # reset text
- #----------------
- viewOptions = Box(app, width='fill', align='bottom' ) # new Box for bottom alignment of View controls
- btn_font_bigger = PushButton(viewOptions, command=incFontSize, text='A+',align='left')
- btn_font_smaller = PushButton(viewOptions, command=decFontSize, text='A-',align='left')
- #btn_font_smaller = PushButton(app, fontColor, text='FC',align='left') # paste error .. rename follows ...
- btn_color_window = PushButton(viewOptions, fontColor, text='FC - COLOR',align='left')
- #=======================
- # 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 [Clear ALL] or [Abrir - OPEN] to continue .."
- txt_archive = TextBox(app, height='fill', width='fill', multiline=True, scrollbar=True, text=startTxt ) # scrollbar is handy !
- txt_archive.text_size = editFontSize # SET INITIAL SIZE of font in the EDIT area
- txt_archive.focus() # ready for input ..
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement