-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlight_selected_part_of_text.py
65 lines (43 loc) · 2.37 KB
/
highlight_selected_part_of_text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Highlight Selected Part of Text
import tkinter
from tkinter import *
import tkinter as tk
import tkinter.messagebox as mbox
window = Tk()
window.geometry("1000x700")
window.title("Highlight Selected Part of Text")
class highlight_fun(tk.Frame):
def __init__(self, parent, *args, **kwargs):
hightlightb = Button(window, text="HIGHLIGHT",command=self.highlight_text,font=("Arial", 20), bg = "light green", fg = "blue", borderwidth=3, relief="raised")
hightlightb.place(x =100 , y =610 )
unhightlightb = Button(window, text="UN HIGHLIGHT", command=self.unhightlight_text, font=("Arial", 20), bg="light green", fg="blue", borderwidth=3, relief="raised")
unhightlightb.place(x=320, y=610)
clearb = Button(window, text="CLEAR", command=self.clear_text, font=("Arial", 20),bg="light green", fg="blue", borderwidth=3, relief="raised")
clearb.place(x=600, y=610)
tk.Frame.__init__(self, parent, *args, **kwargs)
self.text = tk.Text(self,height=17, width=70, font=("Arial", 15), bg="light yellow", fg="brown",borderwidth=3, relief="solid")
self.text.pack(fill="both", expand=True)
self.text.tag_configure("start", background="green", foreground="red")
def highlight_text(self):
try:
self.text.tag_add("start", "sel.first", "sel.last")
except tk.TclError:
pass
def unhightlight_text(self):
self.text.tag_remove("start", "1.0", 'end')
def clear_text(self):
self.text.delete('1.0', END)
# top label
start1 = tk.Label(text = "HIGHLIGHT SELECTED TEXT", font=("Arial", 50), fg="magenta",underline=0) # same way bg
start1.place(x = 50, y = 10)
note1 = tk.Label(text = "Enter Any Paragraph in Text Area, select any text and click on HIGHLIGHT Button.\nAnd to un-highlight the highlighted text, click on UN HIGHLIGHT button.\nTo clear the entered text, click on CLEAR button.", font=("Arial", 18), fg="brown") # same way bg
note1.place(x = 50, y = 510)
# highlight_text(window).pack(expand=1, fill="both")
highlight_fun(window).place(x = 100, y = 100)
def exit_win():
if mbox.askokcancel("Exit", "Do you want to exit?"):
window.destroy()
exitb = Button(window, text="EXIT",command=exit_win,font=("Arial", 20), bg = "red", fg = "blue", borderwidth=3, relief="raised")
exitb.place(x =780 , y =610 )
window.protocol("WM_DELETE_WINDOW", exit_win)
window.mainloop()