I made a text editor with a scrollbar added. I have two main issues here.
The problem I am facing is that my text does not completely go to the next line when it is close to the right side and the last character just hides under the scroll bar. I want the word should either move to the next line or the character should not go behind the scroll bar.
I have an option of changing fonts but the fonts list is unsorted and it is difficult to find the specific font. I don't know how to make it sorted or better option how to add a search widget for selection of specific font just like in Microsoft word editor.
Here is my code :
import os.path
from tkinter import *
from tkinter import colorchooser, font
from tkinter.messagebox import *
from tkinter.filedialog import *
'''
This is a text editior which can open save make new files
The fonts and color of text is changeable.
'''
def change_color():
color = colorchooser.askcolor(title="Pick any color")
text_area.config(fg=color[1])
def change_font(*args):
text_area.config(font=(font_name.get(), size_box.get()))
def new_file():
window.title("Untitled")
text_area.delete(1.0, END)
def open_file():
# from filedialog module it gives just the file directry and opening GUI
file_name = askopenfilename(defaultextension=".txt",
filetypes=[("Text Documents", '.txt'),
("All files",'*.*')])
# if window is closed without selecting a file.
# comparing it to none did not have effect as file is str type
# and has empty string when nothing is selected
if file_name == "":
print("No file was selected")
return
try:
window.title(os.path.basename(file_name))
text_area.delete(1.0, END)
# This builtin open function opens the file in given address
file = open(file_name, 'r')
# writes at start the strings text in file.read()
text_area.insert(1.0, file.read())
except Exception:
print("File could not be opened")
finally:
file.close()
def save_file():
file = asksaveasfilename(initialfile='Untitled.txt',
defaultextension='.txt',
filetypes=[('All files','.*'),
('Text Documents','.txt')])
# if window is closed without selecting a file.
# comparing it to none did not have effect as file is str type
# and has empty string when nothing is selected
if file == "" :
print("No file was selected")
return
try:
window.title(os.path.basename(file))
file = open(file, 'w')
file.write(text_area.get(1.0, END))
except Exception:
print("Could not save file")
finally:
file.close()
def copy():
text_area.event_generate("<<Copy>>") # dont know how this function works
def cut():
text_area.event_generate("<<Cut>>") # dont know how this function works
def paste():
text_area.event_generate("<<Paste>>") # dont know how this function works
def about():
# Using message box modue
howinfo("About this program","This is a text editor")
def close():
window.destroy()
window = Tk()
window.title("Text Editor")
window_height = 500
window_width = 500
# Screen display size of my PC
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
print(screen_width)
print(screen_height)
# To make the window appear in centre.
x = int((screen_width / 2) - (window_width / 2))
y = int((screen_height / 2) - (window_height / 2))
window.geometry('{}x{}+{}+{}'.format(window_width, window_height,x,y))
# default font selection
font_name = StringVar(window)
font_name.set('Arial')
font_size = StringVar(window)
font_size.set("25")
# adding text area to write and attach scroll bar to it.
text_area = Text(window, font=(font_name.get(), font_size.get()))
scrollbar = Scrollbar(text_area)
scrollbar.pack(side=RIGHT, fill=Y)
# adding scroll bar to our text area
text_area.config(yscrollcommand=scrollbar.set)
#This line is making text move by scroll.
scrollbar.config(command=text_area.yview, cursor='arrow')
# This will make text to span when window is expanded or compressed
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)
text_area.grid(sticky=N + E + S + W)
frame =Frame(window)
frame.grid()
# making color change , spin and font changing buttons
color_button = Button(frame, text="color", command=change_color)
color_button.grid(row=0,column=0)
# Option menu from font class
font_change = OptionMenu(frame, font_name, *font.families(),
command=change_font)
font_change.grid(row=0, column=1)
#spinbox for changing font size
size_box = Spinbox(frame, from_=1, to=100, textvariable=font_size,
command=change_font)
size_box.grid(row=0, column=2)
#menu tabs
menu_bar = Menu(window)
window.config(menu=menu_bar)
# File menu
file_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New File", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=close)
# Edit menu
edit_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="copy", command=copy)
edit_menu.add_command(label="paste", command=paste)
edit_menu.add_command(label="cut", command=cut)
edit_menu.add_separator()
# help menu
help_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label='help', menu=help_menu)
help_menu.add_command(label='About', command=about)
window.mainloop()
Problem picture:
For issue 1, you need to put the scrollbar in window
instead of text_area
:
text_area = Text(window, font=(font_name.get(), font_size.get()))
scrollbar = Scrollbar(window)
...
text_area.grid(row=0, column=0, sticky=N+E+S+W)
scrollbar.grid(row=0, column=1, sticky=N+S)
For issue 2, you can sort the list using sorted()
:
font_change = OptionMenu(frame, font_name, *sorted(font.families()),
command=change_font)