JAWarr

Text Editor V6 Menus

May 6th, 2022
1,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 KB | None | 0 0
  1. ### Text Editor V6 drop down menu 06/05/2022
  2. from guizero import App, TextBox, PushButton, Box, Combo, Slider, Text, MenuBar
  3.  
  4. def open_file():
  5.     with open(file_name.value, "r") as f:
  6.         editor.value = f.read()
  7.  
  8. def save_file():
  9.     with open(file_name.value, "w") as f:
  10.         f.write(editor.value)
  11.  
  12. # A new function that closes the app
  13. def exit_app():
  14.     app.destroy()
  15.    
  16. # function for reading files
  17. def open_file():
  18.     with open(file_name.value, "r") as f:
  19.         editor.value = f.read()
  20.  
  21. # function for writing files
  22. def save_file():
  23.     with open(file_name.value, "w") as f:
  24.         f.write(editor.value)
  25.  
  26. # function for changing fonts
  27. def change_font():
  28.     editor.font = font.value
  29.  
  30. def courier_font():
  31.     editor.font = "courier"
  32. def times_font():
  33.     editor.font = "times new roman"
  34. def verdana_font():
  35.     editor.font = "verdana"
  36.  
  37.  
  38. def change_text_size():
  39.     editor.text_size = size.value
  40.     '''
  41.    resize the widget because if the text is made bigger,
  42.    this might affect the size of the TextBox so guizero
  43.    needs to know how to maintain the intended layout
  44.    '''
  45.     editor.resize(1, 1)
  46.     editor.resize("fill", "fill")
  47.     editor.show()
  48.    
  49. # function for changing text colour
  50. def change_font_color():
  51.     choice = font_color.value
  52.     if choice == "Red":
  53.         editor.text_color = "Red"
  54.     if choice == "Green":
  55.         editor.text_color = "Green"
  56.     if choice == "Blue":
  57.         editor.text_color = "Blue"
  58.     if choice == "Reset":
  59.         editor.text_color = None
  60.  
  61. app = App(title="texteditor")
  62.  
  63. menubar = MenuBar(app,
  64.                   # These are the menu options
  65.                   toplevel=["File", "Font"],
  66.                   # The options are file and font style
  67.                   options=[
  68.                       [ ["open",open_file], ["save",save_file], ["exit",exit_app ] ],
  69.                       [ ["courier",courier_font], ["times new roman",times_font], ["verdana",verdana_font ] ]
  70.                   ])
  71.  
  72. # create a box to house the controls
  73. file_controls = Box(app, align="top", width="fill", border=True)
  74.  
  75. # create a TextBox for the file name
  76. file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
  77.  
  78. # create a TextBox which is not in the box and fills the rest of the GUI
  79. editor = TextBox(app, multiline=True, height="fill", width="fill")
  80.  
  81. # create a box to house the controls at the bottom, add a font combobox and size slider
  82. preferences_controls = Box(app, align="bottom", width="fill", border=True)
  83.  
  84. font = Combo(preferences_controls, options=["courier", "times new roman", "verdana"], align="left", command=change_font)
  85. size = Slider(preferences_controls,  align="right", command=change_text_size, start=8, end=42, visible=True)
  86. font_color = Combo(preferences_controls, options=["Red", "Green", "Blue", "Reset"], align="bottom", command=change_font_color)
  87. text = Text(preferences_controls, text="Saved", visible=False)
  88.  
  89. app.display()
  90.  
Advertisement
Add Comment
Please, Sign In to add comment