Chinaunix首页 | 论坛 | 博客
  • 博客访问: 136678
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 309
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-06 11:27
个人简介

开启暴走模式。

文章分类

全部博文(31)

文章存档

2017年(19)

2016年(1)

2015年(11)

我的朋友

分类: Python/Ruby

2015-08-28 17:11:25

从网上找了下答案,稍作整理,运行在windows下python2.* IDLE下. Python3.*里的模块有改变。

点击(此处)折叠或打开

  1. #-*- coding: utf-8 -*-

  2. from Tkinter import *
  3. import tkMessageBox,tkFileDialog
  4. import platform

  5. def openfile():
  6.     global filename # 使用global声明为全局变量,方便后边的程序调用
  7.     systype = platform.system() # 判断系统类型
  8.     if systype == 'windows':
  9.      basedir = 'c:\\'
  10.     else:
  11.      basedir = '/'
  12.     filename = tkFileDialog.askopenfilename(initialdir=basedir)
  13.     try:
  14.         fobj_r = open(filename, 'r')
  15.     except IOError, errmsg:
  16.         print '*** Failed open file:', errmsg
  17.     else:
  18.         editbox.delete(1.0, END)
  19.         for eachline in fobj_r:
  20.             editbox.insert(INSERT, eachline)
  21.         fobj_r.close()

  22. def savefile():
  23.     save_data = editbox.get(1.0, END)
  24.     try:
  25.         fobj_w = open(filename, 'w')
  26.         fobj_w.writelines(save_data.encode('utf-8'))
  27.         fobj_w.close()
  28.         tkMessageBox.showinfo(title='提示',
  29.                                     message='保存成功')
  30.     except IOError, errmsg:
  31.      tkMessageBox.showwarning(title='保存失败', message='保存出错 ')
  32.      tkMessageBox.showwarning(title='错误信息', message=errmsg)
  33.     except NameError:
  34.      tkMessageBox.showwarning(title='保存失败', message='未打开文件')
  35. def showlinenum():
  36.     tkMessageBox.showinfo(title='提示',
  37.                  message='这个功能作者现在不会写,放这里装饰用的.')
  38. def destroy_ui(ui):
  39.     ui.destroy()
  40.  
  41. def aboutauthor():
  42.     author_ui = Toplevel()
  43.     author_ui.title('关于')
  44.     author_ui.geometry('200x80')
  45.     about_string = Label(author_ui,
  46.      text="作者: 大G\n\n主页: 大G.com/")
  47.     confirmbtn = Button(author_ui, text='确定',
  48.      command=lambda:destroy_ui(author_ui))
  49.     about_string.pack()
  50.     confirmbtn.pack()
  51.     # author_ui.mainloop()
  52.  
  53. def CreateMenus():
  54.     # 初始化菜单
  55.     Menubar = Menu(root)
  56.  
  57.     # 创建文件菜单
  58.     filemenu = Menu(Menubar, tearoff=0)
  59.     filemenu.add_command(label='打开文件', command=openfile)
  60.     filemenu.add_command(label='保存文件', command=savefile)
  61.     filemenu.add_command(label='退出', command=lambda:destroy_ui(root))
  62.     Menubar.add_cascade(label='文件', menu=filemenu)
  63.  
  64.     # 创建编辑菜单
  65.     editmenu = Menu(Menubar, tearoff=0)
  66.     editmenu.add_command(label='显示行号', command=showlinenum)
  67.     Menubar.add_cascade(label='编辑', menu=editmenu)
  68.  
  69.     # 创建帮助菜单
  70.     helpmenu = Menu(Menubar, tearoff=0)
  71.     helpmenu.add_command(label='关于作者', command=aboutauthor)
  72.     Menubar.add_cascade(label='帮助', menu=helpmenu)
  73.     root.config(menu=Menubar)
  74.  
  75. root = Tk()
  76. root.title('文本编辑器')
  77. root.geometry('500x400')
  78. CreateMenus()
  79. editbox = Text(root, width=70, height=25, bg='white')
  80. editbox.pack(side=TOP, fill=X)
  81. root.mainloop()


阅读(1574) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~