Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import messagebox as mb
- import os
- _root = frame1
- filepath0 = ''
- def getnewpath():
- global filepath0
- global os
- filepath1 = 'T:/Storage/Documents/Untitled'
- rep = 0
- while True:
- if os.path.exists(filepath1 + str(rep) + '.txt'):
- rep += 1
- else:
- filepath0 = filepath1 + str(rep) + '.txt'
- break
- getnewpath()
- textarea = tk.Text(_root, font='consolas', bd=0)
- menubar = tk.Frame(_root, bd=1, relief='solid', bg='white')
- tascrollbar = tk.Scrollbar(textarea, cursor='arrow')
- textarea.config(yscrollcommand=tascrollbar.set)
- tascrollbar.config(command=textarea.yview)
- def open_file(path):
- global textarea
- global filepath0
- filepath0 = path
- _file = open(path, 'r')
- try:
- _conts = _file.read()
- _file.close()
- textarea.delete(1.0, 'end')
- textarea.insert(1.0, _conts)
- except UnicodeDecodeError:
- textarea.delete(1.0, 'end')
- textarea.insert(1.0, 'Unable to open file\nFile is not in text format.')
- def save_file():
- global filepath0
- global textarea
- try:
- sav = open(filepath0, 'w')
- sav.write(textarea.get(1.0, 'end')[:-1])
- sav.close()
- except PermissionError:
- global mb
- mb.showinfo("Permission Denied", "You do not seem to have permission to edit this file.")
- def new_file():
- global getnewpath
- global textarea
- textarea.delete(1.0, 'end')
- getnewpath()
- newfilebutton = tk.Button(menubar, bd=0, bg='white', activebackground='lightgray', text='New', command=new_file)
- savefilebutton = tk.Button(menubar, bd=0, bg='white', activebackground='lightgray', text='Save', command=save_file)
- menubar.pack(side='top', anchor='n', fill='x')
- newfilebutton.pack(anchor='w', side='left')
- savefilebutton.pack(anchor='w', side='left')
- textarea.pack(expand=True, fill='both')
- tascrollbar.pack(side='right', anchor='e', fill='y')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement