Advertisement
maroph

text_editor2.py

Dec 8th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.02 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 = '2'
  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. def save_file():
  48.     name = file_name_text_box.value
  49.     if name is None:
  50.         save_as_file()
  51.         return
  52.     if len(name) == 0:
  53.         save_as_file()
  54.         return
  55.  
  56.     try:
  57.         with open(name, "w") as f:
  58.             f.write(editor.value)
  59.         # save_button.enabled = False
  60.         save_button.visible = False
  61.     except Exception as e:
  62.         app.error('Write Error', 'Error in reading file: ' + str(e))
  63.         return
  64.  
  65.  
  66. def save_as_file():
  67.     f = get_folder()
  68.  
  69.     name = app.select_file('Save file', folder=f, filetypes=[["Text files", "*.txt"], ["Markdown files", "*.md"]], save=True)
  70.     if name is None:
  71.         return
  72.     if len(name) == 0:
  73.         return
  74.  
  75.     try:
  76.         with open(name, "w") as f:
  77.             f.write(editor.value)
  78.             # save_button.enabled = False
  79.             save_button.visible = False
  80.     except Exception as e:
  81.         app.error('Write Error', 'Error in reading file: ' + str(e))
  82.         return
  83.  
  84.     file_name_text_box.value = name
  85.  
  86.  
  87. def new_file():
  88.     file_name_text_box.value = ''
  89.     editor.value = ''
  90.     # save_button.enabled = False
  91.     save_button.visible = False
  92.     char_count_text_box.value = '0'
  93.  
  94.  
  95. def change_font():
  96.     editor.font = font.value
  97.  
  98.  
  99. def change_text_color():
  100.     editor.text_color = text_color.value
  101.  
  102.  
  103. def change_text_size():
  104.     editor.text_size = size.value
  105.     editor.resize(1, 1)
  106.     editor.resize("fill", "fill")
  107.  
  108.  
  109. def enable_save():
  110.     if get_text_chars() == 0:
  111.         # save_button.enabled = False
  112.         save_button.visible = False
  113.         return
  114.  
  115.     # save_button.enabled = True
  116.     save_button.visible = True
  117.  
  118.  
  119. def get_text_chars():
  120.     chars = len(editor.value)
  121.     if chars > 0:
  122.         return chars -1
  123.     else:
  124.         return 0
  125.  
  126. def count_text_chars():
  127.     char_count_text_box.value = get_text_chars()
  128.  
  129. def toggle_char_count():
  130.     if char_count_box.visible:
  131.         char_count_box.visible = False
  132.     else:
  133.         char_count_box.visible = True
  134.  
  135. file_controls = Box(app, width='fill', border=True)
  136. file_name_label = Text(file_controls, text='File: ', align="left")
  137. file_name_text_box = TextBox(file_controls, text='', width='fill', align="left", enabled=False)
  138. file_name_text_box.bg = 'white'
  139. about_button = PushButton(file_controls, text="About", align="right", command=about_editor)
  140. toggle_char_count_button = PushButton(file_controls, text="Char count", align="right", command=toggle_char_count)
  141. save_as_button = PushButton(file_controls, text="Save as...", align="right", command=save_as_file)
  142. save_button = PushButton(file_controls, text="Save", align="right", command=save_file)
  143. # save_button.enabled = False
  144. save_button.visible = False
  145. open_button = PushButton(file_controls, text="Open...", align="right", command=open_file)
  146. new_button = PushButton(file_controls, text="New", align="right", command=new_file)
  147.  
  148. preferences_controls = Box(app, align="top", width="fill", border=True)
  149. font = Combo(preferences_controls, options=["Courier", "Times New Roman", "Verdana"], align="left", selected='Verdana', command=change_font)
  150. size = Slider(preferences_controls,  align="left", command=change_text_size, start=10, end=18)
  151. size.value = 10
  152. text_color = Combo(preferences_controls, options=['black', 'blue', 'red', 'magenta', 'yellow'], align="left", selected='black', command=change_text_color)
  153.  
  154. char_count_box = Box(app, width='fill', border=True)
  155. char_count_label = Text(char_count_box, text='Size of text: ', align="left")
  156. char_count_text_box = TextBox(char_count_box, text='', width='20', align="left", enabled=False)
  157. char_count_text_box.value = '0'
  158.  
  159. editor = TextBox(app, multiline=True, height="fill", width="fill", text='', scrollbar=True, command=enable_save)
  160. editor.bg = 'white'
  161. editor.font = font.value
  162. editor.text_size = size.value
  163. editor.text_color = text_color.value
  164. editor.repeat(500, count_text_chars)
  165. editor.focus()
  166.  
  167. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement