Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from guizero import App, Box, PushButton, TextBox, Combo, Text, info, Window, MenuBar, CheckBox
- # Event Handlers
- def btnSaveClicked():
- global fileSaved
- f = open(txtFilename.value, "w")
- f.write(txtEditor.value)
- f.close()
- info('SAVE','Your file has been saved')
- fileSaved = True
- def btnLoadClicked():
- with open(txtFilename.value, "r") as f:
- txtEditor.value = f.read()
- def btnExitClicked():
- if fileSaved:
- app.destroy()
- else:
- openSaveWindow()
- def ChangeBgcolour():
- boxEditor.bg = cboChangeBg.value
- bgWindow.hide()
- def btnChangeFontSize():
- fontSizeWindow.show(wait=True)
- def btnChangeFont():
- fontWindow.show(wait = True)
- def btnChangeBg():
- bgWindow.show(wait = True)
- def FontChanged():
- boxEditor.font = cboChangeFont.value
- fontWindow.hide()
- def ChangeFontSize():
- boxEditor.text_size = int(cboChangeFontSize.value)
- fontSizeWindow.hide()
- def cboCancel():
- fontWindow.hide()
- def cboCancel2():
- fontSizeWindow.hide()
- def cboCancel3():
- bgWindow.hide()
- def cboCancel4():
- saveWindow.hide()
- def textChanged():
- global fileSaved
- fileSaved = False
- def openSaveWindow():
- saveWindow.show(wait=True)
- def confirmExit():
- app.destroy()
- def darkMode():
- if chkDark.value == 1:
- boxEditor.bg = 'black'
- boxEditor.text_color = 'white'
- else:
- boxEditor.bg = 'light yellow'
- boxEditor.text_color = 'black'
- # Main App WIndow
- app = App(title="Text Editor",width="800",height="600")
- fileSaved = False
- # Menu Bar
- menubar = MenuBar(app,
- toplevel=["File", "Appearance"],
- options=[
- [ ["Open", btnLoadClicked], ["Save", btnSaveClicked], ['Exit', btnExitClicked ] ],
- [ ["Font", btnChangeFont], ["Font Size", btnChangeFontSize], ["Background", btnChangeBg ] ]
- ])
- # GUI widgets
- boxFileControls = Box(app, align="top", width='fill', height = 30)
- boxFileControls.bg = "white"
- boxFileControls.text_color = 'black'
- lblFilename = Text(boxFileControls, text = 'Filename: ', align = 'left' ,color = 'red')
- lblFilename.text_size = 10
- txtFilename = TextBox(boxFileControls, text = 'editor.txt', width = 30, height = 'fill', align = 'left')
- txtFilename.text_size =10
- chkDark = CheckBox(boxFileControls, text="Dark Mode", align = 'right', command = darkMode)
- #..............................................................................
- boxEditor = Box(app, align = 'top', width='fill', height = 'fill', border = 1)
- boxEditor.bg = 'light yellow'
- boxEditor.font ='Palatino Linotype'
- txtEditor = TextBox(boxEditor, width = 'fill', height = 'fill', multiline = True, scrollbar = True, command = textChanged)
- #..............................................................................
- # Windows
- # Font Window
- fontWindow = Window(app,"Change Font",width = 300, height = 200)
- fontWindow.bg = 'light blue'
- fontWindow.font='Segoe Ui'
- fontWindow.hide()
- lblChangeFont = Text(fontWindow, text = "Select a font", align='top')
- cboChangeFont = Combo(fontWindow, options = ['Times New Roman','Courier New', 'Palatino Linotype', 'Segoe UI'],align = 'top')
- btnSubmit1 = PushButton(fontWindow, text = 'OK', align = 'left', width = 20, command = FontChanged)
- btnCancel1 = PushButton(fontWindow, text = 'CANCEL', align = 'left', width = 20, command = cboCancel)
- # Font Size Window
- fontSizeWindow = Window(app,"Change Font Size",width = 300, height = 200)
- fontSizeWindow.bg = 'light blue'
- fontSizeWindow.font='Segoe Ui'
- fontSizeWindow.hide()
- lblChangeFontSize = Text(fontSizeWindow, text = "Select a size",align="top")
- cboChangeFontSize = Combo(fontSizeWindow, options = ['10','12','14','16','18','36'],align="top")
- btnSubmit2 = PushButton(fontSizeWindow, text = 'OK', align = 'left', width = 20, command = ChangeFontSize)
- btnCancel2 = PushButton(fontSizeWindow, text = 'CANCEL', align = 'left', width = 20, command = cboCancel2)
- # Background Colour Window
- bgWindow = Window(app,"Change Background Colour",width = 300, height = 200)
- bgWindow.bg = 'light blue'
- bgWindow.font='Segoe Ui'
- bgWindow.hide()
- lblChangeBg= Text(bgWindow, text = "Select a colour",align="top")
- cboChangeBg = Combo(bgWindow, options = ['light yellow','honeydew','light blue','lavender','pink'],align="top")
- btnSubmit3 = PushButton(bgWindow, text = 'OK', align = 'left', width = 20, command = ChangeBgcolour)
- btnCancel3 = PushButton(bgWindow, text = 'CANCEL', align = 'left', width = 20, command = cboCancel3)
- # Save Window
- saveWindow = Window(app,"WARNING",width = 400, height = 70)
- saveWindow.bg = 'yellow2'
- saveWindow.font='Segoe Ui'
- saveWindow.hide()
- lblWarning = Text(saveWindow, text = 'Your file has not been saved. Do you really want to exit?', color = 'red')
- btnOK = PushButton(saveWindow, text = 'OK', align = 'left', width = 20, command = confirmExit)
- btnCancel4 = PushButton(saveWindow, text = 'CANCEL', align = 'right', width = 20, command = cboCancel4)
- #..............................................................................
- # Show the GUI on the screen
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement