Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Text Editor V7 save warning message and cgange theme 06/05/2022
- from guizero import App, TextBox, PushButton, Box, Combo, Slider, Text, MenuBar
- # A new function that closes the app
- def exit_app():
- if text.visible==False:
- text.show()
- else:
- app.destroy()
- # function for reading files
- def open_file():
- with open(file_name.value, "r") as f:
- editor.value = f.read()
- # function for writing files
- def save_file():
- with open(file_name.value, "w") as f:
- f.write(editor.value)
- text.hide()
- # functions for changing fonts
- def change_font():
- editor.font = font.value
- def courier_font():
- editor.font = "courier"
- def times_font():
- editor.font = "times new roman"
- def verdana_font():
- editor.font = "verdana"
- def change_text_size():
- editor.text_size = size.value
- editor.resize(1, 1)
- editor.resize("fill", "fill")
- editor.show()
- # function for changing text colour
- def change_font_color():
- choice = font_color.value
- if choice == "Red":
- editor.text_color = "Red"
- if choice == "Green":
- editor.text_color = "Green"
- if choice == "Blue":
- editor.text_color = "Blue"
- if choice == "Reset":
- editor.text_color = None
- # change theme
- def change_theme():
- choice = theme.value
- if choice == "Light":
- editor.text_color = "Black"
- editor.bg = "White"
- if choice == "Dark":
- editor.text_color = "White"
- editor.bg = "Black"
- app = App(title="texteditor")
- menubar = MenuBar(app,
- # These are the menu options
- toplevel=["File", "Font"],
- # The options are file and font style
- options=[
- [ ["open",open_file], ["save",save_file], ["exit",exit_app ] ],
- [ ["courier",courier_font], ["times new roman",times_font], ["verdana",verdana_font ] ]
- ])
- # create a box to house the controls
- file_controls = Box(app, align="top", width="fill", border=True)
- # create a TextBox for the file name
- file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
- # create a TextBox which is not in the box and fills the rest of the GUI
- editor = TextBox(app, multiline=True, height="fill", width="fill")
- # create a box to house the controls at the bottom, add a font combobox and size slider
- preferences_controls = Box(app, align="bottom", width="fill", border=True)
- font = Combo(preferences_controls, options=["courier", "times new roman", "verdana"], align="right", command=change_font)
- size = Slider(preferences_controls, align="right", command=change_text_size, start=8, end=42)
- font_color = Combo(preferences_controls, options=["Red", "Green", "Blue", "Reset"], align="left", command=change_font_color)
- text = Text(preferences_controls, text="Save changes to this file?", visible=False)
- theme = Combo(preferences_controls, align="left", options=["Light","Dark"], command=change_theme)
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment