Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from guizero import App, TextBox, PushButton, Box, Combo, CheckBox, Slider, MenuBar
- def open_file():
- with open(file_name.value, "r") as f:
- editor.value = f.read()
- def save_file():
- with open(file_name.value, "w") as f:
- f.write(editor.value)
- # Disable the save_button
- save_button.disable()
- # Enable the open_button
- open_button.enable()
- 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_font_colour():
- editor.text_color = font_colour.value
- def change_bg_color(col):
- editor.bg = col
- def enable_save():
- save_button.enable()
- open_button.disable()
- def preference_control_toggle():
- if preference_chkbox.value == True:
- preferences_controls.show()
- else:
- preferences_controls.hide()
- def dark_mode():
- if dark_chkbox.value == True:
- editor.bg = "black"
- editor.text_color = "white"
- dark_chkbox.value = False
- else:
- editor.bg = "white"
- editor.text_color = "black"
- dark_chkbox.value = True
- def exit_app():
- if save_button.enabled == True:
- app.warn("Warning!", "You have not saved the changes")
- else:
- app.destroy()
- app = App(title="textzero")
- # Menu bar
- menubar = MenuBar(app,
- # These are the menu options
- toplevel=["File", "Edit", "Background"],
- # The options are recorded in a nested lists, one list for each menu option
- # Each option is a list containing a name and a function
- options=[[ ["Open file", open_file], ["Save file", save_file], ["Exit",exit_app] ],
- [ ["Change font", change_font], ["Change text size", change_text_size], ["Change font colour", change_font_colour] ],
- [["Orange",lambda: change_bg_color('orange')], ["Yellow",lambda: change_bg_color('yellow')],
- ["magenta",lambda: change_bg_color('magenta')], ["Reset",lambda: change_bg_color('white')]]
- ])
- # One box to house the controls
- file_controls = Box(app, align="bottom", width="fill", border=True)
- preferences_controls = Box(app, align="top", width="fill", border=True, visible=False)
- preference_chkbox = CheckBox(file_controls, text="View options", align="left", command=preference_control_toggle)
- dark_chkbox = CheckBox(file_controls, text="Dark", align="right", command=dark_mode)
- # One slider for font sizes
- size = Slider(preferences_controls, align="left", start = 8, end = 18, command=change_text_size)
- # Font controls
- font = Combo(preferences_controls, options=["Courier", "Century Gothic", "Verdana"], align="left", command=change_font)
- font_colour = Combo(preferences_controls, options=["black", "orange", "purple"], align="right", command=change_font_colour)
- # File name text box
- file_name = TextBox(file_controls, text="text_file.txt", width=24, align="left")
- # Buttons
- save_button = PushButton(file_controls, text="Save", command=save_file, align="right", enabled=False)
- open_button = PushButton(file_controls, text="Open", command=open_file, align="right")
- # TextBox to fill remainder of GUI
- editor = TextBox(app, multiline=True, height="fill", width="fill", command=enable_save)
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement