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)
self.root.mainloop()
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",
command=self.get_text)
self.buttonOK.grid(row=1, column=0)
self.buttonCancel = Button(master.root, text="NEVERMIND",
command=self.cancel)
self.buttonCancel.grid(row=1, column=1)
def get_text(self):
self.master.Text = self.textBox.get(1.0, END)
self.master.root.destroy()
def cancel(self):
self.master.root.destroy()
def get_big_string(text=""):
bs = big_string(text)
output = bs.Text
return output
The window:
Edit the text:
The output:
Written by Cloud Cray
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#