python寫的一個文本編輯器
#!/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聲明為全局變量,方便后邊的程序調(diào)用
systype = platform.system() # 判斷系統(tǒng)類型
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='這個功能作者現(xiàn)在不會寫,放這里裝飾用的.')
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)
# 創(chuàng)建文件菜單
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)
# 創(chuàng)建編輯菜單
editmenu = Menu(Menubar, tearoff=0)
editmenu.add_command(label='顯示行號', command=showlinenum)
Menubar.add_cascade(label='編輯', menu=editmenu)
# 創(chuàng)建幫助菜單
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()
相關文章
Python解析命令行讀取參數(shù)--argparse模塊使用方法
這篇文章主要介紹了Python解析命令行讀取參數(shù)--argparse模塊使用方法,需要的朋友可以參考下2018-01-01python肯德爾系數(shù)相關性數(shù)據(jù)分析示例
這篇文章主要為大家介紹了python肯德爾系數(shù)相關性數(shù)據(jù)分析示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02python編程開發(fā)之類型轉換convert實例分析
這篇文章主要介紹了python編程開發(fā)之類型轉換convert用法,結合實例形式分析了Python中常見的數(shù)據(jù)類型及類型轉換convert的具體使用方法,需要的朋友可以參考下2015-11-11Python如何使用k-means方法將列表中相似的句子歸類
這篇文章主要介紹了Python如何使用k-means方法將列表中相似的句子聚為一類,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08pytorch 實現(xiàn)張量tensor,圖片,CPU,GPU,數(shù)組等的轉換
今天小編就為大家分享一篇pytorch 實現(xiàn)張量tensor,圖片,CPU,GPU,數(shù)組等的轉換,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01