文本编辑器

<<Python核心编程>>第三章最后一题的答案... 
Windows,和类Unix支持。 

fobj_w.writelines(save_data.encode('utf-8')) # 感谢 OSC-骠骑将军指教
标签: <无>

代码片段(1)

[代码] [Python]代码

01 #!/usr/bin/env python
02 #-*- coding: utf-8 -*-
03 #=============================================================================
04 #     FileName:
05 #         Desc:
06 #       Author: ToughGuy
07 #        Email: wj0630@gmail.com
08 #     HomePage: http://www.techzhai.net/
09 #      Version: 0.0.1
10 #   LastChange: 2013-02-20 14:52:11
11 #      History:
12 #=============================================================================
13  
14 from Tkinter import *
15 import tkMessageBox,tkFileDialog
16 import platform
17  
18 # nl = os.linesep
19  
20 def openfile():
21     global filename             # 使用global声明为全局变量,方便后边的程序调用
22     systype = platform.system() # 判断系统类型
23     if systype == 'windows':
24         basedir = 'c:\\'
25     else:
26         basedir = '/'
27     filename = tkFileDialog.askopenfilename(initialdir=basedir)
28     try:
29         fobj_r = open(filename, 'r')
30     except IOError, errmsg:
31         print '*** Failed open file:', errmsg
32     else:
33         editbox.delete(1.0, END)
34         for eachline in fobj_r:
35             editbox.insert(INSERT, eachline)
36         fobj_r.close()
37  
38 def savefile():
39     save_data = editbox.get(1.0, END)
40     try:
41         fobj_w = open(filename, 'w')
42         fobj_w.writelines(save_data.encode('utf-8')) # 感谢OSC-骠骑将军 指教
43         fobj_w.close()
44         tkMessageBox.showinfo(title='提示',
45                 message='保存成功')
46     except IOError, errmsg:
47         tkMessageBox.showwarning(title='保存失败', message='保存出错    ')
48         tkMessageBox.showwarning(title='错误信息', message=errmsg)
49     except NameError:
50         tkMessageBox.showwarning(title='保存失败', message='未打开文件')
51 def showlinenum():
52     tkMessageBox.showinfo(title='提示',
53             message='这个功能作者现在不会写,放这里装饰用的.')
54 def destroy_ui(ui):
55     ui.destroy()
56  
57 def aboutauthor():
58     author_ui = Toplevel()
59     author_ui.title('关于')
60     author_ui.geometry('200x80')
61     about_string = Label(author_ui,
62             text="作者: ToughGuy\n\n主页: http://www.techzhai.net/")
63     confirmbtn = Button(author_ui, text='确定',
64             command=lambda:destroy_ui(author_ui))
65     about_string.pack()
66     confirmbtn.pack()
67     # author_ui.mainloop()
68  
69 def CreateMenus():
70     # 初始化菜单
71     Menubar = Menu(root)
72  
73     # 创建文件菜单
74     filemenu = Menu(Menubar, tearoff=0)
75     filemenu.add_command(label='打开文件', command=openfile)
76     filemenu.add_command(label='保存文件', command=savefile)
77     filemenu.add_command(label='退出', command=lambda:destroy_ui(root))
78     Menubar.add_cascade(label='文件', menu=filemenu)
79  
80     # 创建编辑菜单
81     editmenu = Menu(Menubar, tearoff=0)
82     editmenu.add_command(label='显示行号', command=showlinenum)
83     Menubar.add_cascade(label='编辑', menu=editmenu)
84  
85     # 创建帮助菜单
86     helpmenu = Menu(Menubar, tearoff=0)
87     helpmenu.add_command(label='关于作者', command=aboutauthor)
88     Menubar.add_cascade(label='帮助', menu=helpmenu)
89     root.config(menu=Menubar)
90  
91 root = Tk()
92 root.title('文本编辑器')
93 root.geometry('500x400')
94 CreateMenus()
95 editbox = Text(root, width=70, height=25, bg='white')
96 editbox.pack(side=TOP, fill=X)
97 root.mainloop()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值