Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from guizero import App, TextBox, PushButton, Box, Combo, Slider, MenuBar, Text, yesno, CheckBox
- # function for reading files
- def open_file():
- if (lbl_needSave.visible):
- answer = yesno("Warning!!!","The editor has unsaved changes; open the other file anyway?")
- if (answer):
- open_file_confirmed()
- else:
- open_file_confirmed()
- def open_file_confirmed():
- with open(file_name.value, "r") as f:
- editor.value = f.read()
- # Disable the save_button
- save_button.disable()
- #save_button.hide()
- lbl_needSave.visible = False
- # function for writing files
- def save_file():
- with open(file_name.value, "w") as f:
- f.write(editor.value)
- # Disable the save_button
- save_button.disable()
- #save_button.hide()
- lbl_needSave.visible = False
- # A new function that closes the app
- def exit_app():
- if (lbl_needSave.visible):
- answer = yesno("Warning!!!","The editor has unsaved changes; exit anyway?")
- if (answer):
- exit_app_confirmed()
- else:
- exit_app_confirmed()
- def exit_app_confirmed():
- app.destroy()
- def enable_save():
- save_button.enable()
- #save_button.show()
- lbl_needSave.visible = True
- def change_font():
- print(editor.font)
- editor.font = font.value
- def change_text_size():
- editor.text_size = size.value
- # resize the widget because if the text is made bigger, this might affect the size of the TextBox so guizero needs to know how to maintain the intended layout
- editor.resize(1, 1)
- editor.resize("fill", "fill")
- def change_text_color():
- editor.text_color = text_col.value
- def change_bg_color(col):
- editor.bg = col
- def toggle_mode():
- app.bg = "black" if checkbox_mode.value == 1 else "light grey"
- app.text_color = "white" if checkbox_mode.value == 1 else "black"
- text_col.value = app.text_color
- app = App(title="texteditor")
- # The new MenuBar
- menubar = MenuBar(app,
- toplevel=["File", "Background color"],
- options=[
- [["open",open_file], ["save",save_file], ["exit",exit_app]],
- [["red",lambda: change_bg_color('red')], ["blue",lambda: change_bg_color('blue')], ["cyan",lambda: change_bg_color('cyan')], ["magenta",lambda: change_bg_color('magenta')]]
- ])
- # 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="10_text_file.txt", width=50, align="left")
- # label that contains an asterisk, used to show wheter the save is needed or not
- lbl_needSave = Text(file_controls, text="*", size=16, 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, enabled=False)
- # create an open button which uses the open_file function
- open_button = PushButton(file_controls, text="Open", align="right", command=open_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=enable_save)
- preferences_controls = Box(app, align="bottom", width="fill", border=True)
- font = Combo(preferences_controls, options=["courier new", "times new roman", "verdana"], align="left", command=change_font)
- size = Slider(preferences_controls, align="left", command=change_text_size, start=10, end=60)
- text_col = Combo(preferences_controls, options=["black", "blue", "green", "gray", "magenta", "white"], align="left", command=change_text_color)
- checkbox_mode = CheckBox(preferences_controls, text="Dark mode", align="right", command=toggle_mode)
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement