Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- from guizero import App,TextBox,PushButton,Box,Combo,Slider,MenuBar,CheckBox
- from tkinter import filedialog
- revertText = ""
- def OpenFile():
- global revertText
- filepath = filedialog.askopenfilename()
- file_name.value = filepath
- f = open(filepath,"r")
- editor.value = f.read()
- revertText = editor.value
- save_button.hide()
- f.close()
- def SaveFile():
- global revertText
- filepath = filedialog.asksaveasfile()
- if filepath:
- f = open(filepath.name,"w")
- f.write(editor.value)
- revertText = editor.value
- save_button.hide()
- open_button.enable()
- revert_button.disable()
- f.close()
- def ChangeFont():
- editor.font = font.value
- def ChangeTextSize():
- editor.text_size = size.value
- editor.resize(1,1)
- editor.resize("fill","fill")
- def ChangeColor():
- editor.text_color = color.value
- def TextChanged():
- global revertText
- editText = editor.value
- compareText = revertText
- if editText == compareText:
- save_button.hide()
- open_button.enable()
- revert_button.disable()
- else:
- save_button.show()
- open_button.disable()
- revert_button.enable()
- def Revert():
- global revertText
- editor.value = revertText
- revertText = editor.value # weird guizero artificat if not done
- save_button.hide()
- open_button.enable()
- revert_button.disable()
- # A new function that closes the app
- def ExitApp():
- global revertText
- if revertText != editor.value:
- quitNow = app.yesno('Warning!',
- 'Quit without saving changes?')
- if not quitNow:
- return
- app.destroy()
- def DarkMode():
- if darkMode.value:
- app.bg = "black"
- editor.saved_text_color = editor.text_color
- app.text_color = "white"
- editor.text_color = editor.saved_text_color
- else:
- app.bg = "white"
- editor.saved_text_color = editor.text_color
- app.text_color = "black"
- editor.text_color = editor.saved_text_color
- app = App(title="texteditor")
- # The new MenuBar
- menubar = MenuBar(app,
- toplevel=["File","Edit"],
- options=[[["open",OpenFile],["save",SaveFile],
- ["exit",ExitApp]],
- [["revert",Revert]]])
- # create a box to house the controls, we want the box to span the entire width of the app
- file_controls = Box(app, align="top", width="fill")
- # create a TextBox for the file name
- file_name = TextBox(file_controls, text="text_file.txt", width=32, align="left")
- # create a save button which uses the save_file function
- save_button = PushButton(file_controls,text="Save",align="right",
- visible=False,command=SaveFile)
- # create an open button which uses the open_file function
- open_button = PushButton(file_controls,text="Open",align="right",
- command=OpenFile)
- revert_button = PushButton(file_controls,text="Revert",align="right",
- enabled=False,command=Revert)
- preferences_controls = Box(app, align="top", width="fill", border=True)
- font = Combo(preferences_controls, options=["courier", "times new roman", "verdana"],
- align="left", command=ChangeFont)
- color = Combo(preferences_controls, options=["black", "red", "blue", "green"],
- align="left", command=ChangeColor)
- size = Slider(preferences_controls, align="left",
- start = 8, end = 42, command=ChangeTextSize)
- darkMode = CheckBox(preferences_controls,text="Dark Mode",
- align="left",command=DarkMode)
- # create a TextBox which is not in the box and fills the rest of the GUI
- editor = TextBox(app, multiline=True, height="fill", width="fill",command=TextChanged)
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement