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="texteditor")
- # 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)
- save_button.disable() # disables the save button after saving
- btn_ltr_cnt.disable() # enables the letter count button
- def enable_save():
- save_button.enable() # enables the save button
- btn_ltr_cnt.enable() # enables the letter count button
- def change_font():
- editor.font = font.value
- def exit_app ():
- app.destroy()
- def change_font_size():
- editor.text_size = txtsize.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_font_colour():
- editor.text_color = txtcol.value
- def count_letters():
- ltr_cnt.value = len(editor.value)-1
- menubar = MenuBar(app, toplevel = ["File","Font","Count"],
- options = [
- [["open", open_file], ["save", save_file], ["exit", exit_app]],
- [["size", change_font_size],["colour", change_font_colour],["style",change_font]],
- [["count", count_letters]]
- ])
- # 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")
- # a padder to pull it away form the edge
- pad = Box(file_controls,align = "left", width=5, height=5)
- # preference controls
- pref_controls = Box(app,align="bottom",width = "fill", border = True)
- font = Combo(pref_controls,options=["courier", "times new roman", "verana","arial"], align="left", command=change_font)
- txtsize = Slider(pref_controls, start = 8, end = 30, command = change_font_size, align = "left")
- txtcol = Combo(pref_controls, options=["green","blue","back","grey"], command = change_font_colour, align = "left")
- ltr_cnt = TextBox(pref_controls, align ="left")
- btn_ltr_cnt = PushButton(pref_controls, command= count_letters, text = "Count", align ="left", enabled = False )
- # create a TextBox for the file name
- file_name = TextBox(file_controls, text="text_file.txt", width="fill", align="left")
- # create a save button which uses the save_file function
- save_button = PushButton(file_controls, text="Save", command = save_file and count_letters, align="right", enabled = False)
- # create an open button which uses the open_file function
- open_button = PushButton(file_controls, text="Open", command = open_file, align="right")
- # create a TextBox which is not in the box and fills the rest of the GUI
- editor = TextBox(app, multiline=True, command= enable_save, height="fill", width="fill")
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement