Advertisement
brendan-stanford

Texteditor-GUI-text_preferences

Oct 11th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. from guizero import App, TextBox, PushButton, Box, Combo, Slider
  2.  
  3. app = App(title="text editor")
  4.  
  5. # function for reading files
  6. def open_file():
  7. with open(file_name.value, "r") as f:
  8. editor.value = f.read()
  9.  
  10. # function for writing files
  11. def save_file():
  12. with open(file_name.value, "w") as f:
  13. f.write(editor.value)
  14.  
  15. # function for changing font style
  16. def change_font():
  17. editor.font = font.value
  18.  
  19. #function for changing text size
  20. def change_text_size():
  21. editor.text_size = size.value
  22. #reset box size to adjust to new text size
  23. editor.resize(1,1)
  24. editor.resize("fill", "fill")
  25.  
  26. def change_color():
  27. editor.text_color = colour.value
  28.  
  29. # create a box to house the controls, we want the box to span the entire width of the app
  30. file_controls = Box(app, align="top", width="fill", border = True)
  31.  
  32. #Create a new box to house text preferences
  33. text_preferences = Box(app, align = "top", width = "fill", border = True)
  34. font = Combo(text_preferences, align = "left", options = ["courier", "helvetica", "times new roman", "verdana"], command = change_font)
  35. size = Slider(text_preferences, align = "left", command = change_text_size, start = 10, end = 24)
  36. colour = Combo(text_preferences, align = "left", command = change_color, options = ["black", "red", "blue", "green"])
  37.  
  38. # create an open button which uses the open_file function
  39. open_button = PushButton(file_controls, text="Open", align="left", command = open_file)
  40.  
  41. # create a TextBox for the file name
  42. file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
  43.  
  44. # create a save button which uses the save_file function
  45. save_button = PushButton(file_controls, text="Save", align="right", command = save_file)
  46.  
  47. # create a TextBox which is not in the box and fills the rest of the GUI
  48. editor = TextBox(app, multiline=True, height="fill", width="fill")
  49.  
  50. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement