Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from guizero import*
- app = App(title="texteditor")
- font_tools = Window(app, visible = False)
- align_window = Window(app, visible = False)
- # A new function that closes the app
- def exit_app():
- app.info("Alert!", "Have you saved your work")
- app.destroy()
- # save as function
- def save_as():
- name = app.question("Save as...", "What would you like to call your file?")
- # If cancel is pressed, None is returned
- # so check a name of file was entered
- if name is not None:
- file_name.value = name
- with open(file_name.value, "w") as f:
- f.write(editor.value)
- # 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)
- #disable the save button
- save_button.disable()
- # enabling buttons
- def enable_save():
- save_button.enable()
- save_as_button.enable()
- #change font function
- def change_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")
- #change colour function
- def change_color(value):
- editor.text_color = lb_color.value
- #controls visable/invisable
- def controls_visible_toggle():
- if controls_ckb.value == True:
- file_controls.show()
- else:
- file_controls.hide()
- def show():
- file_controls.visible = True
- #show new font options in menu bar
- def show_font_tools():
- change_font.value = font.value
- size.value = size.value
- change_color.value = lb_color.value
- font_tools.show(wait = True)
- def change_alignment():
- if bgp_alignment.value == "right":
- right = Text(app, editor, align = "right")
- elif bgp_alignment.value == "left":
- text.align = "left"
- elif bgp_alignment.value == "top":
- editor.align = "top"
- elif bgp_alignment.value == "bottom":
- editor.align = "bottom"
- align_window.hide()
- save_as_button.enable()
- def show_align_window():
- align_window.show(wait = True)
- def darkmode():
- editor.darkmode = darkmode_button.value
- editor.bg = "black"
- editor.text_color = "white"
- editor.tk.config(insertbackground = "White")
- def lightmode():
- editor.lightmode = lightmode_button.value
- editor.tk.config(insertbackground = "black")
- editor.bg = "white"
- editor.text_color = "black"
- def clear_text():
- editor.value = ""
- def commit_tools_window():
- editor.change_font = font.value
- editor.change_text_size = size.value
- editor.change_color = lb_color.value
- editor.resize(1, 1)
- editor.resize("fill", "fill")
- save_button.enable()
- font_tools.hide()
- def get_help():
- app.info("Help!", "PLease use this link: https://lawsie.github.io/guizero/ ")
- # The new MenuBar
- menubar = MenuBar(app,
- toplevel=["File", "Edit", "Show", "Help"],
- options=[
- [["Open",open_file],
- ["Save",save_file],
- ["Save as", save_as],
- ["Exit",exit_app]],
- [["Format", show_font_tools],
- ["Align", show_align_window]],
- [["Show",show],
- ["dark", darkmode],
- ["light", lightmode]],
- [["Help", get_help]]
- ])
- file_controls = Box(app, align="top", width="fill", border=True)
- file_name = TextBox(file_controls, text="text_file.txt", width = 25, align="left")
- #preferences_controls = Box(app, align="top", height = 50, width = "fill", border=True)
- #controls visable toggle
- controls_ckb = CheckBox(file_controls, text="Show controls", align = "right", command = controls_visible_toggle)
- save_button = PushButton(file_controls, text="Save", command = save_file, align="right")
- open_button = PushButton(file_controls, text="Open", command = open_file, align="right")
- save_as_button = PushButton(file_controls, text="Save as", command = save_as, align="right")
- change_align_button = PushButton(align_window, text = "change alignment", command = change_alignment)
- editor = TextBox(app, multiline = True, height = "fill", width = "fill", command = enable_save)
- darkmode_button = PushButton(app, text = "Darkmode", command = darkmode, align="right")
- lightmode_button = PushButton(app, text ="lightmode", command = lightmode, align="right")
- #font drop-down
- fontchosen = TextBox (font_tools, text = "Choose Font", width = 20, align = "top")
- font = Combo(font_tools, options=["courier", "times new roman", "verdana", "comic sans", "calibra", "Arial Black" ], width = 25, align="left", command = change_font)
- #slider to control font size
- size = Slider(font_tools, align="left", command=change_text_size, start=8, end=22)
- #change colour listbox
- lb_color = ListBox(
- font_tools,
- items = ["red", "green", "blue", "yellow", "purple", "turquoise", "pink", "orange", "black", "brown", "cyan"],
- selected = "black",
- align = "right",
- command = change_color,
- scrollbar = True)
- #alignment tab
- message1 = Text(align_window, text = "Which alignment would you like?")
- bgp_alignment = ButtonGroup(align_window, options =["Top", "Bottom", "Left", "Right"], selected="none")
- clear_button = PushButton(file_controls, text = "Clear Text", align="left", command = clear_text, enabled = True)
- tools_btn_done = PushButton(font_tools, align = "bottom", text="Done", command = commit_tools_window)
- save_button.disable()
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement