python仿微软记事本
# -*- coding=utf-8 -*-
from tkinter import filedialog
import tkinter as tk
import tkinter.scrolledtext as tkst
from tkinter import messagebox
import fileinput
from tkinter import *
import os
import time t1 = []
root = None def die():
root.destroy() def about():
messagebox.showinfo(title = "当前版本为1.0,欢迎使用",message = "**作者:韩东\n**状态:继续努力ing")
class editor:
def __init__(self,rt):
if rt == None:
self.t = tk.Tk()
else:
self.t = tk.Toplevel(rt)
self.t.title("文本编辑器%d" % (len(t1)+1))
self.bar = tk.Menu(rt) self.filem = tk.Menu(self.bar)
self.filem.add_separator()
self.filem.add_command(label = "新建",command = self.neweditor)
self.filem.add_separator()
self.filem.add_command(label = "打开",command = self.openfile)
self.filem.add_separator()
self.filem.add_command(label = "保存",command = self.savefile)
self.filem.add_separator()
self.filem.add_command(label = "关闭",command = self.close)
self.filem.add_separator()
self.filem.add_command(label = "退出",command = die) self.editm = tk.Menu(self.bar)
self.editm.add_separator()
self.editm.add_command(label = "复制",command = self.copy)
self.editm.add_separator()
self.editm.add_command(label = "黏贴",command = self.paste)
self.editm.add_separator()
self.editm.add_command(label = "剪切",command = self.cut)
self.editm.add_separator()
self.editm.add_command(label = "删除",command = self.delete_text)
self.editm.add_separator()
self.editm.add_command(label = "查找",command = self.find_char)
self.editm.add_separator()
self.editm.add_command(label = "全选",command = self.select_char_all) self.helpm = tk.Menu(self.bar)
self.helpm.add_command(label = "关于",command = about)
self.bar.add_cascade(label = "文件",menu = self.filem)
self.bar.add_cascade(label = "编辑",menu = self.editm)
self.bar.add_cascade(label = "帮助",menu = self.helpm) self.t.config(menu = self.bar) self.f = tk.Frame(self.t,width = 512)
self.f.pack(expand =1) self.st = tkst.ScrolledText(self.t)
self.st.pack(expand = 1) def close(self):
self.t.destroy()
def openfile(self):
oname = filedialog.askopenfilename(filetypes = [("打开文件","*.txt")])
if oname:
for line in fileinput.input(oname):
self.st.insert("1.0",line)
self.t.title(oname) def savefile(self):
sname = filedialog.asksaveasfilename(title = "保存好你的宝宝哟",filetypes = [("保存文件","*.txt")])
if sname:
ofp = open(sname,"a")
ofp.write(self.st.get(1.0,tk.END))
ofp.flush()
ofp.close()
self.t.title(sname) def neweditor(self):
global root
t1.append(editor(root))
def copy(self):
text = self.st.get(tk.SEL_FIRST,tk.SEL_LAST)
self.st.clipboard_clear()
self.st.clipboard_append(text)
def paste(self):
try:
text = self.st.selection_get(selection = "CLIPBOARD")
self.st.insert(tk.INSERT,text)
except tk.TclError:
pass def cut(self):
text = self.st.get(tk.SEL_FIRST,tk.SEL_LAST)
self.st.delete(tk.SEL_FIRST,tk.SEL_LAST)
self.st.clipboard_clear()
self.st.clipboard_append(text) def delete_text(self):
self.st.delete(tk.SEL_FIRST,tk.SEL_LAST) def find_char(self):
target = simpledialog.askstring("简易文本编辑器","寻找字符串")
if target:
end = self.st.index(tk.END)
endindex = end.split(".")
end_line = int(endindex[0])
end_column = int(endindex[1])
pos_line =1
pos_column=0
length =len(target)
while pos_line <= end_line :
if pos_line == end_line and pos_column +length > end_column:
break
elif pos_line < end_line and pos_column + length >100:
pos_line = pos_line + 1
pos_column = 100 - (pos_column + length)
if pos_column > end_column:
break
else:
pos = str(pos_line)+"."+str(pos_column)
where = self.st.search(target,pos,tk.END)
if where:
print(where)
where1 =where.split(".")
sele_end_col = str(int(where1[1])+length)
sele = where1[0] + "."+ sele_end_col
self.st.tag_add(tk.SEL,where,sele)
self.st.mark_set(tk.INSERT,sele)
self.st.see(tk.INSERT)
#self.st.focus() again = messagebox.askokcancel(title = "继续查询么")
if again:
pos_line = int(where1[0])
pos_column = int(sele_end_col)
else:
aa=messagebox.showinfo(title = "你终于还是放弃了我",message = "你放弃了我--!")
if aa:
sys.exit() def select_char_all(self):
self.st.tag_add(tk.SEL,1.0,tk.END)
self.st.see(tk.INSERT)
self.st.focus()
if __name__ == "__main__":
root = None
t1.append(editor(root))
root = t1[0].t
root.mainloop()
效果:

python仿微软记事本的更多相关文章
- EF+LINQ事物处理 C# 使用NLog记录日志入门操作 ASP.NET MVC多语言 仿微软网站效果(转) 详解C#特性和反射(一) c# API接受图片文件以Base64格式上传图片 .NET读取json数据并绑定到对象
EF+LINQ事物处理 在使用EF的情况下,怎么进行事务的处理,来减少数据操作时的失误,比如重复插入数据等等这些问题,这都是经常会遇到的一些问题 但是如果是我有多个站点,然后存在同类型的角色去操作 ...
- 使用Ant Design写一个仿微软ToDo
实习期的第一份活,自己看Ant Design的官网学习,然后用Ant Design写一个仿微软ToDo. 不做教学目的,只是记录一下. 1.学习 Ant Design 是个组件库,想要会用,至少要知道 ...
- 一行导出所有任意微软SQL server数据脚本-基于Python的微软官方mssql-scripter工具使用全讲解
文章标题: 一行导出所有任意微软SQL serer数据脚本-基于Python的微软官方mssql-scripter工具使用全讲解 关键字 : mssql-scripter,SQL Server 文章分 ...
- Python编写的记事本小程序
用Python中的Tkinter模块写的一个简单的记事本程序,Python2.x和Python3.x的许多内置函数有所改变,所以以下分为Python2.x和Python3.x版本. 一.效果展示: 二 ...
- python 去除微软的BOM
傻逼微软会给文件前面加上efbbbf, 导致开发人员浪费很多时间在排错上,下面通过python代码来实现去除微软BOM的功能 用法很简单,指定可能含有BOM开头的文件,并且将微软的\r\n 换成lin ...
- Python开发简单记事本
摘要: 本文是使用Python,结合Tkinter开发简单记事本. 本文的操作环境:ubuntu,Python2.7,采用的是Pycharm进行代码编辑,个人很喜欢它的代码自动补齐功能. 最近很想对p ...
- 15行Python 仿百度搜索引擎
开发工具:PyCharm 开发环境:python3.6 + flask + requests 开发流程: 1. 启动一个web服务 from flask import Flask app = Flas ...
- ASP.NET MVC多语言 仿微软网站效果(转)
本文转自: https://blog.csdn.net/Cooldiok/article/details/7831351 2017年10月22日 21:31:22 Cooldiok 微软作为ASP.N ...
- ASP.NET MVC多语言 仿微软网站效果
文章转载自:https://blog.csdn.net/cooldiok/article/details/78313513 微软作为ASP.NET的创造者,它对于官网的结构设计肯定有值得我们借鉴和参考 ...
随机推荐
- Maven invalid task...
执行maven构建项目报错: Invalid task '‐DgroupId=*': you must specify a valid lifecycle phase, or a goal in th ...
- ACM题目————中缀表达式转后缀
题目描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说)操作符在两个操作数中间:num1 operand num2.同理,后缀表达式就是操作符在两 ...
- 计算机学院大学生程序设计竞赛(2015’12)Polygon
Polygon Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- SlickGrid example 1: 最简单的例子和用法
SlickGrid例子和用法 开始学习使用SlickGrid,确实挺好用,挺方便的. 官网地址: https://github.com/mleibman/SlickGrid 不多说,先上效果图. 上代 ...
- PHP生成 excl、word文件
PHP生成 excl.word文件 $time = time(); $filename = date("Y年m月d日h点m分s秒", $time).'问卷数据'; $rows ...
- Maximum Value(哈希)
B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- MySql5.7-多源复制(多主单从)
1.1.主库配置 my.cnf #确保唯一 server-id=1 #作为Master要开启binlog log-bin=mysql-bin #binlog format有三种形式:Stateme ...
- hdu 3944 dp?
DP? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 128000/128000 K (Java/Others)Total Subm ...
- VFL +AutoLayout
横竖屏事件响应(viewWillLayoutSubviews和通知)两种方式 http://blog.csdn.net/nogodoss/article/details/17246489 一,NSLa ...
- [Objective-C]__bridge,__bridge_retained和__bridge_transfer的意思,区别与使用
使用ARC能帮我们减轻不少内存管理方面的负担,尤其是对用惯了Java的程序员来说.但是像我这种Java基础本身就不牢固,做了两年的iOS已经习惯手动管理内存的半吊子,使用ARC还是经常碰壁. 对于CF ...