已实现基本功能,显示行号功能暂时实现不了(后面学会了再加,右下角可以实现定位光标所在行.列) 可能会有些bug
1 from tkinter import *
2 from tkinter.messagebox import *
3 from tkinter.filedialog import *
4 from threading import Timer
5 import string
6
7 #定义一个添加菜单的类,想加什么菜单直接调用即可,副作用是没法加分隔线
8 class menuNameAccCom:
9 def __init__(self,menuname,menucom,menuacc):
10 self.menuname=menuname
11 self.menuacc=menuacc
12 self.menucom=menucom
13
14 def addmenu(self,wigetName):
15 for (name,com,acc) in (zip(self.menuname,self.menucom,self.menuacc)):
16 wigetName.add_command(label=name,accelerator=acc,command=com)
17
18 filename=''
19 def openfile():
20 global filename
21 filename = askopenfilename(defaultextension='.txt')
22 if filename == '':
23 filename = None
24 else:
25 root.title('FileName:'+os.path.basename(filename))
26 textPad.delete(1.0,END)#delete all
27 f = open(filename,'r')
28 textPad.insert(1.0,f.read())
29 f.close()
30
31 def newfile():
32 global FileName
33 root.title('new file')
34 filename = None
35 textPad.delete(1.0,END)
36
37 def savefile():
38 global filename
39 try:
40 f = open(filename,'w')
41 msg = textPad.get(1.0,END)
42 f.write(msg)
43 f.close()
44 except:
45 saveas()
46
47 def saveas():
48 global filename
49 f = asksaveasfilename(initialfile = 'newfile',defaultextension ='.txt')
50 filename = f
51 fh = open(f,'w')
52 msg = textPad.get(1.0,END)
53 fh.write(msg)
54 fh.close()
55 root.title('FileName:'+os.path.basename(f))
56
57 def cut():
58 textPad.event_generate('<<Cut>>')
59
60 def copy():
61 textPad.event_generate('<<Copy>>')
62
63 def paste():
64 textPad.event_generate('<<Paste>>')
65
66 def redo():
67 textPad.event_generate('<<Redo>>')
68
69 def undo():
70 textPad.event_generate('<<Undo>>')
71
72 def selectall():
73 textPad.tag_add('sel',1.0,END)
74
75 def search():
76 topsearch=Toplevel(root)
77 topsearch.geometry('300x30+200+250')
78 labell=Label(topsearch,text='find')
79 labell.grid(row=0,column=0,padx=5)
80 entry1=Entry(topsearch,width=28)
81 entry1.grid(row=0,column=1,padx=5)
82 button1=Button(topsearch,text='find')
83 button1.grid(row=0,column=2)
84
85 def addButton(name,command):
86 for (toolname ,toolcom) in zip(name,command):
87 shortButton = Button(toolbar,text=toolname,relief='groove',command=toolcom)
88 shortButton.pack(side=LEFT,padx=2,pady=5)
89
90 root = Tk()
91 root.title('Andy Note')
92 root.geometry('800x500+100+100')
93
94 menubar = Menu(root)
95 root.config(menu= menubar)
96
97 #文件菜单
98 filemenu = Menu(menubar,tearoff=False)
99 filemenuName = ('New','Open','Save','Save as')
100 filemenuAcc = ('Ctrl+N','Ctrl+O','Ctrl+S','Ctrl+Shift+S')
101 filemenuCom = (newfile,openfile,savefile,saveas)
102
103 filem = menuNameAccCom(filemenuName,filemenuCom,filemenuAcc)#调用添加菜单的类
104 filem.addmenu(filemenu)
105 menubar.add_cascade(label='File',menu=filemenu)
106
107 #编辑菜单
108 editmenu = Menu(menubar,tearoff=False)
109 editmenuName = ('Undo','Redo','Cut','Copy','Paste','Select All')
110 editmenuAcc = ('Ctrl+Z','Ctrl+Y','Ctrl+X','Ctrl+C','Ctrl+V','Ctrl+A')
111 editmenuCom = (undo,redo,cut,copy,paste,selectall)
112
113 editm = menuNameAccCom(editmenuName,editmenuCom,editmenuAcc)#调用添加菜单的类
114 editm.addmenu(editmenu)
115 menubar.add_cascade(label='Edit',menu=editmenu)
116
117 findmenu = Menu(menubar,tearoff=False)
118 findmenu.add_command(label='Find',accelerator='Ctrl+F',command=search)
119 menubar.add_cascade(label='Find',menu=findmenu)
120
121 #按钮
122 toolbar = Frame(root,height=20)
123 toolbarName = ('New','Open','Save','SaveAs','Undo','Redo','Cut','Copy','Paste','SelectAll')
124 toolbarCommand = (newfile,openfile,savefile,saveas,undo,redo,cut,copy,paste,selectall)
125
126 addButton(toolbarName,toolbarCommand) #调用添加按钮的函数
127 toolbar.pack(fill=X)
128
129
130 text = Text(root,width=4,wrap=CHAR,height=1)
131 text.pack(side=LEFT,fill=Y)
132
133 textPad = Text(root,undo=True)
134 textPad.pack(expand=YES,fill=BOTH)
135 textPad.focus_set()
136
137 scroll = Scrollbar(textPad)
138 textPad.config(yscrollcommand=scroll.set)
139 scroll.config(command=textPad.yview)
140 scroll.pack(side=RIGHT,fill=Y)
141
142 timer_interval = 1
143 def getline():
144 global t,row
145 row,col = textPad.index(INSERT).split('.')
146 lineNum = 'Ln: ' +row+' '+'Co: '+col
147 var.set(lineNum)
148
149 t = Timer(timer_interval,getline)
150 t.start()
151 t = Timer(timer_interval,getline)
152 t.start()
153
154 var = StringVar()
155 status = Label(root,anchor=E,height=1,text='Ln',relief=FLAT,takefocus=False,textvariable=var,padx=2)
156 status.pack(fill=X)
157
158 mainloop()