记事本源代码 python3

已实现基本功能,显示行号功能暂时实现不了(后面学会了再加,右下角可以实现定位光标所在行.列) 可能会有些bug

  1 from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
from threading import Timer
import string #定义一个添加菜单的类,想加什么菜单直接调用即可,副作用是没法加分隔线
class menuNameAccCom:
def __init__(self,menuname,menucom,menuacc):
self.menuname=menuname
self.menuacc=menuacc
self.menucom=menucom def addmenu(self,wigetName):
for (name,com,acc) in (zip(self.menuname,self.menucom,self.menuacc)):
wigetName.add_command(label=name,accelerator=acc,command=com) filename=''
def openfile():
global filename
filename = askopenfilename(defaultextension='.txt')
if filename == '':
filename = None
else:
root.title('FileName:'+os.path.basename(filename))
textPad.delete(1.0,END)#delete all
f = open(filename,'r')
textPad.insert(1.0,f.read())
f.close() def newfile():
global FileName
root.title('new file')
filename = None
textPad.delete(1.0,END) def savefile():
global filename
try:
f = open(filename,'w')
msg = textPad.get(1.0,END)
f.write(msg)
f.close()
except:
saveas() def saveas():
global filename
f = asksaveasfilename(initialfile = 'newfile',defaultextension ='.txt')
filename = f
fh = open(f,'w')
msg = textPad.get(1.0,END)
fh.write(msg)
fh.close()
root.title('FileName:'+os.path.basename(f)) def cut():
textPad.event_generate('<<Cut>>') def copy():
textPad.event_generate('<<Copy>>') def paste():
textPad.event_generate('<<Paste>>') def redo():
textPad.event_generate('<<Redo>>') def undo():
textPad.event_generate('<<Undo>>') def selectall():
textPad.tag_add('sel',1.0,END) def search():
topsearch=Toplevel(root)
topsearch.geometry('300x30+200+250')
labell=Label(topsearch,text='find')
labell.grid(row=0,column=0,padx=5)
entry1=Entry(topsearch,width=28)
entry1.grid(row=0,column=1,padx=5)
button1=Button(topsearch,text='find')
button1.grid(row=0,column=2) def addButton(name,command):
for (toolname ,toolcom) in zip(name,command):
shortButton = Button(toolbar,text=toolname,relief='groove',command=toolcom)
shortButton.pack(side=LEFT,padx=2,pady=5) root = Tk()
root.title('Andy Note')
root.geometry('800x500+100+100') menubar = Menu(root)
root.config(menu= menubar) #文件菜单
filemenu = Menu(menubar,tearoff=False)
filemenuName = ('New','Open','Save','Save as')
filemenuAcc = ('Ctrl+N','Ctrl+O','Ctrl+S','Ctrl+Shift+S')
filemenuCom = (newfile,openfile,savefile,saveas) filem = menuNameAccCom(filemenuName,filemenuCom,filemenuAcc)#调用添加菜单的类
filem.addmenu(filemenu)
menubar.add_cascade(label='File',menu=filemenu) #编辑菜单
editmenu = Menu(menubar,tearoff=False)
editmenuName = ('Undo','Redo','Cut','Copy','Paste','Select All')
editmenuAcc = ('Ctrl+Z','Ctrl+Y','Ctrl+X','Ctrl+C','Ctrl+V','Ctrl+A')
editmenuCom = (undo,redo,cut,copy,paste,selectall) editm = menuNameAccCom(editmenuName,editmenuCom,editmenuAcc)#调用添加菜单的类
editm.addmenu(editmenu)
menubar.add_cascade(label='Edit',menu=editmenu) findmenu = Menu(menubar,tearoff=False)
findmenu.add_command(label='Find',accelerator='Ctrl+F',command=search)
menubar.add_cascade(label='Find',menu=findmenu) #按钮
toolbar = Frame(root,height=20)
toolbarName = ('New','Open','Save','SaveAs','Undo','Redo','Cut','Copy','Paste','SelectAll')
toolbarCommand = (newfile,openfile,savefile,saveas,undo,redo,cut,copy,paste,selectall) addButton(toolbarName,toolbarCommand) #调用添加按钮的函数
toolbar.pack(fill=X) text = Text(root,width=4,wrap=CHAR,height=1)
text.pack(side=LEFT,fill=Y) textPad = Text(root,undo=True)
textPad.pack(expand=YES,fill=BOTH)
textPad.focus_set() scroll = Scrollbar(textPad)
textPad.config(yscrollcommand=scroll.set)
scroll.config(command=textPad.yview)
scroll.pack(side=RIGHT,fill=Y) timer_interval = 1
def getline():
global t,row
row,col = textPad.index(INSERT).split('.')
lineNum = 'Ln: ' +row+' '+'Co: '+col
var.set(lineNum) t = Timer(timer_interval,getline)
t.start()
t = Timer(timer_interval,getline)
t.start() var = StringVar()
status = Label(root,anchor=E,height=1,text='Ln',relief=FLAT,takefocus=False,textvariable=var,padx=2)
status.pack(fill=X) mainloop()
上一篇:【转】通过Navicat for MySQL远程连接的时候报错mysql 1130的解决方法


下一篇:WPF异常“调用线程无法访问此对象,因为另一个线程拥有该对象 ”