Advertisement
brendan-stanford

Texteditor-GUI-hide+show

Oct 11th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 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. #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. def name_show():
  41. file_name.show()
  42.  
  43. # create a box to house the controls, we want the box to span the entire width of the app
  44. file_controls = Box(app, align="top", width="fill", border = True)
  45.  
  46. #Create a new box to house text preferences
  47. text_preferences = Box(app, align = "top", width = "fill", border = True)
  48. font = Combo(text_preferences, align = "left", options = ["courier", "helvetica", "times new roman", "verdana"], command = change_font)
  49. size = Slider(text_preferences, align = "left", command = change_text_size, start = 10, end = 24)
  50. colour = Combo(text_preferences, align = "left", command = change_color, options = ["black", "red", "blue", "green"])
  51.  
  52. # create an open button which uses the open_file function
  53. open_button = PushButton(file_controls, text="Open", align="left", command = open_file)
  54.  
  55. #create a button to create a new file
  56. new_button = PushButton(file_controls, text = "New", align = "left", command = name_show)
  57.  
  58. # create a TextBox for the file name
  59. file_name = TextBox(file_controls, text="text_file.txt", width=20, align="left", visible = False)
  60.  
  61. # create a save button which uses the save_file function
  62. save_button = PushButton(file_controls, text="Save", align="right", command = save_file)
  63.  
  64. # create a TextBox which is not in the box and fills the rest of the GUI
  65. editor = TextBox(app, multiline=True, height="fill", width="fill", command = save_enable)
  66.  
  67. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement