maroph

text_editor.py

Dec 8th, 2019
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. from guizero import App, Box, Combo, PushButton, Slider, Text, TextBox
  2. from pathlib import Path
  3.  
  4. app_name = 'Simple Text Editor'
  5. app_version = '1'
  6. app_verdate = '08-DEC-2019'
  7. app_version_string = app_name + ' v' + app_version + ' (' + app_verdate + ')'
  8.  
  9. app = App(title=app_version_string, bg='slategray', width=800, height=600)
  10.  
  11. folder = '.'
  12.  
  13.  
  14. def get_folder():
  15.     name = file_name_text_box.value
  16.     if name is None:
  17.         return '.'
  18.     elif len(name) == 0:
  19.         return '.'
  20.     else:
  21.         return Path(name).parent
  22.  
  23.  
  24. def about_editor():
  25.     app.info('About', app_version_string + "\nA simple Python guizero based text editor")
  26.     return
  27.  
  28.  
  29. def open_file():
  30.     f = get_folder()
  31.     name = app.select_file('Open file', folder=f, filetypes=[["Text files", "*.txt"], ["Markdown files", "*.md"]], save=False)
  32.     if name is None:
  33.         return
  34.     if len(name) == 0:
  35.         return
  36.  
  37.     try:
  38.         with open(name, "r") as f:
  39.             editor.value = f.read()
  40.     except Exception as e:
  41.         app.error('Read Error', 'Error in reading file: ' + str(e))
  42.         return
  43.  
  44.     file_name_text_box.value= name
  45.  
  46.  
  47. # function for writing files
  48. def save_file():
  49.     name = file_name_text_box.value
  50.     if name is None:
  51.         save_as_file()
  52.         return
  53.     if len(name) == 0:
  54.         save_as_file()
  55.         return
  56.  
  57.     try:
  58.         with open(name, "w") as f:
  59.             f.write(editor.value)
  60.     except Exception as e:
  61.         app.error('Write Error', 'Error in reading file: ' + str(e))
  62.         return
  63.  
  64.  
  65. def save_as_file():
  66.     f = get_folder()
  67.  
  68.     name = app.select_file('Save file', folder=f, filetypes=[["Text files", "*.txt"], ["Markdown files", "*.md"]], save=True)
  69.     if name is None:
  70.         return
  71.     if len(name) == 0:
  72.         return
  73.  
  74.     try:
  75.         with open(name, "w") as f:
  76.             f.write(editor.value)
  77.     except Exception as e:
  78.         app.error('Write Error', 'Error in reading file: ' + str(e))
  79.         return
  80.  
  81.     file_name_text_box.value = name
  82.  
  83.  
  84. def new_file():
  85.     file_name_text_box.value = ''
  86.     editor.value = ''
  87.  
  88.  
  89. def change_font():
  90.     editor.font = font.value
  91.  
  92.  
  93. def change_text_color():
  94.     editor.text_color = text_color.value
  95.  
  96.  
  97. def change_text_size():
  98.     editor.text_size = size.value
  99.     editor.resize(1, 1)
  100.     editor.resize("fill", "fill")
  101.  
  102.  
  103. file_controls = Box(app, width='fill')
  104. file_name_label = Text(file_controls, text='File: ', align="left")
  105. file_name_text_box = TextBox(file_controls, text='', width='fill', align="left", enabled=False)
  106. file_name_text_box.bg = 'white'
  107. about_button = PushButton(file_controls, text="About", align="right", command=about_editor)
  108. save_as_button = PushButton(file_controls, text="Save as...", align="right", command=save_as_file)
  109. save_button = PushButton(file_controls, text="Save", align="right", command=save_file)
  110. open_button = PushButton(file_controls, text="Open...", align="right", command=open_file)
  111. new_button = PushButton(file_controls, text="New", align="right", command=new_file)
  112.  
  113. preferences_controls = Box(app, align="top", width="fill", border=True)
  114. font = Combo(preferences_controls, options=["Courier", "Times New Roman", "Verdana"], align="left", selected='Verdana', command=change_font)
  115. size = Slider(preferences_controls,  align="left", command=change_text_size, start=10, end=18)
  116. size.value = 10
  117. text_color = Combo(preferences_controls, options=['black', 'blue', 'red', 'magenta', 'yellow'], align="left", selected='black', command=change_text_color)
  118.  
  119. editor = TextBox(app, multiline=True, height="fill", width="fill", scrollbar=True)
  120. editor.bg = 'white'
  121. editor.font = font.value
  122. editor.text_size = size.value
  123. editor.text_color = text_color.value
  124.  
  125. app.display()
Advertisement
Add Comment
Please, Sign In to add comment