Advertisement
brendan-stanford

Texteditor-GUI-final

Oct 11th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. #setup libraries
  2. from guizero import App, TextBox, PushButton, Box, Combo, Slider, MenuBar, Text, CheckBox
  3.  
  4. #setup app
  5. app = App(title="text editor")
  6.  
  7. # function for reading files
  8. def open_file():
  9. with open(file_name.value, "r") as f:
  10. editor.value = f.read()
  11. #disable save button till edited
  12. save_button.hide()
  13. file_name.show()
  14.  
  15. # function for writing files
  16. def save_file():
  17. with open(file_name.value, "w") as f:
  18. f.write(editor.value)
  19. #disable save button till edited
  20. save_button.hide()
  21. file_name.show()
  22.  
  23. # function for changing font style
  24. def change_font():
  25. editor.font = font.value
  26.  
  27. #function for changing text size
  28. def change_text_size():
  29. editor.text_size = size.value
  30. #reset box size to adjust to new text size
  31. editor.resize(1,1)
  32. editor.resize("fill", "fill")
  33.  
  34. #function for changing text colour
  35. def change_color():
  36. editor.text_color = colour.value
  37.  
  38. #function to re-anable save button
  39. def save_enable():
  40. save_button.show()
  41.  
  42. #function that reveals file name after initializing as hidden
  43. def name_show():
  44. file_name.show()
  45.  
  46. #function that revealsthe file controls
  47. def file_controls_show():
  48. file_controls.show()
  49.  
  50. #function that reveals the text preferences
  51. def text_preferences_show():
  52. text_preferences.show()
  53.  
  54. #show editor after hiding for popup
  55. def editor_show():
  56. editor.show()
  57. popup.hide()
  58.  
  59. #function that exits the app
  60. def exit_app():
  61. popup.show()
  62. editor.hide()
  63.  
  64. #save and exit function
  65. def save_exit():
  66. with open(file_name.value, "w") as f:
  67. f.write(editor.value)
  68. app.destroy()
  69.  
  70. #exit without saving
  71. def final_exit():
  72. app.destroy()
  73.  
  74. #dark mode option
  75. def dark_mode():
  76. app.bg = "black"
  77. file_controls.text_color = "white"
  78. text_preferences.text_color = "white"
  79. editor.text_color = "white"
  80. popup.text_color = "white"
  81.  
  82. #Main menubar
  83. menubar = MenuBar(app, toplevel = ["File", "Edit"], options = [[["Open", file_controls_show], ["Save", save_file], ["Exit", exit_app]], [["Font", text_preferences_show], ["Size", text_preferences_show], ["Colour", text_preferences_show]]])
  84.  
  85. # create a box to house the controls, we want the box to span the entire width of the app
  86. file_controls = Box(app, align="top", width="fill", border = True)
  87.  
  88. #Create a new box to house text preferences
  89. text_preferences = Box(app, align = "top", width = "fill", border = True, visible = False)
  90. font = Combo(text_preferences, align = "left", options = ["courier", "helvetica", "times new roman", "verdana"], command = change_font)
  91. size = Slider(text_preferences, align = "left", command = change_text_size, start = 10, end = 24)
  92. colour = Combo(text_preferences, align = "left", command = change_color, options = ["black", "red", "blue", "green"])
  93.  
  94. # create an open button which uses the open_file function
  95. open_button = PushButton(file_controls, text="Open", align="left", command = open_file)
  96.  
  97. #create a button to create a new file
  98. new_button = PushButton(file_controls, text = "New", align = "left", command = name_show)
  99.  
  100. # create a TextBox for the file name
  101. file_name = TextBox(file_controls, text="text_file.txt", width=20, align="left")
  102.  
  103. # create a save button which uses the save_file function
  104. save_button = PushButton(file_controls, text="Save", align="right", command = save_file)
  105.  
  106. # create a TextBox which is not in the box and fills the rest of the GUI
  107. editor = TextBox(app, multiline=True, height="fill", width="fill", command = save_enable)
  108.  
  109. #warning popup to prompt user to save
  110. popup = Box(app, align = "top", height = 100, width = 500, border = True, visible = False)
  111. warn = Text(popup, text = "Save file before exit?")
  112. save_and_exit = PushButton(popup, text = "Save and Exit", align = "top", command = save_exit)
  113. last_exit = PushButton(popup, text = "Exit Without Saving", align = "top", command = final_exit)
  114.  
  115. #dark mode
  116. dark_box = Box(file_controls, align = "right", border = True)
  117. txt_drk = Text(dark_box, text = "Enable dark mode?", font = "Times", size = 10)
  118. chk_drk = CheckBox(dark_box, text = "Turn on dark mode!", command = dark_mode)
  119.  
  120. #start app
  121. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement