forked from Techsrijan/mpsummer25
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_in_tkinter.py
More file actions
78 lines (64 loc) · 1.84 KB
/
text_in_tkinter.py
File metadata and controls
78 lines (64 loc) · 1.84 KB
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
from tkinter import *
from tkinter import filedialog,messagebox,colorchooser
from turtledemo.colormixer import setbgcolor
root=Tk()
def back():
c=colorchooser.askcolor()
t.config(background=c[1])
print(c)
def fore():
c = colorchooser.askcolor()
t.config(foreground=c[1])
print(c)
def get_data():
print(t.get(1.0,END))
def open_file():
global current_open_file
f=filedialog.askopenfile(initialdir="/",
filetypes=[('All Files','*.*'),('Text File','*.txt')])
current_open_file=f
print(f)
for data in f:
t.insert(INSERT,data)
def save_file():
s=open(current_open_file.name,'w')
data=t.get(1.0,END)
s.write(data)
def saveAs_file():
d=filedialog.asksaveasfile(defaultextension=".txt",mode='w')
print(d)
data = t.get(1.0, END)
d.write(data)
def new_file():
x = t.get(1.0, END)
if x.strip()=='':
pass
else:
print(x)
res = messagebox.askyesnocancel("Save File Confirmation", 'Do you want to Save this file?')
if res==True:
saveAs_file()
elif res==False:
t.delete(1.0, END)
t=Text(root,width=15,wrap=WORD,selectbackground='red')
# t.insert(INSERT,'hello how ar you\n')
# t.insert(INSERT,'i am fine')
# t.pack(fill=BOTH,expand=1)
t.pack()
btn_back=Button(root,text="background",command=back)
btn_back.pack()
btn_fore=Button(root,text="foreground",command=fore)
btn_fore.pack()
btn_new=Button(root,text="new",command=new_file)
btn_new.pack()
btn=Button(root,text="Clickme",command=get_data)
btn.pack()
btn1=Button(root,text="open file",command=open_file)
btn1.pack()
btn_save=Button(root,text="Save file",command=save_file)
btn_save.pack()
btn_saveAs=Button(root,text="Save AS file",command=saveAs_file)
btn_saveAs.pack()
root.geometry("600x800+200+100")
# root.resizable(0,0)
root.mainloop()