- 论坛徽章:
- 0
|
回复 #4 asnama 的帖子
#!/usr/local/bin/python
from Tkinter import *
from ScrolledText import*
import tkMessageBox
from tkFileDialog import *
import fileinput
tl = []
root = None
class editor:
def __init__(self,rt):
if rt == None:
self.t = Tk()
else:
self.t = Toplevel(rt)
self.t.title("Tkeditor %d" % len(tl))
self.bar = Menu(rt)
self.filem = Menu(self.bar)
self.filem.add_command(label="Open...",command=self.openfile)
self.filem.add_command(label="New...",command=neweditor)
self.filem.add_command(label="Save as...",command=self.savefile)
self.filem.add_command(label="Close",command=self.close)
self.filem.add_separator()
self.filem.add_command(label="Exit",command=self.die)
self.helpm = Menu(self.bar)
self.filem.add_command(label="About",command=self.about)
self.bar.add_cascade(label="File",menu=self.filem)
self.bar.add_cascade(label="Help",menu=self.helpm)
self.t.config(menu=self.bar)
self.f = Frame(self.t,width=512)
self.f.pack(expand=1,fill=BOTH)
self.st = ScrolledText(self.f,background="white")
self.st.pack(side=LEFT,fill=BOTH,expand=1)
def close(self):
self.t.destroy()
def openfile(self):
pl = END
oname = askopenfilename(filetypes=(("python files","*.py")))
if oname:
for line in fileinput.input(oname):
self.st.insert(pl,line)
self.t.title(oname)
def savefile(self):
sname = asksaveasfilename()
if sname:
ofp = open(sname,"w")
ofp.write(self.st.get(1.0,END))
ofp.flush()
ofp.close()
self.t.title(sname)
def about(self):
tkMessageBox.showinfo("Tkeditor","test\n" "2007.09.16\n" "author:rubee")
def die(self):
sys.exit(0)
def neweditor(self):
global root
tl.append(editor(root))
if __name__== "__main__":
root = None
tl.append (editor(root))
root = tl[0].t
root.mainloop()
给你调好了,运行一下,看能得到你要的结果不? |
|