Advertisement
mrFitzpatrick

textEditor_V1.1

Aug 20th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. from guizero import App, TextBox, Text, PushButton, Box, Combo, CheckBox
  2.  
  3. # function for reading files
  4. def open_file():
  5.     with open(file_name.value, "r") as f:
  6.         editor.value = f.read()
  7.         app.title = "Now editing " + file_name.value
  8.  
  9. # function for writing files
  10. def save_file():
  11.     with open(file_name.value, "w") as f:
  12.         f.write(editor.value)
  13.         app.title = file_name.value + " successfully saved!"
  14.     save_button.disable()
  15.     file_name.disable()
  16.    
  17. def showHide():
  18.     if chk_showHide.value == True:
  19.         box_appearance_controls.show()
  20.     elif chk_showHide.value == False:
  21.         box_appearance_controls.hide()
  22.    
  23. def bg_col():
  24.     if cmb_bg.value == "Calm":
  25.         editor.bg = "#fcdd88"
  26.         editor.text_color = "#855600"
  27.     elif cmb_bg.value == "Clear":
  28.         editor.bg = "#ffffff"
  29.         editor.text_color = "#2222ff"
  30.     elif cmb_bg.value == "Hacker":
  31.         editor.bg = "#000000"
  32.         editor.text_color = "#4cf000"
  33.  
  34. def font_up():
  35.     editor.text_size = editor.text_size + 2
  36.  
  37. def font_down():
  38.     editor.text_size = editor.text_size - 2
  39.  
  40. def enable_save():
  41.     save_button.enable()
  42.  
  43. app = App(title="texteditor", width=500)
  44.  
  45. #Box to house the show/hide control widget
  46. box_showHide = Box(app, align="left", visible=True)
  47. box_showHide.bg="#fcdd88"
  48. chk_showHide = CheckBox(box_showHide, text="toolbar on or off", height=50, align="left", visible=True, enabled=True, command=showHide)
  49.  
  50. #Box to house the visual options
  51. box_appearance_controls = Box(app, layout="auto", align="top", width="fill", height=50)
  52.  
  53. #Widgets for background colour
  54. lbl_background = Text(box_appearance_controls, text="What's your mood?", align="left")
  55. cmb_bg = Combo(box_appearance_controls, options=["Clear", "Hacker", "Calm"], align="left", height="fill", command=bg_col)
  56.  
  57. #Widgets for font size
  58. lbl_size = Text(box_appearance_controls, text="        Font size: ", align="left")
  59. #box_spacer = Box(box_appearance_controls, width=25, align="left", height="fill")
  60. btn_font_up = PushButton(box_appearance_controls, command=font_up, text="+", align="left")
  61. btn_font_down = PushButton(box_appearance_controls, command=font_down, text="-", align="left")
  62.  
  63.  
  64. # create a TextBox which is not in the box and fills the rest of the GUI
  65. editor = TextBox(app, multiline=True, height="fill", width="fill", scrollbar=True, command=enable_save)
  66.  
  67.  
  68. # create a box to house the controls, we want the box to span the entire width
  69. file_controls = Box(app, align="bottom", width="fill")
  70.  
  71. # create a TextBox for the file name
  72. file_name = TextBox(file_controls, text="type file name here (add .txt to the end)", width=40, align="left")
  73.  
  74. # create an open button which uses the open_file function
  75. open_button = PushButton(file_controls, text="Open",  align="right", command=open_file)
  76.  
  77. box_spacer = Box(file_controls, width=15, align="right")
  78.  
  79. # create a save button which uses the save_file function
  80. save_button = PushButton(file_controls, text="Save",  align="right", command=save_file, enabled=False)
  81.  
  82. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement