Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from guizero import App, TextBox, PushButton, Box, Combo, Slider, MenuBar
- 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()
- #function that exits the app
- def exit_app():
- app.destroy()
- #Main menubar
- menubar = MenuBar(app, toplevel = ["File", "Edit"], options = [[["Open", file_controls_show], ["Save", file_controls_show], ["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, visible = False)
- #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", visible = False)
- # 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)
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement