[Tkinter-discuss] Text and newlines [newbie Q]
Michael Lange
klappnase at web.de
Mon Jan 3 18:14:08 CET 2011
Hi,
Thus spoketh <spooky.ln at tbs-software.com>
unto us on Mon, 3 Jan 2011 13:50:44 +0100:
> hi all,
> i have little method for parsing line in tkinter.Text.
> text.bind(‘<Return>’, parse)
> def parse(event=None):
> text = get_current_line() #
> this give me whole text on line pattern = r”^.*define\s<?
> P<defkeyword>.*):$” # simple search pattern reg = re.compile(pattern,
> re.S|re.M) # for use with more patterns match =
> re.search(reg, text) if match:
> print(‘found %s definition.’ % match.group(‘defkeyword’))
> text.insert(‘end’, ‘\n’)
> text.insert(‘insert linestart’, ‘\t’)
> there is small problem for me. how to insert tab into newline without
> next ‘\n’ ? i want only 2 lines not 3.
> i want:
> 1. define some:
> 2. .......[next insert will be here]
> and not:
> 1. define some:
> 2. .......
> 3. [now ‘end’ points here]
I am not sure what exactly you mean, simply changing your example into
if match:
print(‘found %s definition.’ % match.group(‘defkeyword’))
text.insert(‘end’, ‘\t’)
does probably not what you want?
> and second how i define width of tab in chars not in pixels using
> text.config(tabs=X) thanks.
This is explained in the text man page:
To achieve a different standard spacing, for example every 4
characters, simply configure the widget with ``-tabs "[expr {4 * [font
measure $font 0]}] left" -tabstyle wordprocessor''.
"Translated" into python this would be something like:
from Tkinter import *
import tkFont
root=Tk()
f = tkFont.Font(family='courier', size=-12)
text = Text(root, font=f, tabs=(4 * f.measure(0), 'left'), tabstyle='wordprocessor')
text.pack(fill=BOTH, expand=1)
root.mainloop()
Obviously f.measure(0) returns the average width of a character, although
this behavior doesn't seem to be documented.
Regards
Michael
.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.
Only a fool fights in a burning house.
-- Kank the Klingon, "Day of the Dove", stardate unknown
More information about the Tkinter-discuss
mailing list