scipiguy

FutureLearn GUIZero - Text Editor Open Save Buttons

Nov 11th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. from guizero import App, TextBox, PushButton, Box, Combo, Slider
  2.  
  3. # Functions
  4. def open_file():
  5.     with open(file_name.value, "r") as f:
  6.         editor.value = f.read()
  7.     open_button.hide()
  8.  
  9. def save_file():
  10.     with open(file_name.value, "w") as f:
  11.         f.write(editor.value)
  12.     save_button.hide()
  13.    
  14. def enable_save():
  15.     save_button.enable()
  16.     save_button.show()
  17.     enable_open()
  18.    
  19. def enable_open():
  20.     open_button.enable()
  21.     open_button.show()
  22.  
  23. def change_font():
  24.     editor.font = font.value
  25.    
  26. def change_text_size():
  27.     editor.text_size = size.value
  28.     editor.resize(1, 1)
  29.     editor.resize("fill", "fill")
  30.    
  31. def change_font_colour():
  32.     editor.text_color = font_colour.value
  33.  
  34. app = App(title="Text Editor")
  35.  
  36.  
  37. # Lower box and widgets
  38. preferences_controls = Box(app, align="top", width="fill", border=True)
  39. font = Combo(preferences_controls, options=["courier", "stencil", "times new roman", "verdana"], align="left", command=change_font)
  40. size = Slider(preferences_controls, align="left", command=change_text_size, start=10, end=18)
  41. font_colour = Combo(preferences_controls, align="left", command=change_font_colour, options=["black", "blue", "green", "orange", "red", "yellow"])
  42.  
  43. editor = TextBox(app, multiline=True, height="fill", width="fill", command=enable_save)
  44.  
  45. # Top box and widgets
  46. file_controls = Box(app, align="top", width="fill")
  47. file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
  48. save_button =PushButton(file_controls, text="Save", command=save_file, align="right", enabled=False)
  49. open_button = PushButton(file_controls, text = "Open", command=open_file, align="right")
  50.  
  51.  
  52. app.display()
Add Comment
Please, Sign In to add comment