-----Původní zpráva----- From: Michael Lange
Sent: Monday, January 03, 2011 6:14 PM
To: tkinter-discuss@python.org
Subject: Re: [Tkinter-discuss] Text and newlines [newbie Q]

thanks for Tab info.
But in insertion tabs im with no luck.

im binding Return event into parse method where i parse line for regexp.
if regexp found i want insert some indent using tabs or spaces on new line.
but with edit.insert('end', '\t') tab is inserted right after ':' on the same line[1]. if i use insert('end', '\n\t') tabs are on new line[2] [ this is i want] but cursor is on next line[3].

nevermind here's example:

import re
from tkinter import *
from tkinter.font import Font

def get_current_line():
   start = text.index('insert linestart')
   end   = text.index('insert')
   return text.get(start, end)

def parse(event=None):
   line = get_current_line()
   match = re.search(reg, line)
   if match:
       print('match!', match.group('defkey'))
       text.insert('end', '\t')

root=Tk()
f = Font(family='courier', size=-12)
text = Text(root, font=f, tabs=(4 * f.measure(0), 'left'), tabstyle='wordprocessor')
text.pack(fill=BOTH, expand=1)
text.bind('<Return>', parse)
text.focus()
pattern  = r"^.*define\s(?P<defkey>.*):$"
reg = re.compile(pattern, re.S|re.M)

root.mainloop()

i'm on python 3.1.3 and win7.


Hi,
Thus spoketh <spooky...@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
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to