-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathformat_menu.py
125 lines (103 loc) Β· 4.65 KB
/
format_menu.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from tkinter import *
from tkinter.colorchooser import askcolor
from tkinter.font import Font, families
from tkinter.scrolledtext import *
import time
import sys
class Format():
def __init__(self, text):
self.text = text
def changeBg(self):
(triple, hexstr) = askcolor()
if hexstr:
self.text.config(bg=hexstr)
def changeFg(self):
(triple, hexstr) = askcolor()
if hexstr:
self.text.config(fg=hexstr)
def bold(self, *args): # Works only if text is selected
try:
current_tags = self.text.tag_names("sel.first")
if "bold" in current_tags:
self.text.tag_remove("bold", "sel.first", "sel.last")
else:
self.text.tag_add("bold", "sel.first", "sel.last")
bold_font = Font(self.text, self.text.cget("font"))
bold_font.configure(weight="bold")
self.text.tag_configure("bold", font=bold_font)
except:
pass
def italic(self, *args): # Works only if text is selected
try:
current_tags = self.text.tag_names("sel.first")
if "italic" in current_tags:
self.text.tag_remove("italic", "sel.first", "sel.last")
else:
self.text.tag_add("italic", "sel.first", "sel.last")
italic_font = Font(self.text, self.text.cget("font"))
italic_font.configure(slant="italic")
self.text.tag_configure("italic", font=italic_font)
except:
pass
def underline(self, *args): # Works only if text is selected
try:
current_tags = self.text.tag_names("sel.first")
if "underline" in current_tags:
self.text.tag_remove("underline", "sel.first", "sel.last")
else:
self.text.tag_add("underline", "sel.first", "sel.last")
underline_font = Font(self.text, self.text.cget("font"))
underline_font.configure(underline=1)
self.text.tag_configure("underline", font=underline_font)
except:
pass
def overstrike(self, *args): # Works only if text is selected
try:
current_tags = self.text.tag_names("sel.first")
if "overstrike" in current_tags:
self.text.tag_remove("overstrike", "sel.first", "sel.last")
else:
self.text.tag_add("overstrike", "sel.first", "sel.last")
overstrike_font = Font(self.text, self.text.cget("font"))
overstrike_font.configure(overstrike=1)
self.text.tag_configure("overstrike", font=overstrike_font)
except:
pass
def addDate(self):
full_date = time.localtime()
day = str(full_date.tm_mday)
month = str(full_date.tm_mon)
year = str(full_date.tm_year)
date = day + '/' + month + '/' + year
self.text.insert(INSERT, date, "a")
def main(root, text, menubar):
objFormat = Format(text)
fontoptions = families(root)
font = Font(family="Arial", size=10)
text.configure(font=font)
formatMenu = Menu(menubar)
fsubmenu = Menu(formatMenu, tearoff=0)
ssubmenu = Menu(formatMenu, tearoff=0)
for option in fontoptions:
fsubmenu.add_command(label=option, command=lambda option=option: font.configure(family=option))
for value in range(1, 31):
ssubmenu.add_command(label=str(value), command=lambda value=value: font.configure(size=value))
formatMenu.add_command(label="Change Background", command=objFormat.changeBg)
formatMenu.add_command(label="Change Font Color", command=objFormat.changeFg)
formatMenu.add_cascade(label="Font", underline=0, menu=fsubmenu)
formatMenu.add_cascade(label="Size", underline=0, menu=ssubmenu)
formatMenu.add_command(label="Bold", command=objFormat.bold, accelerator="Ctrl+B")
formatMenu.add_command(label="Italic", command=objFormat.italic, accelerator="Ctrl+I")
formatMenu.add_command(label="Underline", command=objFormat.underline, accelerator="Ctrl+U")
formatMenu.add_command(label="Overstrike", command=objFormat.overstrike, accelerator="Ctrl+T")
formatMenu.add_command(label="Add Date", command=objFormat.addDate)
menubar.add_cascade(label="Format", menu=formatMenu)
root.bind_all("<Control-b>", objFormat.bold)
root.bind_all("<Control-i>", objFormat.italic)
root.bind_all("<Control-u>", objFormat.underline)
root.bind_all("<Control-T>", objFormat.overstrike)
root.grid_columnconfigure(0, weight=1)
root.resizable(True, True)
root.config(menu=menubar)
if __name__ == "__main":
print("Please run 'main.py'")