paste text with newlines into raw_input?
BartlebyScrivener
bscrivener42 at gmail.com
Thu May 31 18:17:32 EDT 2007
Hi,
I'm going to post this here in case somebody else searches for an
example Tkinter Text Widget for entering multiline text. I don't like
GUI and don't even quite understand how it works, but it seems to
work. In my case it's part of a program for pasting a quote from the
clipboard into a MySQL database (hence the separate paste button).
I also don't know OO and Classes. If someone wants to wrap it in a
class and repost it could save the free world.
Or suggestions for making it better much appreciated.
Thanks,
Rick
---------------------------------
#! /usr/bin/python
import Tkinter
import tkFont
"""
Tkinter Text Widget for entering multline text.
Rick Dooling http://dooling.com
Based on:
Alan Gauld's "GUI Programming with Tkinter"
http://www.freenetpages.co.uk/hp/alan.gauld/tutgui.htm
Jeff Eppler's clp post - 3 August 2005
"cut and paste text between Tkinter widgets"
http://tinyurl.com/2d97gj
"""
# the first two functions come from Jeff Eppler's post
def make_menu(w):
global the_menu
the_menu = Tkinter.Menu(w, tearoff=0)
the_menu.add_command(label="Cut")
the_menu.add_command(label="Copy")
the_menu.add_command(label="Paste")
def show_menu(e):
w = e.widget
the_menu.entryconfigure("Cut",
command=lambda: w.event_generate("<<Cut>>"))
the_menu.entryconfigure("Copy",
command=lambda: w.event_generate("<<Copy>>"))
the_menu.entryconfigure("Paste",
command=lambda: w.event_generate("<<Paste>>"))
the_menu.tk.call("tk_popup", the_menu, e.x_root, e.y_root)
def evClear():
eText.delete(0.0,Tkinter.END)
def assign():
# get text from the text widget and assign it to Quote
Quote = eText.get(0.0, Tkinter.END)
# just for testing the assignment
print Quote
def paste():
eText.event_generate("<<Paste>>")
t = Tkinter.Tk()
# create the top level window/frame
F = Tkinter.Frame(t)
F.master.title("Enter Quote ")
F.pack(expand="true")
myfont = tkFont.Font(family="Courier", size=14)
# frame for message to the troops
fMessage = Tkinter.Frame(F, border=1)
fMessage.pack(side="top", expand="true")
lMessage = Tkinter.Label(fMessage, text="Paste your quote into the
Text Box from the clipboard, or type it in. When you are finished,
click Enter.")
lMessage.pack(expand="true")
# frame for text entry field
fText = Tkinter.Frame(F, border=1)
fText.pack(side="top", expand="true")
# the text widget
eText = Tkinter.Text(fText, width= 75, height=20, font=myfont,
wrap=Tkinter.WORD); eText.pack(side="top")
eText.bind_class("Text", "<Button-3><ButtonRelease-3>", show_menu)
# frame with the buttons
fButtons = Tkinter.Frame(F, relief="groove", border=3)
# the buttons
bPaste = Tkinter.Button(fButtons, text="Paste", command=paste)
bPaste.pack(side="left", padx=15, pady=4)
bEnter = Tkinter.Button(fButtons, text="Enter", command=assign)
bEnter.pack(side="left", padx=15, pady=4)
bClear = Tkinter.Button(fButtons, text="Clear Text", command=evClear)
bClear.pack(side="left", padx=15, pady=4)
bQuit = Tkinter.Button(fButtons, text="Quit", command=F.quit)
bQuit.pack(side="left", padx=15, pady=4)
# pack them
fButtons.pack(side="bottom", expand="true")
make_menu(t)
t.mainloop()
More information about the Python-list
mailing list