Advertisement
Mori007

mynewtexteditor

Feb 9th, 2021
1,222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. from guizero import App, TextBox, PushButton, Box, Combo, Slider, CheckBox
  2.  
  3. app = App(title="My Text Editor")
  4.  
  5. # function for reading files
  6. def open_file():
  7.     with open(file_name.value, "r") as f:
  8.         editor.value = f.read()
  9.  
  10. # functioning for saving files
  11. def save_file():
  12.     with open(file_name.value, "w") as f:
  13.         f.write(editor.value)
  14.  
  15. # create a preference new font, size and colour of the text, dark theme
  16. def change_font():
  17.     editor.font = font.value
  18.  
  19. def change_text_size():
  20.     editor.text_size = size.value
  21.     editor.resize(1, 1)
  22.     editor.resize("fill", "fill")
  23.  
  24. def change_colour():
  25.     editor.text_color = colour.value
  26.    
  27. def change_theme():
  28.     if dark_mode.value == 1:
  29.         app.bg = "black"
  30.         app.text_color = "white"
  31.     else:
  32.         app.bg = None
  33.         app.text_color = None
  34.  
  35.  
  36. # create a box to house the controls, we want the box to span the entire width of the app
  37. file_controls = Box(app, align="top", width="fill")
  38.  
  39. # create a TextBox for the file name
  40. file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
  41.  
  42. # create a save button which uses the save_file function
  43. save_button = PushButton(file_controls, text="Save",  command=save_file, align="right")
  44.  
  45. # create an open button which uses the open_file function
  46. open_button = PushButton(file_controls, text="Open",  command=open_file, align="right")
  47.  
  48. # create a TextBox which is not in the box and fills the rest of the GUI
  49. editor = TextBox(app, multiline=True, height="fill", width="fill", command=change_colour)
  50.  
  51. # create a box that consist controling the text and theme
  52. preferences_control = Box(app, layout='auto', align="bottom", width="fill")
  53. font = Combo(preferences_control,options=["verdana","arial","times new roman"], align="left", command=change_font)
  54. colour = Combo(preferences_control, options=["red","green","black"], align="left", command=change_colour)
  55. size = Slider(preferences_control,start=8, end=16, align="left", command=change_text_size)
  56. dark_mode = CheckBox(preferences_control, text="dark", align="left", command=change_theme)
  57.  
  58. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement