Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from guizero import App, Box, Combo, PushButton, Slider, Text, TextBox
- from pathlib import Path
- app_name = 'Simple Text Editor'
- app_version = '2'
- app_verdate = '08-DEC-2019'
- app_version_string = app_name + ' v' + app_version + ' (' + app_verdate + ')'
- app = App(title=app_version_string, bg='slategray', width=800, height=600)
- folder = '.'
- def get_folder():
- name = file_name_text_box.value
- if name is None:
- return '.'
- elif len(name) == 0:
- return '.'
- else:
- return Path(name).parent
- def about_editor():
- app.info('About', app_version_string + "\nA simple Python guizero based text editor")
- return
- def open_file():
- f = get_folder()
- name = app.select_file('Open file', folder=f, filetypes=[["Text files", "*.txt"], ["Markdown files", "*.md"]], save=False)
- if name is None:
- return
- if len(name) == 0:
- return
- try:
- with open(name, "r") as f:
- editor.value = f.read()
- except Exception as e:
- app.error('Read Error', 'Error in reading file: ' + str(e))
- return
- file_name_text_box.value= name
- def save_file():
- name = file_name_text_box.value
- if name is None:
- save_as_file()
- return
- if len(name) == 0:
- save_as_file()
- return
- try:
- with open(name, "w") as f:
- f.write(editor.value)
- # save_button.enabled = False
- save_button.visible = False
- except Exception as e:
- app.error('Write Error', 'Error in reading file: ' + str(e))
- return
- def save_as_file():
- f = get_folder()
- name = app.select_file('Save file', folder=f, filetypes=[["Text files", "*.txt"], ["Markdown files", "*.md"]], save=True)
- if name is None:
- return
- if len(name) == 0:
- return
- try:
- with open(name, "w") as f:
- f.write(editor.value)
- # save_button.enabled = False
- save_button.visible = False
- except Exception as e:
- app.error('Write Error', 'Error in reading file: ' + str(e))
- return
- file_name_text_box.value = name
- def new_file():
- file_name_text_box.value = ''
- editor.value = ''
- # save_button.enabled = False
- save_button.visible = False
- char_count_text_box.value = '0'
- def change_font():
- editor.font = font.value
- def change_text_color():
- editor.text_color = text_color.value
- def change_text_size():
- editor.text_size = size.value
- editor.resize(1, 1)
- editor.resize("fill", "fill")
- def enable_save():
- if get_text_chars() == 0:
- # save_button.enabled = False
- save_button.visible = False
- return
- # save_button.enabled = True
- save_button.visible = True
- def get_text_chars():
- chars = len(editor.value)
- if chars > 0:
- return chars -1
- else:
- return 0
- def count_text_chars():
- char_count_text_box.value = get_text_chars()
- def toggle_char_count():
- if char_count_box.visible:
- char_count_box.visible = False
- else:
- char_count_box.visible = True
- file_controls = Box(app, width='fill', border=True)
- file_name_label = Text(file_controls, text='File: ', align="left")
- file_name_text_box = TextBox(file_controls, text='', width='fill', align="left", enabled=False)
- file_name_text_box.bg = 'white'
- about_button = PushButton(file_controls, text="About", align="right", command=about_editor)
- toggle_char_count_button = PushButton(file_controls, text="Char count", align="right", command=toggle_char_count)
- save_as_button = PushButton(file_controls, text="Save as...", align="right", command=save_as_file)
- save_button = PushButton(file_controls, text="Save", align="right", command=save_file)
- # save_button.enabled = False
- save_button.visible = False
- open_button = PushButton(file_controls, text="Open...", align="right", command=open_file)
- new_button = PushButton(file_controls, text="New", align="right", command=new_file)
- preferences_controls = Box(app, align="top", width="fill", border=True)
- font = Combo(preferences_controls, options=["Courier", "Times New Roman", "Verdana"], align="left", selected='Verdana', command=change_font)
- size = Slider(preferences_controls, align="left", command=change_text_size, start=10, end=18)
- size.value = 10
- text_color = Combo(preferences_controls, options=['black', 'blue', 'red', 'magenta', 'yellow'], align="left", selected='black', command=change_text_color)
- char_count_box = Box(app, width='fill', border=True)
- char_count_label = Text(char_count_box, text='Size of text: ', align="left")
- char_count_text_box = TextBox(char_count_box, text='', width='20', align="left", enabled=False)
- char_count_text_box.value = '0'
- editor = TextBox(app, multiline=True, height="fill", width="fill", text='', scrollbar=True, command=enable_save)
- editor.bg = 'white'
- editor.font = font.value
- editor.text_size = size.value
- editor.text_color = text_color.value
- editor.repeat(500, count_text_chars)
- editor.focus()
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement