Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #setup libraries
- from guizero import App, TextBox, PushButton, Box, Combo, Slider, MenuBar, Text, CheckBox
- #setup app
- app = App(title="text editor")
- # function for reading files
- def open_file():
- with open(file_name.value, "r") as f:
- editor.value = f.read()
- #disable save button till edited
- save_button.hide()
- file_name.show()
- # function for writing files
- def save_file():
- with open(file_name.value, "w") as f:
- f.write(editor.value)
- #disable save button till edited
- save_button.hide()
- file_name.show()
- # function for changing font style
- def change_font():
- editor.font = font.value
- #function for changing text size
- def change_text_size():
- editor.text_size = size.value
- #reset box size to adjust to new text size
- editor.resize(1,1)
- editor.resize("fill", "fill")
- #function for changing text colour
- def change_color():
- editor.text_color = colour.value
- #function to re-anable save button
- def save_enable():
- save_button.show()
- #function that reveals file name after initializing as hidden
- def name_show():
- file_name.show()
- #function that revealsthe file controls
- def file_controls_show():
- file_controls.show()
- #function that reveals the text preferences
- def text_preferences_show():
- text_preferences.show()
- #show editor after hiding for popup
- def editor_show():
- editor.show()
- popup.hide()
- #function that exits the app
- def exit_app():
- popup.show()
- editor.hide()
- #save and exit function
- def save_exit():
- with open(file_name.value, "w") as f:
- f.write(editor.value)
- app.destroy()
- #exit without saving
- def final_exit():
- app.destroy()
- #dark mode option
- def dark_mode():
- app.bg = "black"
- file_controls.text_color = "white"
- text_preferences.text_color = "white"
- editor.text_color = "white"
- popup.text_color = "white"
- #Main menubar
- menubar = MenuBar(app, toplevel = ["File", "Edit"], options = [[["Open", file_controls_show], ["Save", save_file], ["Exit", exit_app]], [["Font", text_preferences_show], ["Size", text_preferences_show], ["Colour", text_preferences_show]]])
- # 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", border = True)
- #Create a new box to house text preferences
- text_preferences = Box(app, align = "top", width = "fill", border = True, visible = False)
- font = Combo(text_preferences, align = "left", options = ["courier", "helvetica", "times new roman", "verdana"], command = change_font)
- size = Slider(text_preferences, align = "left", command = change_text_size, start = 10, end = 24)
- colour = Combo(text_preferences, align = "left", command = change_color, options = ["black", "red", "blue", "green"])
- # create an open button which uses the open_file function
- open_button = PushButton(file_controls, text="Open", align="left", command = open_file)
- #create a button to create a new file
- new_button = PushButton(file_controls, text = "New", align = "left", command = name_show)
- # create a TextBox for the file name
- file_name = TextBox(file_controls, text="text_file.txt", width=20, align="left")
- # create a save button which uses the save_file function
- save_button = PushButton(file_controls, text="Save", align="right", command = save_file)
- # 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", command = save_enable)
- #warning popup to prompt user to save
- popup = Box(app, align = "top", height = 100, width = 500, border = True, visible = False)
- warn = Text(popup, text = "Save file before exit?")
- save_and_exit = PushButton(popup, text = "Save and Exit", align = "top", command = save_exit)
- last_exit = PushButton(popup, text = "Exit Without Saving", align = "top", command = final_exit)
- #dark mode
- dark_box = Box(file_controls, align = "right", border = True)
- txt_drk = Text(dark_box, text = "Enable dark mode?", font = "Times", size = 10)
- chk_drk = CheckBox(dark_box, text = "Turn on dark mode!", command = dark_mode)
- #start app
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement