Advertisement
Mori007

editingtexteditor

Feb 10th, 2021
1,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. # Text editor can be writing, editing and saving fastly.
  2.  
  3. from guizero import App, TextBox, PushButton, Box, Combo, Slider, CheckBox
  4.  
  5. app = App(title="My Text Editor with editing and saving file")
  6.  
  7. # function for reading files
  8. def open_file():
  9.     with open(file_name.value, "r") as f:
  10.         editor.value = f.read()
  11.  
  12. # functioning for saving files
  13. def save_file():
  14.     with open(file_name.value, "w") as f:
  15.         f.write(editor.value)
  16. # Disable the save button after saving file
  17.     save_button.visible = False
  18.  
  19. # Enable the save button after back to editing
  20. def enable_save():
  21.     save_button.show()
  22.    
  23. # create a preference new font, size and colour of the text, dark theme
  24. def change_font():
  25.     editor.font = font.value
  26.  
  27. def change_text_size():
  28.     editor.text_size = size.value
  29.     editor.resize(1, 1)
  30.     editor.resize("fill", "fill")
  31.  
  32. def change_colour():
  33.     editor.text_color = colour.value
  34.  
  35. def change_theme():
  36.     if dark_mode.value == 1:
  37.         app.bg = "black"
  38.         app.text_color = "white"
  39.     else:
  40.         app.bg = None
  41.         app.text_color = None
  42.  
  43.  
  44. # create a box to house the controls, we want the box to span the entire width of the app
  45. file_controls = Box(app, align="top", width="fill")
  46.  
  47. # create a TextBox for the file name
  48. file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
  49.  
  50. # create a save button which uses the save_file function
  51. save_button = PushButton(file_controls, text="Save",  command=save_file, align="right", visible=True)
  52.  
  53. # create an open button which uses the open_file function
  54. open_button = PushButton(file_controls, text="Open",  command=open_file, align="right", visible=True)
  55.  
  56. # create a TextBox which is not in the box and fills the rest of the GUI and editing the new text
  57. editor = TextBox(app, multiline=True, height="fill", width="fill", command=enable_save)
  58.  
  59. # create a box that consist controling the text and theme
  60. preferences_control = Box(app, layout='auto', align="bottom", width="fill")
  61. font = Combo(preferences_control,options=["verdana","arial","times new roman"], align="left", command=change_font)
  62. colour = Combo(preferences_control, options=["red","green","black"], align="left", command=change_colour)
  63. size = Slider(preferences_control,start=8, end=16, align="left", command=change_text_size)
  64. dark_mode = CheckBox(preferences_control, text="dark", align="left", command=change_theme)
  65.  
  66. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement