Advertisement
JDpaste

Python GUI guizero: Text Edit prototype with Colour Picker

Oct 7th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.89 KB | None | 0 0
  1. # This Python code implements a prototype Text Editor with basic Colour Picker.
  2. # Colours are specified in RGB format. Text can be written to / read from file.
  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.  
  8. # This code is refactored / fixed by JD.
  9. from guizero import *
  10. app = App( width=700, height=600, title='Chochi Chode')
  11.  
  12. #----------------------------------
  13. def showColor():
  14.     # get current values from Sliders
  15.     red   = c1Slider.value
  16.     green = c2Slider.value
  17.     blue  = c3Slider.value
  18.     # notify Views ..
  19.     colorSelect.value = "("+str(red)+", "+str(green)+", "+str(blue)+")"
  20.    
  21.     nombre =  [red,green,blue] # specify color in list format [ iR, iG, iB ]
  22.     colorBox.bg = nombre
  23.     txt_archive.text_color = nombre # set color of font in edit area
  24. #----------------------------------
  25.  
  26. def open_file():
  27.     if ( not (txt_name.value)):
  28.         app.error("NO filename provided", "Please provide a filename to Open ..")
  29.     else:
  30.         with open(txt_name.value, 'r') as f:
  31.             txt_archive.value = f.read()
  32.  
  33. def save_file():
  34.     if ( not (txt_name.value)):
  35.         app.error("NO filename provided", "Please provide a filename to Save to ..")
  36.     else:
  37.         with open(txt_name.value, 'w') as f:
  38.             f.write(txt_archive.value)
  39.  
  40. #----------------------------------------
  41. editFontSize = 14 # global value in Model : font size initial value
  42.  
  43. def incFontSize(): # increment + 2
  44.     global editFontSize
  45.     editFontSize += 2
  46.     # notify View ..
  47.     txt_archive.text_size = editFontSize
  48.  
  49. def decFontSize(): # decrement - 2
  50.     global editFontSize
  51.     editFontSize -= 2
  52.     if (editFontSize < 6 ): editFontSize = 6  # min limit
  53.     # notify View ..
  54.     txt_archive.text_size = editFontSize
  55. #----------------------------------------
  56. """ REPLACED WITH CODE ABOVE.
  57. # CALLED FROM PushButton: command = myFunctionName
  58. # .. NO ARGUMENT is passed.
  59. # myFunctionName evaluates to a "reference to the function" so named. This ref can then be used to call the function later eg. on event ..
  60. # myFunctionName() is a direct call to execute the function and evaluates to the return value of the function.
  61. #-----------------
  62.  
  63. def fontSize(font):
  64.    print("in fontSize") # debug
  65.    if font == 'b':
  66.        txt_archive.text_size = archive_txt_size+2
  67.    else:
  68.        txt_archive.text_size = archive_txt_size-2
  69. """
  70. def fontColor():
  71.     colors.visible = True
  72.  
  73. def clear_text():
  74.     if ( app.yesno("RESET editor text", "Clear ALL editor text ?") ): txt_archive.value = ""
  75.  
  76. ###################
  77. # declare Widgets #
  78. # --------------- #
  79. colors = Window( app, title='Color Select', width=300, height=300, visible=False )  # pop-up: initially NOT VISIBLE
  80. colorSelect = Text(colors, size=16, align='top', text='El color creado se vera aqui')
  81. colorBox    = Box(colors, width=100,height=100)
  82.  
  83. t1Slider = Text(colors, text='Red',align='left')
  84. c1Slider = Slider(colors,start=0,end=255,horizontal=False,align='left',command=showColor)
  85. t2Slider = Text(colors, text='Green',align='left')
  86. c2Slider = Slider(colors,start=0,end=255,horizontal=False,align='left',command=showColor)
  87. t3Slider = Text(colors, text='Blue',align='left')
  88. c3Slider = Slider(colors,start=0,end=255,horizontal=False,align='left',command=showColor)
  89. #----------------
  90.  
  91. caja_header = Box(app,align='top',width='fill')
  92.  
  93. label_name = Text(caja_header, text='Nombre del archivo - FILE PATH', align='left')
  94. txt_name   = TextBox(caja_header, align='left',width=20)
  95. btn_open   = PushButton(caja_header, command=open_file, text='Abrir - OPEN',align='right')
  96. btn_save   = PushButton(caja_header, command=save_file, text='Guardar - SAVE',align='right')
  97. btn_clear  = PushButton(caja_header, command=clear_text, text='Clear ALL',align='right')  # reset text
  98.  
  99. #----------------
  100. viewOptions = Box(app, width='fill', align='bottom' ) # new Box for bottom alignment of View controls
  101. btn_font_bigger  = PushButton(viewOptions, command=incFontSize, text='A+',align='left')
  102. btn_font_smaller = PushButton(viewOptions, command=decFontSize, text='A-',align='left')
  103. #btn_font_smaller = PushButton(app, fontColor, text='FC',align='left') # paste error .. rename follows ...
  104. btn_color_window = PushButton(viewOptions, fontColor, text='FC - COLOR',align='left')
  105. #=======================
  106. # finally, the EDIT area .. take all available space. Maybe align='bottom' to stack on top of viewOptions Box
  107. startTxt = "Template text for initial styling.\n\n" +\
  108.            "After choosing View options below, please click [Clear ALL] or [Abrir - OPEN] to continue .."
  109. txt_archive = TextBox(app, height='fill', width='fill', multiline=True, scrollbar=True, text=startTxt ) # scrollbar is handy !
  110. txt_archive.text_size = editFontSize # SET INITIAL SIZE of font in the EDIT area
  111. txt_archive.focus() # ready for input ..
  112.  
  113.  
  114. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement