Advertisement
kalpin

Text Editor v4

Aug 5th, 2020 (edited)
1,309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.16 KB | None | 0 0
  1. from guizero import App, Box, PushButton, TextBox, Combo, Text, info, Window, MenuBar, CheckBox
  2.  
  3. # Event Handlers
  4.  
  5. def btnSaveClicked():
  6.     global fileSaved
  7.     f =  open(txtFilename.value, "w")
  8.     f.write(txtEditor.value)
  9.     f.close()
  10.     info('SAVE','Your file has been saved')
  11.     fileSaved = True
  12.  
  13. def btnLoadClicked():
  14.     with open(txtFilename.value, "r") as f:
  15.         txtEditor.value = f.read()
  16.  
  17. def btnExitClicked():
  18.     if fileSaved:
  19.         app.destroy()
  20.     else:
  21.         openSaveWindow()
  22.  
  23. def ChangeBgcolour():
  24.     boxEditor.bg = cboChangeBg.value
  25.     bgWindow.hide()
  26.  
  27. def btnChangeFontSize():
  28.     fontSizeWindow.show(wait=True)
  29.    
  30. def btnChangeFont():
  31.     fontWindow.show(wait = True)
  32.  
  33. def btnChangeBg():
  34.     bgWindow.show(wait = True)
  35.        
  36. def FontChanged():
  37.     boxEditor.font = cboChangeFont.value
  38.     fontWindow.hide()
  39.    
  40. def ChangeFontSize():
  41.     boxEditor.text_size = int(cboChangeFontSize.value)
  42.     fontSizeWindow.hide()
  43.  
  44. def cboCancel():
  45.     fontWindow.hide()
  46.  
  47. def cboCancel2():
  48.     fontSizeWindow.hide()
  49.  
  50. def cboCancel3():
  51.     bgWindow.hide()
  52.  
  53. def cboCancel4():
  54.     saveWindow.hide()    
  55.  
  56. def textChanged():
  57.     global fileSaved
  58.     fileSaved = False
  59.  
  60. def openSaveWindow():
  61.     saveWindow.show(wait=True)
  62.  
  63. def confirmExit():
  64.     app.destroy()
  65.  
  66. def darkMode():
  67.     if chkDark.value == 1:
  68.         boxEditor.bg = 'black'
  69.         boxEditor.text_color = 'white'
  70.     else:
  71.         boxEditor.bg = 'light yellow'
  72.         boxEditor.text_color = 'black'
  73.        
  74. # Main App WIndow
  75.  
  76. app = App(title="Text Editor",width="800",height="600")
  77. fileSaved = False
  78.  
  79. # Menu Bar
  80. menubar = MenuBar(app,
  81.                   toplevel=["File", "Appearance"],
  82.                   options=[
  83.                       [ ["Open", btnLoadClicked], ["Save", btnSaveClicked], ['Exit', btnExitClicked ] ],
  84.                       [ ["Font", btnChangeFont], ["Font Size", btnChangeFontSize], ["Background", btnChangeBg ] ]
  85.                   ])
  86.  
  87. # GUI widgets
  88.  
  89. boxFileControls = Box(app, align="top", width='fill', height = 30)
  90. boxFileControls.bg = "white"
  91. boxFileControls.text_color = 'black'
  92. lblFilename = Text(boxFileControls, text = 'Filename: ', align = 'left' ,color = 'red')
  93. lblFilename.text_size = 10
  94. txtFilename = TextBox(boxFileControls, text = 'editor.txt', width = 30, height = 'fill', align = 'left')
  95. txtFilename.text_size =10
  96. chkDark = CheckBox(boxFileControls, text="Dark Mode", align = 'right', command = darkMode)
  97.  
  98. #..............................................................................
  99.  
  100. boxEditor = Box(app, align = 'top', width='fill', height = 'fill', border = 1)
  101. boxEditor.bg = 'light yellow'
  102. boxEditor.font ='Palatino Linotype'
  103. txtEditor = TextBox(boxEditor, width = 'fill', height = 'fill', multiline = True, scrollbar = True, command = textChanged)
  104.  
  105. #..............................................................................
  106.  
  107. # Windows
  108.  
  109. # Font Window
  110. fontWindow = Window(app,"Change Font",width = 300, height = 200)
  111. fontWindow.bg = 'light blue'
  112. fontWindow.font='Segoe Ui'
  113. fontWindow.hide()
  114.  
  115. lblChangeFont = Text(fontWindow, text = "Select a font", align='top')
  116. cboChangeFont = Combo(fontWindow, options = ['Times New Roman','Courier New', 'Palatino Linotype', 'Segoe UI'],align = 'top')
  117. btnSubmit1 = PushButton(fontWindow, text = 'OK', align = 'left', width = 20, command = FontChanged)
  118. btnCancel1 = PushButton(fontWindow, text = 'CANCEL', align = 'left', width = 20, command = cboCancel)
  119.  
  120. # Font Size Window
  121. fontSizeWindow = Window(app,"Change Font Size",width = 300, height = 200)
  122. fontSizeWindow.bg = 'light blue'
  123. fontSizeWindow.font='Segoe Ui'
  124. fontSizeWindow.hide()
  125.  
  126. lblChangeFontSize = Text(fontSizeWindow, text = "Select a size",align="top")
  127. cboChangeFontSize = Combo(fontSizeWindow, options = ['10','12','14','16','18','36'],align="top")
  128. btnSubmit2 = PushButton(fontSizeWindow, text = 'OK', align = 'left', width = 20, command = ChangeFontSize)
  129. btnCancel2 = PushButton(fontSizeWindow, text = 'CANCEL', align = 'left', width = 20, command = cboCancel2)
  130.  
  131. # Background Colour Window
  132. bgWindow = Window(app,"Change Background Colour",width = 300, height = 200)
  133. bgWindow.bg = 'light blue'
  134. bgWindow.font='Segoe Ui'
  135. bgWindow.hide()
  136.  
  137. lblChangeBg= Text(bgWindow, text = "Select a colour",align="top")
  138. cboChangeBg = Combo(bgWindow, options = ['light yellow','honeydew','light blue','lavender','pink'],align="top")
  139. btnSubmit3 = PushButton(bgWindow, text = 'OK', align = 'left', width = 20, command = ChangeBgcolour)
  140. btnCancel3 = PushButton(bgWindow, text = 'CANCEL', align = 'left', width = 20, command = cboCancel3)
  141.  
  142. # Save Window
  143. saveWindow = Window(app,"WARNING",width = 400, height = 70)
  144. saveWindow.bg = 'yellow2'
  145. saveWindow.font='Segoe Ui'
  146. saveWindow.hide()
  147.  
  148. lblWarning = Text(saveWindow, text = 'Your file has not been saved. Do you really want to exit?', color = 'red')
  149. btnOK = PushButton(saveWindow, text = 'OK', align = 'left', width = 20, command = confirmExit)
  150. btnCancel4 = PushButton(saveWindow, text = 'CANCEL', align = 'right', width = 20, command = cboCancel4)
  151.  
  152. #..............................................................................
  153.  
  154. # Show the GUI on the screen
  155.  
  156. app.display()
  157.  
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement