Advertisement
davidhellam

Python: Text Editor 4

Aug 11th, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.60 KB | None | 0 0
  1. from guizero import App, TextBox,Text, PushButton, Box, Slider, ButtonGroup, MenuBar, info, yesno
  2.  
  3. from time import sleep
  4.  
  5. def exit_app():
  6.     if (save_button.enabled):
  7.         if (yesno("Unsaved Data Alert!","You have unsaved data, do you really want to quit?")):
  8.             app.destroy()
  9.         else:
  10.             info("OK","Returning to Editor")
  11.     else:
  12.         app.destroy()
  13.  
  14. def darktheme():
  15.     if (bt_night_toggle.bg == "black"):
  16.         editor.bg = "black"
  17.         editor.text_color = "white"
  18.         bt_night_toggle.bg = "yellow"
  19.         bt_night_toggle.text_color = "black"
  20.     else:
  21.         editor.bg = "white"
  22.         editor.text_color = "black"
  23.         bt_night_toggle.bg = "black"
  24.         bt_night_toggle.text_color = "yellow"
  25.        
  26.  
  27. def dofonts():
  28.     sl_fontsize.show()
  29.     bg_fontcolour.show()
  30.     bt_fontstuff.disable()
  31.     bt_endfonts.show()
  32.    
  33. def finished():
  34.     sl_fontsize.hide()
  35.     bg_fontcolour.hide()
  36.     bt_fontstuff.enable()
  37.     bt_endfonts.hide()
  38.  
  39. def about():
  40.     info("About this Program","It\'s a Text Editor")
  41.  
  42. def more():
  43.     info("More About this Program","by David Hellam")
  44.  
  45. def fontsize():
  46.     editor.text_size = sl_fontsize.value
  47.  
  48. def fontcolour():
  49.     editor.text_color = bg_fontcolour.value
  50.  
  51. def open_file():
  52.     with open(file_name.value, "r") as f:
  53.         editor.value = f.read()
  54.  
  55. # function for writing files
  56. def save_file():
  57.     with open(file_name.value, "w") as f:
  58.         f.write(editor.value)
  59.     save_button.disable()
  60.    
  61.  
  62. def save_enable():
  63.     save_button.enable()
  64.  
  65. app = App(title="My Text Editor in Python", width=640, height=480, layout="grid")
  66. app.bg=(204,204,255)
  67.  
  68. menubar = MenuBar(app,
  69.                   toplevel=["File","Help"],
  70.                   options=[[["open",open_file],["save",save_file],["exit",exit_app]],[["About",about],["Author",more]]])
  71.  
  72. # create a box to house the controls, we want the box to span the entire width of the app
  73. file_controls = Box(app, align="left", height=20, width=560, grid=[0,0])
  74.  
  75. # create a TextBox for the file name
  76. txt_filelabel=Text(file_controls,text="File Name: ",size=8, align="left")
  77. file_name = TextBox(file_controls, text="example.txt", width=50, align="left")
  78. file_name.bg=(255,255,255)
  79.  
  80. # create a TextBox which is not in the box and fills the rest of the GUI
  81. editspace = Box(app, align="top", height=460, width=560, grid=[0,1])
  82. editor = TextBox(editspace, multiline=True, height="fill", width="fill", scrollbar=True)
  83. editspace.bg=(255,255,255)
  84. editor.update_command(save_enable)
  85.  
  86. # create a save button which uses the save_file function
  87. buttonspace = Box(app, align="top", width=80, height=480, grid=[1,0,1,2])
  88. save_button = PushButton(buttonspace, width=6, text="Save", command=save_file)
  89. save_button.bg=(255,204,204)
  90.  
  91. # create an open button which uses the open_file function
  92. open_button = PushButton(buttonspace, text="Open", width=6, command=open_file)
  93. open_button.bg=(204,255,204)
  94.  
  95. bt_fontstuff = PushButton(buttonspace, text="Font Stuff", width=6, command=dofonts)
  96.  
  97. # create a slider for font size
  98. sl_fontsize = Slider(buttonspace,start=6,end=36,horizontal=False, command=fontsize)
  99. sl_fontsize.hide()
  100.  
  101. # create a button group for text colour
  102. bg_fontcolour = ButtonGroup(buttonspace, options=["Black", "Red", "Green", "Blue"], selected="Black", command=fontcolour)
  103. bg_fontcolour.hide()
  104.  
  105. bt_endfonts = PushButton(buttonspace, text="Finish", width=6, command=finished)
  106. bt_endfonts.hide()
  107.  
  108. bt_night_toggle = PushButton(buttonspace, text="Night Time", width=6, command=darktheme)
  109. bt_night_toggle.bg="black"
  110. bt_night_toggle.text_color="yellow"
  111.  
  112. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement