In [82]:
try:
import tkinter as tk
except:
import Tkinter as tk
try:
import tkMessageBox as pop_up
except:
import tkinter.tkMessageBox as pop_up
import time
EMPTY_TITLE_ERROR_MESSAGE_SAVE = "Please write the name of the file you want to save in the given field."
EMPTY_TITLE_ERROR_MESSAGE_OPEN = "Please write the name of the file you want to open in the given field."
FILE_NOT_FOUND_ERROR_MESSAGE = "No file with the given title was found, remember that this text editor can only read files in its directory."
SAVING_SUCCESS_MESSAGE = "Your text is now stored in the {filename} file"
SIGNATURE_TXT_NOT_FOUND_MESSAGE = "Please be sure that the file you want to open exists and that it is in the same folder of this editor."
def _open():
if not file_title.get():
pop_up.showerror("Title is empty.", EMPTY_TITLE_ERROR_MESSAGE_OPEN)
return 1
if ".txt" not in file_title.get():
filename = file_title.get() + ".txt"
try:
with open(filename) as f:
main_text.delete("1.0", tk.END)
main_text.insert(tk.INSERT, f.read(), "a")
except IOError:
pop_up.showerror("File not found.", FILE_NOT_FOUND_ERROR_MESSAGE)
def save():
if not file_title.get():
pop_up.showerror("No title.", EMPTY_TITLE_ERROR_MESSAGE_SAVE)
return 1
if ".txt" not in file_title.get():
filename=file_title.get() + ".txt"
with open(filename, "w+") as f:
f.write(main_text.get(1.0, tk.END))
pop_up.showinfo(
"File saved succesfully",
SAVING_SUCCESS_MESSAGE.format(
filename=filename))
def add_date():
full_date = time.localtime()
day = str(full_date.tm_mday)
month = str(full_date.tm_mon)
year = str(full_date.tm_year)
date = "\n" + day + '/' + month + '/' + year
main_text.insert(tk.INSERT, date, "a")
def add_signature():
try:
with open("signature.txt") as f:
main_text.insert(tk.INSERT, "\n" + f.read(), "a")
except IOError:
MESSAGE = SIGNATURE_TXT_NOT_FOUND_MESSAGE
pop_up.showerror("\"signature.txt\" not found.", MESSAGE)
root=tk.Tk()
menubar= tk.Menu(root)
menubar.add_command(label="Open", command=_open)
menubar.add_command(label="Save", command=save)
menubar.add_command(label="Add Date", command=add_date)
menubar.add_command(label="Add Signature", command=add_signature)
root.config(menu=menubar)
#canvas = tk.Canvas(root)
#scroll_up=tk.Scrollbar(root, command=canvas.yview)
#scroll_up.pack(side=tk.RIGHT, fill=tk.BOTH)
#canvas.configure(yscrollcommand = scrollbar.set)
#canvas.bind('<Configure>', on_configure)
# --- put frame in canvas ---
#frame = tk.Frame(canvas)
#canvas.create_window((0,0), window=frame, anchor='nw')
#canvas = tk.Canvas(root)
#frame = tk.Frame(canvas)
#vsb = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
#canvas.configure(yscrollcommand=vsb.set)
#vsb.pack(side="right", fill="y")
#canvas.create_window((4,4), window=frame, anchor="nw")
#frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))
top = tk.Frame(root)
temp = tk.Label(root, text="Title:")
temp.pack(in_=top, side=tk.LEFT)
file_title=tk.Entry(root)
file_title.pack(in_=top, side=tk.RIGHT)
top.pack()
main_text=tk.Text(root)
main_text.pack()
tk.mainloop()
In [ ]:
In [81]:
from Tkinter import *
root=Tk()
frame=Frame(root,width=300,height=300)
frame.grid(row=0,column=0)
canvas=Canvas(frame,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
hbar=Scrollbar(frame,orient=HORIZONTAL)
hbar.pack(side=BOTTOM,fill=X)
hbar.config(command=canvas.xview)
vbar=Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config(width=300,height=300)
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
canvas.pack(side=LEFT,expand=True,fill=BOTH)
root.mainloop()
In [ ]: