JAWarr

Text Editor enable widgets V5 visible widgets

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