python3写的记事本
本帖最后由 小小大鱼 于 2016-3-19 14:49 编辑之前看到有人发了记事本,而且还更新,最近在学tkinter,然后也摸索着写了一个,功能相当简陋,有些功能暂时还实现不了......而且还很丑。
完整代码在下面......
3.19日更新:
本来是3.18更新,结果恢复了,什么也没有,今天重新更新。
1.实现左侧显示行号功能
2.右下角显示当前光标位置
3.取消代码隐藏
还有一些bug,也希望大家能帮我指出来,帮我改进。
比如:RuntimeError: main thread is not in main loop这个错误还不知道怎么处理。
没能实现滚动条
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)
# line_number
v1= StringVar()
line =Label(root, widt=4, font ='consolas', justify=RIGHT, anchor=N, textvariable=v1, text ='1')
line.pack(side=LEFT,fill=Y)
textPad = Text(root,undo=True, font ='consolas')
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()
r,l = textPad.index(END).split('.')
line_n =
s = ''
for i in line_n:
s = s + i
v1.set(s)
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)
if __name__ == '__main__':
mainloop()
看看 python3写的记事本 学习一下 先复制 再学习一下~~ 谢了~ 666666666666 观摩一下{:5_109:} 学习学习 {:5_107:} 代码上的颜色字体,酷炫啊! 欣赏下大神写的代码 {:7_139:}这么厉害 学习下 不爱穿内裤 发表于 2016-2-1 09:23
代码上的颜色字体,酷炫啊!
用的sublime text3 拈花小仙 发表于 2016-2-1 17:38
这么厉害
慢慢学的,有些功能还实现不了,学习中.... {:10_277:学习下 6666楼主牛b 看看学习下 回复看看 哈哈
页:
[1]
2