Advertisement
brendan-stanford

Texteditor-GUI-Menu

Oct 11th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. from guizero import App, TextBox, PushButton, Box, Combo, Slider, MenuBar
  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. #disable save button till edited
  10. save_button.hide()
  11. file_name.show()
  12.  
  13. # function for writing files
  14. def save_file():
  15. with open(file_name.value, "w") as f:
  16. f.write(editor.value)
  17. #disable save button till edited
  18. save_button.hide()
  19. file_name.show()
  20.  
  21. # function for changing font style
  22. def change_font():
  23. editor.font = font.value
  24.  
  25. #function for changing text size
  26. def change_text_size():
  27. editor.text_size = size.value
  28. #reset box size to adjust to new text size
  29. editor.resize(1,1)
  30. editor.resize("fill", "fill")
  31.  
  32. #function for changing text colour
  33. def change_color():
  34. editor.text_color = colour.value
  35.  
  36. #function to re-anable save button
  37. def save_enable():
  38. save_button.show()
  39.  
  40. #function that reveals file name after initializing as hidden
  41. def name_show():
  42. file_name.show()
  43.  
  44. #function that revealsthe file controls
  45. def file_controls_show():
  46. file_controls.show()
  47.  
  48. #function that reveals the text preferences
  49. def text_preferences_show():
  50. text_preferences.show()
  51.  
  52. #function that exits the app
  53. def exit_app():
  54. app.destroy()
  55.  
  56. #Main menubar
  57. menubar = MenuBar(app, toplevel = ["File", "Edit"], options = [[["Open", file_controls_show], ["Save", file_controls_show], ["Exit", exit_app]], [["Font", text_preferences_show], ["Size", text_preferences_show], ["Colour", text_preferences_show]]])
  58.  
  59. # create a box to house the controls, we want the box to span the entire width of the app
  60. file_controls = Box(app, align="top", width="fill", border = True, visible = False)
  61.  
  62. #Create a new box to house text preferences
  63. text_preferences = Box(app, align = "top", width = "fill", border = True, visible = False)
  64. font = Combo(text_preferences, align = "left", options = ["courier", "helvetica", "times new roman", "verdana"], command = change_font)
  65. size = Slider(text_preferences, align = "left", command = change_text_size, start = 10, end = 24)
  66. colour = Combo(text_preferences, align = "left", command = change_color, options = ["black", "red", "blue", "green"])
  67.  
  68. # create an open button which uses the open_file function
  69. open_button = PushButton(file_controls, text="Open", align="left", command = open_file)
  70.  
  71. #create a button to create a new file
  72. new_button = PushButton(file_controls, text = "New", align = "left", command = name_show)
  73.  
  74. # create a TextBox for the file name
  75. file_name = TextBox(file_controls, text="text_file.txt", width=20, align="left", visible = False)
  76.  
  77. # create a save button which uses the save_file function
  78. save_button = PushButton(file_controls, text="Save", align="right", command = save_file)
  79.  
  80. # create a TextBox which is not in the box and fills the rest of the GUI
  81. editor = TextBox(app, multiline=True, height="fill", width="fill", command = save_enable)
  82.  
  83. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement