Advertisement
Buzzbow

text editor inc darkmode

Aug 25th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. from guizero import*
  2.  
  3. app = App(title="texteditor")
  4.  
  5. font_tools = Window(app, visible = False)
  6. align_window = Window(app, visible = False)
  7.  
  8.  
  9. # A new function that closes the app
  10. def exit_app():
  11. app.info("Alert!", "Have you saved your work")
  12. app.destroy()
  13.  
  14. # save as function
  15. def save_as():
  16. name = app.question("Save as...", "What would you like to call your file?")
  17. # If cancel is pressed, None is returned
  18. # so check a name of file was entered
  19. if name is not None:
  20. file_name.value = name
  21. with open(file_name.value, "w") as f:
  22. f.write(editor.value)
  23.  
  24. # function for reading files
  25. def open_file():
  26. with open(file_name.value, "r") as f:
  27. editor.value = f.read()
  28.  
  29. # function for writing files
  30. def save_file():
  31. with open(file_name.value, "w") as f:
  32. f.write(editor.value)
  33. #disable the save button
  34. save_button.disable()
  35.  
  36. # enabling buttons
  37. def enable_save():
  38. save_button.enable()
  39. save_as_button.enable()
  40.  
  41. #change font function
  42. def change_font():
  43. editor.font = font.value
  44.  
  45. def change_text_size():
  46. editor.text_size = size.value
  47. # 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
  48. editor.resize(1, 1)
  49. editor.resize("fill", "fill")
  50.  
  51. #change colour function
  52. def change_color(value):
  53. editor.text_color = lb_color.value
  54.  
  55. #controls visable/invisable
  56. def controls_visible_toggle():
  57. if controls_ckb.value == True:
  58. file_controls.show()
  59. else:
  60. file_controls.hide()
  61.  
  62. def show():
  63. file_controls.visible = True
  64.  
  65. #show new font options in menu bar
  66. def show_font_tools():
  67. change_font.value = font.value
  68. size.value = size.value
  69. change_color.value = lb_color.value
  70. font_tools.show(wait = True)
  71.  
  72. def change_alignment():
  73. if bgp_alignment.value == "right":
  74. right = Text(app, editor, align = "right")
  75. elif bgp_alignment.value == "left":
  76. text.align = "left"
  77. elif bgp_alignment.value == "top":
  78. editor.align = "top"
  79. elif bgp_alignment.value == "bottom":
  80. editor.align = "bottom"
  81.  
  82. align_window.hide()
  83. save_as_button.enable()
  84.  
  85. def show_align_window():
  86. align_window.show(wait = True)
  87.  
  88. def darkmode():
  89. editor.darkmode = darkmode_button.value
  90. editor.bg = "black"
  91. editor.text_color = "white"
  92. editor.tk.config(insertbackground = "White")
  93.  
  94. def lightmode():
  95. editor.lightmode = lightmode_button.value
  96. editor.tk.config(insertbackground = "black")
  97. editor.bg = "white"
  98. editor.text_color = "black"
  99.  
  100.  
  101.  
  102.  
  103. def clear_text():
  104. editor.value = ""
  105.  
  106.  
  107. def commit_tools_window():
  108. editor.change_font = font.value
  109. editor.change_text_size = size.value
  110. editor.change_color = lb_color.value
  111. editor.resize(1, 1)
  112. editor.resize("fill", "fill")
  113. save_button.enable()
  114. font_tools.hide()
  115.  
  116.  
  117. def get_help():
  118. app.info("Help!", "PLease use this link: https://lawsie.github.io/guizero/ ")
  119.  
  120. # The new MenuBar
  121. menubar = MenuBar(app,
  122. toplevel=["File", "Edit", "Show", "Help"],
  123. options=[
  124. [["Open",open_file],
  125. ["Save",save_file],
  126. ["Save as", save_as],
  127. ["Exit",exit_app]],
  128. [["Format", show_font_tools],
  129. ["Align", show_align_window]],
  130. [["Show",show],
  131. ["dark", darkmode],
  132. ["light", lightmode]],
  133. [["Help", get_help]]
  134. ])
  135.  
  136.  
  137. file_controls = Box(app, align="top", width="fill", border=True)
  138. file_name = TextBox(file_controls, text="text_file.txt", width = 25, align="left")
  139. #preferences_controls = Box(app, align="top", height = 50, width = "fill", border=True)
  140. #controls visable toggle
  141. controls_ckb = CheckBox(file_controls, text="Show controls", align = "right", command = controls_visible_toggle)
  142.  
  143. save_button = PushButton(file_controls, text="Save", command = save_file, align="right")
  144. open_button = PushButton(file_controls, text="Open", command = open_file, align="right")
  145. save_as_button = PushButton(file_controls, text="Save as", command = save_as, align="right")
  146. change_align_button = PushButton(align_window, text = "change alignment", command = change_alignment)
  147. editor = TextBox(app, multiline = True, height = "fill", width = "fill", command = enable_save)
  148. darkmode_button = PushButton(app, text = "Darkmode", command = darkmode, align="right")
  149. lightmode_button = PushButton(app, text ="lightmode", command = lightmode, align="right")
  150.  
  151.  
  152. #font drop-down
  153. fontchosen = TextBox (font_tools, text = "Choose Font", width = 20, align = "top")
  154. font = Combo(font_tools, options=["courier", "times new roman", "verdana", "comic sans", "calibra", "Arial Black" ], width = 25, align="left", command = change_font)
  155.  
  156. #slider to control font size
  157. size = Slider(font_tools, align="left", command=change_text_size, start=8, end=22)
  158.  
  159. #change colour listbox
  160. lb_color = ListBox(
  161. font_tools,
  162. items = ["red", "green", "blue", "yellow", "purple", "turquoise", "pink", "orange", "black", "brown", "cyan"],
  163. selected = "black",
  164. align = "right",
  165. command = change_color,
  166. scrollbar = True)
  167.  
  168. #alignment tab
  169. message1 = Text(align_window, text = "Which alignment would you like?")
  170. bgp_alignment = ButtonGroup(align_window, options =["Top", "Bottom", "Left", "Right"], selected="none")
  171.  
  172. clear_button = PushButton(file_controls, text = "Clear Text", align="left", command = clear_text, enabled = True)
  173. tools_btn_done = PushButton(font_tools, align = "bottom", text="Done", command = commit_tools_window)
  174. save_button.disable()
  175.  
  176. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement