Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from guizero import App, TextBox,Text, PushButton, Box, Slider, ButtonGroup, MenuBar, info, yesno
- from time import sleep
- def exit_app():
- if (save_button.enabled):
- if (yesno("Unsaved Data Alert!","You have unsaved data, do you really want to quit?")):
- app.destroy()
- else:
- info("OK","Returning to Editor")
- else:
- app.destroy()
- def darktheme():
- if (bt_night_toggle.bg == "black"):
- editor.bg = "black"
- editor.text_color = "white"
- bt_night_toggle.bg = "yellow"
- bt_night_toggle.text_color = "black"
- else:
- editor.bg = "white"
- editor.text_color = "black"
- bt_night_toggle.bg = "black"
- bt_night_toggle.text_color = "yellow"
- def dofonts():
- sl_fontsize.show()
- bg_fontcolour.show()
- bt_fontstuff.disable()
- bt_endfonts.show()
- def finished():
- sl_fontsize.hide()
- bg_fontcolour.hide()
- bt_fontstuff.enable()
- bt_endfonts.hide()
- def about():
- info("About this Program","It\'s a Text Editor")
- def more():
- info("More About this Program","by David Hellam")
- def fontsize():
- editor.text_size = sl_fontsize.value
- def fontcolour():
- editor.text_color = bg_fontcolour.value
- def open_file():
- with open(file_name.value, "r") as f:
- editor.value = f.read()
- # function for writing files
- def save_file():
- with open(file_name.value, "w") as f:
- f.write(editor.value)
- save_button.disable()
- def save_enable():
- save_button.enable()
- app = App(title="My Text Editor in Python", width=640, height=480, layout="grid")
- app.bg=(204,204,255)
- menubar = MenuBar(app,
- toplevel=["File","Help"],
- options=[[["open",open_file],["save",save_file],["exit",exit_app]],[["About",about],["Author",more]]])
- # create a box to house the controls, we want the box to span the entire width of the app
- file_controls = Box(app, align="left", height=20, width=560, grid=[0,0])
- # create a TextBox for the file name
- txt_filelabel=Text(file_controls,text="File Name: ",size=8, align="left")
- file_name = TextBox(file_controls, text="example.txt", width=50, align="left")
- file_name.bg=(255,255,255)
- # create a TextBox which is not in the box and fills the rest of the GUI
- editspace = Box(app, align="top", height=460, width=560, grid=[0,1])
- editor = TextBox(editspace, multiline=True, height="fill", width="fill", scrollbar=True)
- editspace.bg=(255,255,255)
- editor.update_command(save_enable)
- # create a save button which uses the save_file function
- buttonspace = Box(app, align="top", width=80, height=480, grid=[1,0,1,2])
- save_button = PushButton(buttonspace, width=6, text="Save", command=save_file)
- save_button.bg=(255,204,204)
- # create an open button which uses the open_file function
- open_button = PushButton(buttonspace, text="Open", width=6, command=open_file)
- open_button.bg=(204,255,204)
- bt_fontstuff = PushButton(buttonspace, text="Font Stuff", width=6, command=dofonts)
- # create a slider for font size
- sl_fontsize = Slider(buttonspace,start=6,end=36,horizontal=False, command=fontsize)
- sl_fontsize.hide()
- # create a button group for text colour
- bg_fontcolour = ButtonGroup(buttonspace, options=["Black", "Red", "Green", "Blue"], selected="Black", command=fontcolour)
- bg_fontcolour.hide()
- bt_endfonts = PushButton(buttonspace, text="Finish", width=6, command=finished)
- bt_endfonts.hide()
- bt_night_toggle = PushButton(buttonspace, text="Night Time", width=6, command=darktheme)
- bt_night_toggle.bg="black"
- bt_night_toggle.text_color="yellow"
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement