python寫的一個文字編輯器

來源:互聯網
上載者:User
代碼如下:


#!/usr/bin/env python
#-*- coding: utf-8 -*-
#=============================================================================
# FileName:
# Desc:
# Author: ToughGuy
# Version: 0.0.1
# LastChange: 2013-02-20 14:52:11
# History:
#=============================================================================

from Tkinter import *
import tkMessageBox,tkFileDialog
import platform

# nl = os.linesep

def openfile():
global filename # 使用global聲明為全域變數,方便後邊的程式調用
systype = platform.system() # 判斷系統類別型
if systype == 'windows':
basedir = 'c:\\'
else:
basedir = '/'
filename = tkFileDialog.askopenfilename(initialdir=basedir)
try:
fobj_r = open(filename, 'r')
except IOError, errmsg:
print '*** Failed open file:', errmsg
else:
editbox.delete(1.0, END)
for eachline in fobj_r:
editbox.insert(INSERT, eachline)
fobj_r.close()

def savefile():
save_data = editbox.get(1.0, END)
try:
fobj_w = open(filename, 'w')
fobj_w.writelines(save_data.encode('utf-8'))
fobj_w.close()
tkMessageBox.showinfo(title='提示',
message='儲存成功')
except IOError, errmsg:
tkMessageBox.showwarning(title='儲存失敗', message='儲存出錯 ')
tkMessageBox.showwarning(title='錯誤資訊', message=errmsg)
except NameError:
tkMessageBox.showwarning(title='儲存失敗', message='未開啟檔案')
def showlinenum():
tkMessageBox.showinfo(title='提示',
message='這個功能作者現在不會寫,放這裡裝飾用的.')
def destroy_ui(ui):
ui.destroy()

def aboutauthor():
author_ui = Toplevel()
author_ui.title('關於')
author_ui.geometry('200x80')
about_string = Label(author_ui,
text="作者: ToughGuy")
confirmbtn = Button(author_ui, text='確定',
command=lambda:destroy_ui(author_ui))
about_string.pack()
confirmbtn.pack()
# author_ui.mainloop()

def CreateMenus():
# 初始化菜單
Menubar = Menu(root)

# 建立檔案菜單
filemenu = Menu(Menubar, tearoff=0)
filemenu.add_command(label='開啟檔案', command=openfile)
filemenu.add_command(label='儲存檔案', command=savefile)
filemenu.add_command(label='退出', command=lambda:destroy_ui(root))
Menubar.add_cascade(label='檔案', menu=filemenu)

# 建立編輯菜單
editmenu = Menu(Menubar, tearoff=0)
editmenu.add_command(label='顯示行號', command=showlinenum)
Menubar.add_cascade(label='編輯', menu=editmenu)

# 建立協助菜單
helpmenu = Menu(Menubar, tearoff=0)
helpmenu.add_command(label='關於作者', command=aboutauthor)
Menubar.add_cascade(label='協助', menu=helpmenu)
root.config(menu=Menubar)

root = Tk()
root.title('文字編輯器')
root.geometry('500x400')
CreateMenus()
editbox = Text(root, width=70, height=25, bg='white')
editbox.pack(side=TOP, fill=X)
root.mainloop()

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.