Advertisement
bill_z

MyTextEditor.py

Aug 21st, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. from guizero import App, TextBox, Slider, Box, Text, ButtonGroup, PushButton, Combo, CheckBox, ListBox, Picture
  2.  
  3. def open_file():
  4. with open (file_name.value, "r") as f:
  5. editor.value = f.read()
  6. def save_file():
  7. with open (file_name.value, "w") as f:
  8. f.write (editor.value)
  9. # Disable the save_button
  10. save_button.disable()
  11.  
  12. def enable_save():
  13. save_button.enable()
  14. save_button.show()
  15.  
  16.  
  17. def change_text_font():
  18. editor.font = font_list.value[font_select]
  19.  
  20. def change_text_color():
  21. editor.color = "red"#color_list[1]#color_select.value]
  22.  
  23. def change_text_size():
  24. editor.text_size = size.value
  25. # 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
  26. editor.resize(1, 1)
  27. editor.resize("fill", "fill")
  28.  
  29. app = App(title="texteditor")
  30.  
  31. font_list = ['times new roman','helvetica','gothic','mincho']
  32. color_list = ['black','red','tan','blue','green','cyan','yellow','megenta','gray']
  33.  
  34. # create a box to house the controls, we want the box to span the entire width of the app
  35. file_controls = Box(app, align="top", width="fill")
  36.  
  37. # create a TextBox for the file name
  38. file_name = TextBox(file_controls, text="text_file.txt", width=28, align="left")
  39. # create a save button which uses the save_file function
  40. save_button = PushButton(file_controls, text="Save", align="right", height= 1)
  41. save_button.command=save_file
  42. save_button.disable()
  43. save_button.hide()
  44.  
  45. # create an open button which uses the open_file function
  46. open_button = PushButton(file_controls, text="Open", align="right", height= 1)
  47. open_button.command=open_file
  48.  
  49. #
  50. # create a TextBox which is not in the box and fills the rest of the GUI
  51. editor = TextBox(app, multiline=True, height="fill", width="fill",command=enable_save)
  52.  
  53.  
  54. preferences_controls = Box(app, align="bottom", width="fill", border=True)
  55. #create a font combo
  56. # Times New Roman 12
  57. # helvetica 12
  58. # gothic 13
  59. # mincho 13
  60. font_select = Combo(preferences_controls, align="right",width="fill", height= 1, options=font_list, selected=font_list[0])
  61. font_select.command=change_text_font
  62. color_select = Combo(preferences_controls, align="right",width="fill", height= 1, options=color_list, selected=color_list[0])
  63. color_select.command=change_text_color
  64. size = Slider(preferences_controls, align="left", start = 8, end = 42, command=change_text_size)
  65.  
  66.  
  67. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement