Create a simple GUI app that looks like a "text editor" using Python's Tkinter. We have incorporated a Text Widget and a Scrollbar to make it a class for easy reuse.
It's a simple "text editor" style app.
First import to use Tkinter, Python's standard GUI library, and a file dialog.
import tkinter as tk
from tkinter import filedialog
Create a text frame class with a scrollbar. The class is derived from the Frame to create text, vertical scrollbars and sidescroll bars and place them in the base frame using the grid.
class SbTextFrame(tk.Frame):
def __init__(self,master):
super().__init__(master)
text = tk.Text(self,wrap='none',undo=True)
x_sb = tk.Scrollbar(self,orient='horizontal')
y_sb = tk.Scrollbar(self,orient='vertical')
x_sb.config(command=text.xview)
y_sb.config(command=text.yview)
text.config(xscrollcommand=x_sb.set,yscrollcommand=y_sb.set)
text.grid(column=0,row=0,sticky='nsew')
x_sb.grid(column=0,row=1,sticky='ew')
y_sb.grid(column=1,row=0,sticky='ns')
self.columnconfigure(0,weight=1)
self.rowconfigure(0,weight=1)
self.text = text
self.x_sb = x_sb
self.y_sb = y_sb
Omit fileopen () and filesave () and jump to the explanation of main (). At the beginning of main (), create an application window and set the title and window size.
def main():
root = tk.Tk()
root.title('editor')
root.geometry('400x300')
Then create a text frame with scrollbars and place it all over the application window. By setting ʻexpand = True`, the size of the text frame will change according to the window size.
textframe = SbTextFrame(root)
textframe.pack(side='top',fill='both',expand=True)
Create a file menu and place it in the menu bar of the application window.
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar,tearoff=0)
filemenu.add_command(label='Open',command=fileopen)
filemenu.add_command(label='Save',command=filesave)
filemenu.add_command(label='Exit',command=exit)
menubar.add_cascade(label='File',menu=filemenu)
root.config(menu=menubar)
The whole source code is as follows.
import tkinter as tk
from tkinter import filedialog
class SbTextFrame(tk.Frame):
def __init__(self,master):
super().__init__(master)
text = tk.Text(self,wrap='none',undo=True)
x_sb = tk.Scrollbar(self,orient='horizontal')
y_sb = tk.Scrollbar(self,orient='vertical')
x_sb.config(command=text.xview)
y_sb.config(command=text.yview)
text.config(xscrollcommand=x_sb.set,yscrollcommand=y_sb.set)
text.grid(column=0,row=0,sticky='nsew')
x_sb.grid(column=0,row=1,sticky='ew')
y_sb.grid(column=1,row=0,sticky='ns')
self.columnconfigure(0,weight=1)
self.rowconfigure(0,weight=1)
self.text = text
self.x_sb = x_sb
self.y_sb = y_sb
def fileopen():
global fname,textframe,root
fname = filedialog.askopenfilename()
f = open(fname,'r')
lines = f.readlines()
f.close()
textframe.text.delete('1.0','end')
for line in lines:
textframe.text.insert('end',line)
root.title('editor - '+fname)
def filesave():
global fname,textframe
if fname == '':
return
f = open(fname,'w')
lines = textframe.text.get('1.0','end-1c')
f.writelines(lines)
f.close()
def main():
global fname,textframe,root
fname = ''
root = tk.Tk()
root.title('editor')
root.geometry('400x300')
textframe = SbTextFrame(root)
textframe.pack(side='top',fill='both',expand=True)
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar,tearoff=0)
filemenu.add_command(label='Open',command=fileopen)
filemenu.add_command(label='Save',command=filesave)
filemenu.add_command(label='Exit',command=exit)
menubar.add_cascade(label='File',menu=filemenu)
root.config(menu=menubar)
root.mainloop()
if __name__ == '__main__':
main()
This time I made a simple GUI application in Python. It was a bit annoying to add vertical and horizontal scrollbars to the text widget, so I tried to make it a class so that it can be reused in the future. I think it's useful when creating an app that makes heavy use of text widgets with vertical and horizontal scroll bars.
Recommended Posts