Last Updated: February 25, 2016
· cloudcray

Python - Minimal GUI window for large text scripting

I do a lot of scripting for quick data analysis. Oftentimes, I need to copy a large amount of text into my script or program, but I don't feel like going through all the escaping, formatting, etc. This is most useful when I am designing a query in a text editor with syntax highlighting, and I need to just copy the text over. Here's a quick class and function in python that creates a gui textbox using tkinter. Calling "y = getbigstring(x)" calls up the window, which contains the text in "x". Selecting "Got it" will return whatever text you entered and store it in y. Examples below.

I keep mine in a module called "convert", but it's up to you where you want to place it.

from tkinter import *

class big_string:

    def __init__(self, text=""):
        self.root = Tk()
        self.Text = text
        self.window = self.text_window(self, self.Text)

    class text_window:

        def __init__(self, master, text):
            self.master = master

            self.textBox = Text(master.root)
            self.textBox.insert(END, text)
            self.textBox.grid(row=0, sticky=NSEW, columnspan=2)
            self.master.root.rowconfigure(0, weight=1)
            self.master.root.columnconfigure(0, weight=1)

            self.buttonOK = Button(master.root, text="GOT IT",
            self.buttonOK.grid(row=1, column=0)

            self.buttonCancel = Button(master.root, text="NEVERMIND",
            self.buttonCancel.grid(row=1, column=1)

        def get_text(self):
            self.master.Text = self.textBox.get(1.0, END)

        def cancel(self):

def get_big_string(text=""):
    bs = big_string(text)
    output = bs.Text
    return output

The window:


Edit the text:


The output: