JAWarr

Text Editor COLOUR V3

May 5th, 2022
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. ### Text Editor V3 05/05/2022
  2. from guizero import App, TextBox, PushButton, Box, Combo, Slider
  3.  
  4. # function for reading files
  5. def open_file():
  6.     with open(file_name.value, "r") as f:
  7.         editor.value = f.read()
  8.  
  9. # function for writing files
  10. def save_file():
  11.     with open(file_name.value, "w") as f:
  12.         f.write(editor.value)
  13.  
  14. # function for changing fonts
  15. def change_font():
  16.     editor.font = font.value
  17.  
  18. def change_text_size():
  19.     editor.text_size = size.value
  20.     '''
  21.    resize the widget because if the text is made bigger,
  22.    this might affect the size of the TextBox so guizero
  23.    needs to know how to maintain the intended layout
  24.    '''
  25.     editor.resize(1, 1)
  26.     editor.resize("fill", "fill")
  27.  
  28. # function for changing text colour
  29. def change_font_color():
  30.     choice = font_color.value
  31.     if choice == "Red":
  32.         editor.text_color = "Red"
  33.     if choice == "Green":
  34.         editor.text_color = "Green"
  35.     if choice == "Blue":
  36.         editor.text_color = "Blue"
  37.     if choice == "None":
  38.         editor.text_color = None
  39.    
  40.  
  41. app = App(title="texteditor")
  42.  
  43. # create a box to house the controls, we want the box to span the entire width of the app
  44. file_controls = Box(app, align="top", width="fill")
  45.  
  46. # create a TextBox for the file name
  47. file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
  48.  
  49. # create a save button which uses the save_file function
  50. save_button = PushButton(file_controls, text="Save", command=save_file, align="right")
  51.  
  52. # create an open button which uses the open_file function
  53. open_button = PushButton(file_controls, text="Open", command=open_file, align="right")
  54.  
  55. # create a TextBox which is not in the box and fills the rest of the GUI
  56. editor = TextBox(app, multiline=True, height="fill", width="fill")
  57.  
  58. # create a box to house the controls at the bottom, add a font combobox and size slider
  59. preferences_controls = Box(app, align="bottom", width="fill", border=True)
  60.  
  61. font = Combo(preferences_controls, options=["courier", "times new roman", "verdana"], align="left", command=change_font)
  62. size = Slider(preferences_controls,  align="right", command=change_text_size, start=8, end=42)
  63. font_color = Combo(preferences_controls, options=["Red", "Green", "Blue", "Reset"], align="bottom", command=change_font_color)
  64.  
  65. app.display()
  66.  
  67.  
Advertisement
Add Comment
Please, Sign In to add comment