Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Text editor can be writing, editing and saving fastly.
- from guizero import App, TextBox, PushButton, Box, Combo, Slider, CheckBox
- app = App(title="My Text Editor with editing and saving file")
- # function for reading files
- def open_file():
- with open(file_name.value, "r") as f:
- editor.value = f.read()
- # functioning for saving files
- def save_file():
- with open(file_name.value, "w") as f:
- f.write(editor.value)
- # Disable the save button after saving file
- save_button.visible = False
- # Enable the save button after back to editing
- def enable_save():
- save_button.show()
- # create a preference new font, size and colour of the text, dark theme
- def change_font():
- editor.font = font.value
- def change_text_size():
- editor.text_size = size.value
- editor.resize(1, 1)
- editor.resize("fill", "fill")
- def change_colour():
- editor.text_color = colour.value
- def change_theme():
- if dark_mode.value == 1:
- app.bg = "black"
- app.text_color = "white"
- else:
- app.bg = None
- app.text_color = None
- # create a box to house the controls, we want the box to span the entire width of the app
- file_controls = Box(app, align="top", width="fill")
- # create a TextBox for the file name
- file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
- # create a save button which uses the save_file function
- save_button = PushButton(file_controls, text="Save", command=save_file, align="right", visible=True)
- # create an open button which uses the open_file function
- open_button = PushButton(file_controls, text="Open", command=open_file, align="right", visible=True)
- # create a TextBox which is not in the box and fills the rest of the GUI and editing the new text
- editor = TextBox(app, multiline=True, height="fill", width="fill", command=enable_save)
- # create a box that consist controling the text and theme
- preferences_control = Box(app, layout='auto', align="bottom", width="fill")
- font = Combo(preferences_control,options=["verdana","arial","times new roman"], align="left", command=change_font)
- colour = Combo(preferences_control, options=["red","green","black"], align="left", command=change_colour)
- size = Slider(preferences_control,start=8, end=16, align="left", command=change_text_size)
- dark_mode = CheckBox(preferences_control, text="dark", align="left", command=change_theme)
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement