Advertisement
Vesna_To

text_editor3

Aug 12th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. from guizero import *
  2.  
  3. app = App(title="texteditor")
  4. # function for reading files
  5. def open_file():
  6. with open(file_name.value, "r") as f:
  7. editor.value = f.read()
  8.  
  9. # function for writing files
  10. def save_file():
  11. # Disable the save_button
  12. save_button.hide()
  13. with open(file_name.value, "w") as f:
  14. f.write(editor.value)
  15.  
  16.  
  17. def enable_save():
  18. save_button.show()
  19.  
  20.  
  21. def change_font():
  22. editor.font = font.value
  23.  
  24. def change_text_size():
  25. editor.text_size = size.value
  26. # 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
  27. editor.resize(1, 1)
  28. editor.resize("fill", "fill")
  29.  
  30. def change_color ():
  31. editor.text_color = font_c.value
  32.  
  33. # A new function that closes the app
  34. def exit_app():
  35. if save_button.visible==True:
  36. app.info("Warning!", "You have not saved the changes")
  37. else:
  38. app.destroy()
  39.  
  40. def undo():
  41. app.info("Information", "This function has not been implemented yet!")
  42.  
  43. def copy():
  44. app.info("Information", "This function has not been implemented yet!")
  45.  
  46. def cut():
  47. app.info("Information", "This function has not been implemented yet!")
  48.  
  49. def paste():
  50. app.info("Information", "This function has not been implemented yet!")
  51.  
  52. def find():
  53. app.info("Information", "This function has not been implemented yet!")
  54.  
  55. def replace():
  56. app.info("Information", "This function has not been implemented yet!")
  57.  
  58.  
  59. def help_function():
  60. app.info("Information", "This function has not been implemented yet!")
  61.  
  62. def about():
  63. app.info("Information", "This function has not been implemented yet!")
  64.  
  65. def make_dark_mode ():
  66. if chk_dark.value == 1:
  67. app.bg="black"
  68. app.text_color = "white"
  69. else:
  70. app.bg="white"
  71. app.text_color = "black"
  72. # new Box for font, size and color
  73.  
  74. menubar = MenuBar(app,
  75. toplevel=["File","Edit","Format","Help"],
  76. options=[[["open",open_file],["save",save_file],["exit",exit_app]],
  77. [["Undo",undo],["Copy",copy], ["Cut",cut], ["Paste",paste], ["Find",find],["Replace",replace]],
  78. [["Font",change_font]],
  79. [["Help",help_function], ["About", about]]])
  80.  
  81. preferences_controls = Box(app, width="fill", border=True)
  82.  
  83. font = Combo(preferences_controls, options=["Courier", "Times new roman", "Verdana", "Calibri", "Tahoma"], align="left", command=change_font)
  84. size = Slider(preferences_controls, align="left", command=change_text_size, start=10, end=18)
  85. font_c = Combo(preferences_controls, options=["Red", "Green", "Blue", "White", "Black", "Gray"], align="left", command=change_color)
  86.  
  87. # box to house the controls, we want the box to span the entire width of the app
  88. file_controls = Box(app, align="bottom", width="fill")
  89.  
  90. # TextBox for the file name
  91. file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
  92.  
  93. # save button which uses the save_file function
  94. save_button = PushButton(file_controls, text="Save", command=save_file, align="right", visible=False)
  95.  
  96. # open button which uses the open_file function
  97. open_button = PushButton(file_controls, text="Open", command=open_file, align="right")
  98.  
  99.  
  100. # text
  101. editor = TextBox(app, multiline=True, height="fill", width="fill", command=enable_save)
  102.  
  103. box_dark=Box(app)
  104. chk_dark = CheckBox (box_dark, text="Dark Mode", command=make_dark_mode)
  105.  
  106. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement