2013-02-09 16 views

Odpowiedz

13

Główną ideą jest zastosowanie tagów na fragmenty tekstu, które chcesz dostosować. Możesz utworzyć znaczniki przy użyciu metody tag_configure, z określonym stylem, a następnie wystarczy zastosować ten znacznik do części tekstu, którą chcesz zmienić, używając metody tag_add. Możesz również usunąć tagi, używając metody tag_remove.

Poniżej przedstawiono przykład użycia metod tag_configure, tag_add i tag_remove.

#!/usr/bin/env python3 

import tkinter as tk 
from tkinter.font import Font 

class Pad(tk.Frame): 

    def __init__(self, parent, *args, **kwargs): 
     tk.Frame.__init__(self, parent, *args, **kwargs) 

     self.toolbar = tk.Frame(self, bg="#eee") 
     self.toolbar.pack(side="top", fill="x") 

     self.bold_btn = tk.Button(self.toolbar, text="Bold", command=self.make_bold) 
     self.bold_btn.pack(side="left") 

     self.clear_btn = tk.Button(self.toolbar, text="Clear", command=self.clear) 
     self.clear_btn.pack(side="left") 

     # Creates a bold font 
     self.bold_font = Font(family="Helvetica", size=14, weight="bold") 

     self.text = tk.Text(self) 
     self.text.insert("end", "Select part of text and then click 'Bold'...") 
     self.text.focus() 
     self.text.pack(fill="both", expand=True) 

     # configuring a tag called BOLD 
     self.text.tag_configure("BOLD", font=self.bold_font) 

    def make_bold(self): 
     # tk.TclError exception is raised if not text is selected 
     try: 
      self.text.tag_add("BOLD", "sel.first", "sel.last")   
     except tk.TclError: 
      pass 

    def clear(self): 
     self.text.tag_remove("BOLD", "1.0", 'end') 


def demo(): 
    root = tk.Tk() 
    Pad(root).pack(expand=1, fill="both") 
    root.mainloop() 


if __name__ == "__main__": 
    demo() 

Jeśli nie wiesz, co sel.first i sel.last są, sprawdź this post lub this odniesienia.

+0

co jeśli muszę zrobić to samo z elementem tekstowym? – Kay

4

Wystarczy popatrzeć na ten przykład:

from tkinter import * 

root = Tk() 

text = Text(root) 
text.insert(INSERT, "Hello, world!\n") 
text.insert(END, "This is a phrase.\n") 
text.insert(END, "Bye bye...") 
text.pack(expand=1, fill=BOTH) 

# adding a tag to a part of text specifying the indices 
text.tag_add("start", "1.8", "1.13") 
text.tag_config("start", background="black", foreground="yellow") 

root.mainloop() 
+2

tag_add (zmienna, startindex [, endindex] ...) Ta metoda oznacza pozycję zdefiniowaną przez startindex lub zakres ograniczony przez pozycje startindex i endindex. –

+0

Rzecz w tym, że 1.8 i 1.13 to miejsce, w którym tekst ma zmienić kolor, kiedy ten tekst pojawi się – Elxafil

4

Zrobiłem klienta IRC. Podświetliłem niektóre części konwersacji przy użyciu niestandardowego, dość łatwego w użyciu widgetu Text, który umożliwia stosowanie znaczników za pomocą wyrażeń regularnych. Został oparty na następującym wpisie: How to highlight text in a tkinter Text widget.

Tu masz przykład użycia:

# "text" is a Tkinter Text 

# configuring a tag with a certain style (font color) 
text.tag_configure("red", foreground="red") 

# apply the tag "red" 
text.highlight_pattern("word", "red") 
Powiązane problemy