Easy GUI app with Tkinter Text

Introduction

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.

Complete image

It's a simple "text editor" style app. editor.gif

Commentary

import

First import to use Tkinter, Python's standard GUI library, and a file dialog.

import tkinter as tk
from tkinter import filedialog

Text frame class

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

Application window

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')

Text placement

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)

Menu creation

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)

Whole source code

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()

in conclusion

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

Easy GUI app with Tkinter Text
Make GUI apps super easy with tkinter
Create a GUI app with Python's Tkinter
Create a native GUI app with Py2app and Tkinter
Creating an image splitting app with Tkinter
Easy web app with Python + Flask + Heroku
MVC with Tkinter
Easy Raspberry Pi GUI App Development Beginner Part 1
Create a GUI executable file created with tkinter
GUI image cropping tool made with Python + Tkinter
GUI automation with Python x Windows App Driver
Easy Raspberry Pi GUI App Development Beginner Part 2
Upload images to S3 with GUI using tkinter
Easy Grad-CAM with pytorch-gradcam
Easy machine learning with scikit-learn and flask ✕ Web app
Tab app in Tkinter
Become Santa with Tkinter
linux with termux app
Text mining with Python-Scraping-
Let's make a Mac app with Tkinter and py2app
Pythonbrew with Sublime Text
[Tkinter] Improve GUI responsiveness
Easy debugging with ipdb
Easy TopView with OpenCV
Creating a GUI as easily as possible with python [tkinter edition]
I tried to make GUI tic-tac-toe with Python and Tkinter
Easy deep learning web app with NNC and Python + Flask
Programming with Python and Tkinter
Todo app with django-bootstrap-modal-forms plugin
Easy tox environment with Jenkins
Getting Started with Tkinter 2: Buttons
[Co-occurrence analysis] Easy co-occurrence analysis with Python! [Python]
Easy folder synchronization with Python
Screen switching / screen transition with Tkinter
Easy image classification with TensorFlow
Easy web scraping with Scrapy
Create Image Viewer with Tkinter
[GUI with Python] PyQt5-Layout management-
Let's make dice with tkinter
Run Label with tkinter [Python]
Easy Python compilation with NUITKA-Utilities
Easy HTTP server with Python
Easy proxy login with django-hijack
I measured BMI with tkinter
[GUI with Python] PyQt5 -Preparation-
Text sentiment analysis with ML-Ask
Play with A3RT (Text Suggest)
[GUI with Python] PyQt5 -Paint-
Azure Monitor determines both cloud monitoring and on-premise monitoring! Easy with GUI!