Advertisement
jolgri

Assignment 10 Text editor

Jan 5th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. from guizero import App, TextBox, PushButton, Box, Combo, Slider, Text
  2.  
  3. app = App(title="texteditor")
  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 changing fonts
  11. def change_font():
  12. editor.font = font.value
  13.  
  14. # function for changing text size
  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.  
  21. # function for writing files
  22. def save_file():
  23. with open(file_name.value, "w") as f:
  24. f.write(editor.value)
  25.  
  26.  
  27. # create a box to house the controls, we want the box to span the entire width of the app
  28. file_controls = Box(app, align="top", width="fill")
  29.  
  30. # create a TextBox for the file name
  31. file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
  32.  
  33. # create a save button which uses the save_file function
  34. save_button = PushButton(file_controls, text="Save", align="right", command=save_file)
  35.  
  36. # create an open button which uses the open_file function
  37. open_button = PushButton(file_controls, text="Open", align="right", command=open_file)
  38.  
  39. # create a TextBox which is not in the box and fills the rest of the GUI
  40. editor = TextBox(app, multiline=True, height="fill", width="fill")
  41.  
  42. #create font changer
  43. preferences_controls = Box(app, align="bottom", width="fill", border=True)
  44. font_label = Text(preferences_controls, text="Text font:", align="left")
  45. font = Combo(preferences_controls, options=["courier", "times new roman", "verdana"], align="left", height=2, command=change_font)
  46.  
  47. #create size slider
  48. size_label = Text(preferences_controls, text="Text size:", align="left")
  49. size = Slider(preferences_controls, align="left", command=change_text_size, start=10, end=42)
  50.  
  51. app.display()# Skriv din kod här :-)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement