scipiguy

FutureLearn GUIZero - Menus

Nov 11th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. from guizero import App, TextBox, PushButton, Box, Combo, Slider, MenuBar
  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. def change_font_courier():
  35.     editor.font = "courier"
  36.    
  37. def change_font_stencil():
  38.     editor.font = "stencil"
  39.    
  40. def change_font_times():
  41.     editor.font = "times new roman"
  42.    
  43. def change_font_verdana():
  44.     editor.font = "verdana"
  45.  
  46. def exit_app():
  47.     app.destroy()
  48.  
  49. app = App(title="Text Editor")
  50.  
  51. menubar = MenuBar(app,
  52.                   toplevel=["File", "Font"],
  53.                   options=[[["open",open_file],["save",save_file],["exit",exit_app]],
  54.                             [["courier",change_font_courier],["stencil",change_font_stencil],["times new roman",change_font_times],["verdana",change_font_verdana]]])
  55.  
  56.  
  57. # Lower box and widgets
  58. preferences_controls = Box(app, align="top", width="fill", border=True)
  59. font = Combo(preferences_controls, options=["courier", "stencil", "times new roman", "verdana"], align="left", command=change_font)
  60. size = Slider(preferences_controls, align="left", command=change_text_size, start=10, end=18)
  61. font_colour = Combo(preferences_controls, align="left", command=change_font_colour, options=["black", "blue", "green", "orange", "red", "yellow"])
  62.  
  63. editor = TextBox(app, multiline=True, height="fill", width="fill", command=enable_save)
  64.  
  65. # Top box and widgets
  66. file_controls = Box(app, align="top", width="fill")
  67. file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
  68. save_button =PushButton(file_controls, text="Save", command=save_file, align="right", enabled=False)
  69. open_button = PushButton(file_controls, text = "Open", command=open_file, align="right")
  70.  
  71.  
  72. app.display()
Add Comment
Please, Sign In to add comment