Advertisement
sphinx2001

Текстовый редактор

Oct 27th, 2020
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import filedialog
  3.  
  4.  
  5. def Quit(ev):
  6.     global root
  7.     root.destroy()
  8.  
  9.  
  10. def LoadFile(ev):
  11.     fn = filedialog.Open(root, filetypes=[('*.txt files', '.txt')]).show()
  12.     if fn == '':
  13.         return
  14.     textbox.delete('1.0', 'end')
  15.     textbox.insert('1.0', open(fn, 'rt').read())
  16.  
  17.  
  18. def SaveFile(ev):
  19.     fn = filedialog.SaveAs(root, filetypes=[('*.txt files', '.txt')]).show()
  20.     if fn == '':
  21.         return
  22.     if not fn.endswith(".txt"):
  23.         fn += ".txt"
  24.     open(fn, 'wt').write(textbox.get('1.0', 'end'))
  25.  
  26.  
  27. root = Tk()
  28.  
  29. panelFrame = Frame(root, height=60, bg='gray')
  30. textFrame = Frame(root, height=340, width=600)
  31.  
  32. panelFrame.pack(side='top', fill='x')
  33. textFrame.pack(side='bottom', fill='both', expand=1)
  34.  
  35. textbox = Text(textFrame, font='Arial 14', wrap='word')
  36. scrollbar = Scrollbar(textFrame)
  37.  
  38. scrollbar['command'] = textbox.yview
  39. textbox['yscrollcommand'] = scrollbar.set
  40.  
  41. textbox.pack(side='left', fill='both', expand=1)
  42. scrollbar.pack(side='right', fill='y')
  43.  
  44. loadBtn = Button(panelFrame, text='Load')
  45. saveBtn = Button(panelFrame, text='Save')
  46. quitBtn = Button(panelFrame, text='Quit')
  47.  
  48. loadBtn.bind("<Button-1>", LoadFile)
  49. saveBtn.bind("<Button-1>", SaveFile)
  50. quitBtn.bind("<Button-1>", Quit)
  51.  
  52. loadBtn.place(x=10, y=10, width=40, height=40)
  53. saveBtn.place(x=60, y=10, width=40, height=40)
  54. quitBtn.place(x=110, y=10, width=40, height=40)
  55.  
  56. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement