Advertisement
JAWarr

Text Editor enable widgets V4

May 5th, 2022
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. ### Text Editor V4 enable widgets 05/05/2022
  2. from guizero import App, TextBox, PushButton, Box, Combo, Slider
  3.  
  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. def enable_save():
  10.     save_button.enable()
  11.  
  12. def disable_save():
  13.     save_button.disable()
  14.  
  15. # function for writing files
  16. def save_file():
  17.     with open(file_name.value, "w") as f:
  18.         f.write(editor.value)
  19.     save_button.disable()
  20.  
  21. # function for changing fonts
  22. def change_font():
  23.     editor.font = font.value
  24.  
  25. def change_text_size():
  26.     editor.text_size = size.value
  27.     '''
  28.    resize the widget because if the text is made bigger,
  29.    this might affect the size of the TextBox so guizero
  30.    needs to know how to maintain the intended layout
  31.    '''
  32.     editor.resize(1, 1)
  33.     editor.resize("fill", "fill")
  34.  
  35. # function for changing text colour
  36. def change_font_color():
  37.     choice = font_color.value
  38.     if choice == "Red":
  39.         editor.text_color = "Red"
  40.     if choice == "Green":
  41.         editor.text_color = "Green"
  42.     if choice == "Blue":
  43.         editor.text_color = "Blue"
  44.     if choice == "None":
  45.         editor.text_color = None
  46.    
  47.  
  48. app = App(title="texteditor")
  49.  
  50. # create a box to house the controls, we want the box to span the entire width of the app
  51. file_controls = Box(app, align="top", width="fill")
  52.  
  53. # create a TextBox for the file name
  54. file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
  55.  
  56. # create a save button which uses the save_file function
  57. save_button = PushButton(file_controls, text="Save", command=save_file, align="right", enabled=False)
  58.  
  59. # create an open button which uses the open_file function
  60. open_button = PushButton(file_controls, text="Open", command=open_file, align="right")
  61.  
  62. # create a TextBox which is not in the box and fills the rest of the GUI
  63. editor = TextBox(app, multiline=True, height="fill", width="fill", command=enable_save)
  64.  
  65. # create a box to house the controls at the bottom, add a font combobox and size slider
  66. preferences_controls = Box(app, align="bottom", width="fill", border=True)
  67.  
  68. font = Combo(preferences_controls, options=["courier", "times new roman", "verdana"], align="left", command=change_font)
  69. size = Slider(preferences_controls,  align="right", command=change_text_size, start=8, end=42)
  70. font_color = Combo(preferences_controls, options=["Red", "Green", "Blue", "Reset"], align="bottom", command=change_font_color)
  71.  
  72. app.display()
  73.  
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement