JAWarr

Text Editor with theme and save warning V7

May 6th, 2022
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. ### Text Editor V7 save warning message and cgange theme 06/05/2022
  2. from guizero import App, TextBox, PushButton, Box, Combo, Slider, Text, MenuBar
  3.  
  4. # A new function that closes the app
  5. def exit_app():
  6.     if text.visible==False:
  7.         text.show()
  8.     else:
  9.         app.destroy()
  10.    
  11. # function for reading files
  12. def open_file():
  13.     with open(file_name.value, "r") as f:
  14.         editor.value = f.read()
  15.  
  16. # function for writing files
  17. def save_file():
  18.     with open(file_name.value, "w") as f:
  19.         f.write(editor.value)
  20.         text.hide()
  21.  
  22. # functions for changing fonts
  23. def change_font():
  24.     editor.font = font.value
  25. def courier_font():
  26.     editor.font = "courier"
  27. def times_font():
  28.     editor.font = "times new roman"
  29. def verdana_font():
  30.     editor.font = "verdana"
  31.  
  32. def change_text_size():
  33.     editor.text_size = size.value
  34.     editor.resize(1, 1)
  35.     editor.resize("fill", "fill")
  36.     editor.show()
  37.    
  38. # function for changing text colour
  39. def change_font_color():
  40.     choice = font_color.value
  41.     if choice == "Red":
  42.         editor.text_color = "Red"
  43.     if choice == "Green":
  44.         editor.text_color = "Green"
  45.     if choice == "Blue":
  46.         editor.text_color = "Blue"
  47.     if choice == "Reset":
  48.         editor.text_color = None
  49.  
  50. # change theme
  51. def change_theme():
  52.     choice = theme.value
  53.     if choice == "Light":
  54.         editor.text_color = "Black"
  55.         editor.bg = "White"
  56.     if choice == "Dark":
  57.         editor.text_color = "White"
  58.         editor.bg = "Black"
  59.  
  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="right", command=change_font)
  85. size = Slider(preferences_controls,  align="right", command=change_text_size, start=8, end=42)
  86. font_color = Combo(preferences_controls, options=["Red", "Green", "Blue", "Reset"], align="left", command=change_font_color)
  87. text = Text(preferences_controls, text="Save changes to this file?", visible=False)
  88. theme = Combo(preferences_controls, align="left", options=["Light","Dark"], command=change_theme)
  89.  
  90. app.display()
  91.  
Advertisement
Add Comment
Please, Sign In to add comment