Advertisement
zhongnaomi

text editor

Jan 2nd, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.85 KB | None | 0 0
  1. from guizero import App, TextBox, PushButton, Box,Combo,Slider
  2.  
  3. app = App(title="texteditor")
  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. def change_font():
  14.     editor.font = font.value
  15. def change_text_size():
  16.     editor.text_size = size.value
  17.     # resize the widget because if the text is made bigger, this might affect the size of the TextBox so guizero needs to know how to maintain the intended layout
  18.     editor.resize(1, 1)
  19.     editor.resize("fill", "fill")
  20. def change_color():
  21.     editor.text_color = color.value
  22. # create a box to house the controls, we want the box to span the entire width of the app
  23. file_controls = Box(app, align="right", height="fill")
  24. file_controls2=Box(app, align="top", width="fill")
  25. preferences_controls = Box(app, align="bottom", width="fill", border=True)
  26. font = Combo(preferences_controls, options=["courier", "times new roman", "verdana"], align="left", command=change_font)
  27. size = Slider(preferences_controls,  align="left", command=change_text_size, start=10, end=18)
  28. color = Combo(preferences_controls, options=["red", "yellow", "blue","white","pink"], align="left", command=change_color)
  29. # create a TextBox for the file name
  30. file_name = TextBox(file_controls2, text="text_file.txt", width=60, align="right")
  31. # create a save button which uses the save_file function
  32. save_button = PushButton(file_controls, text="Save",  align="centre",command=save_file)
  33.  
  34. # create an open button which uses the open_file function
  35. open_button = PushButton(file_controls, text="Open",  align="centre",command=open_file)
  36.  
  37. # create a TextBox which is not in the box and fills the rest of the GUI
  38. editor = TextBox(app, multiline=True, height="fill", width="fill")
  39.  
  40. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement