猫史档案馆


【Python作品分享】记事本GUI【作品秀】

用户:IMG_25522IMG_25522查看:0 回复:2 评论:0 创建时间:2023-06-29T12:10:07


【作品展示】

center_image

 

【作品介绍】

具有用户图形化界面的记事本(python)

 

【作品源代码】

import easygui as eg
import tkinter as tk
from tkinter import messagebox

#新建编码格式录
def xg():
    with open('how_open.bin', 'wt') as f:
        f.write('UTF-8\nGBK\nbig5')

#编码格式
def flush_how():
    try :
        global how
        with open('how_open.bin','rt') as f:
            how = (f.read()).split('\n')
            
    except:
        xg()

#wd:the main windw
wd = tk.Tk()
wd.geometry('500x300')
wd.resizable(width=0, height=0)
wd.title('记事本')
wd['background'] = '#50B0FF'
#FOR OPEN

def openf():
    flush_how()
    global how
    way = eg.fileopenbox(msg='打开文本文件',title='打开')
    if way != None:
        how_open = eg.choicebox(
            choices=how, title='选择', msg='选择编码系统,一般为 UTF-8')
        
        with open(way,'rt',encoding=how_open) as tf:
            try:
                thing = tf.read()
                text = eg.textbox(title=way.split('\\')[-1],msg='文件位置:'+way+'\nCancel:关闭,不保存更改 ;OK:关闭,会问是否保存',text=thing)
            except Exception as e:
                messagebox.showerror(title='错误',
                     message='读取文件时出现错误,\n可能是编码系统选择错误所致。\n错误类型:'+str(e.__class__))
                text = None
        if text != None and messagebox.askyesno(title='提示',message='是否保存?'):
            with open(way, 'wt', encoding=how_open) as tf:
                tf.write(text)

#op:the button for open a file
op = tk.Button(wd, text='打开文件', font=('宋体', 15), height=3,
               width=10, background='#50d050', command=openf)

#FOR NEW
def newf():
    flush_how()
    global how
    how_new = eg.choicebox(choices=how, title='选择', msg='选择编码系统,建议 UTF-8')
    if how_new != None:
        text = eg.textbox(
            title='新文本文件', msg='新文件\nCancel:关闭,不保存更改 ;OK:关闭,会问是否保存', text='')
    
        if text != None and messagebox.askyesno(title='提示', message='是否保存?'):
            way = eg.filesavebox(msg='保存新文本文件', title='保存')
            with open(way, 'wt', encoding=how_new) as tf:
                tf.write(text)

#ne:the button for make a new file
ne = tk.Button(wd, text='新建文件', font=('宋体', 15), height=3,
               width=10, background='#f0f050', command=newf)

#FOR ABOUT

def about():
    eg.textbox(title='关于此GUI记事本...',msg='GUI记事本,制作人:IMG_25522',text=
    '''
    1.此记事本免费开放使用;
    2.任何人可免费将此用于学习等目的,
    但不要为了利益、虚荣心等而使用、占有之。
    
    更新记录:
    v0.03 2023/6/29 针对已知问题进行了修改
    v0.02 2023/6/28 更出了添加编码系统的功能
    v0.01 2023/6/24 第1版


    '''
    )

#ab:the button for about
ab = tk.Button(wd, text='关于\n...', font=('宋体', 15), height=3,
               width=5, background='#50B0FF', command=about)

#for 添加编码格式

def apop():
    new_op=eg.enterbox(msg='输入编码系统名称...',title='输入')
    try :
        with open('how_open.bin','rt') as f:
            onr = (f.read()).split('\n')#onr:旧内容
        if not new_op in onr :
            with open('how_open.bin','at') as f1:
                f1.write('\n'+new_op)
            messagebox.showinfo(title='提示',message='编码系统添加成功!')
        else:
            messagebox.showinfo(title='提示',message='该编码系统已经存在于可供您选择的表中了!')
    except:
        xg()
        if new_op != None:
            with open('how_open.bin', 'at') as f:
                f.write('\n'+new_op)

#apop_b=添加格式的按钮
apop_b = tk.Button(wd, text='添加编码系统', font=('宋体', 15), height=3,
               width=15, background='gray', command=apop)

op.place(x=50, y=70)
ne.place(x=200,y=70)
ab.place(x=320,y=70)
apop_b.place(x=50,y=150)
wd.mainloop()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
昌龙XL昌龙XL

我来试试emotion_编程猫_点赞

 

点赞0


评论


昌龙XL昌龙XL

我认为你的作品很好,可以改进一下,以下是我自己做的可以借助原生Py编程序的文本编辑工具,你可以在作品中添加像这样的实时编辑工具:

 

#Easy python
#be made by 昌龙XL

import tkinter as tk
import easygui as g
from tkinter.filedialog import askopenfilename, asksaveasfilename
import os
import subprocess

def run_cmd_Popen_fileno(cmd_string):
"""执行cmd命令"""
os.system(' start cmd.exe /K '+cmd_string)

def open_file():
try:
"""打开"""
filepath = askopenfilename(
filetypes=[("Python Files", "*.py"), ("Text Files", "*.txt")]
)
if not filepath:
return
txt_edit.delete(1.0, tk.END)
with open(filepath, "r") as input_file:
text = input_file.read()
txt_edit.insert(tk.END, text)
window.title(f"Easy Python v1.0.0 - {filepath}")
except:
g.msgbox(msg="文件打开失败",title="Easy python",ok_button="确认")


def save_file():
"""保存"""
filepath = asksaveasfilename(
defaultextension="txt",
filetypes=[("Python Files", "*.py"), ("Text Files", "*.txt")],
)
if not filepath:
return
with open(filepath, "w") as output_file:
text = txt_edit.get(1.0, tk.END)
output_file.write(text)
window.title(f"Easy Python v1.0.0 - {filepath}")

def pip():
a = g.enterbox(msg="输入库的名称",title="Easy Python")
if a == None:
ts = 0
else:
a1 = "pip install "+str(a)
run_cmd_Popen_fileno(a1)

def open_cmd():
"""打开cmd"""
os.system('start cmd.exe')


window = tk.Tk()
window.title("Easy Python v1.0.0 ")
window.rowconfigure(0, minsize=400, weight=1)
window.columnconfigure(1, minsize=400, weight=1)

txt_edit = tk.Text(window)
fr_buttons = tk.Frame(window, relief=tk.RAISED, bd=2)
btn_open = tk.Button(fr_buttons, text="打开", command=open_file)
btn_save = tk.Button(fr_buttons, text="保存", command=save_file)
btn_pip = tk.Button(fr_buttons, text="安装库", command=pip)
btn_cmd = tk.Button(fr_buttons, text="终端", command=open_cmd)

btn_open.grid(row=0, column=0, sticky="ew", padx=5, pady=5)
btn_save.grid(row=1, column=0, sticky="ew", padx=5, pady=5)
btn_pip.grid(row=2, column=0, sticky="ew", padx=5, pady=5)
btn_cmd.grid(row=3, column=0, sticky="ew", padx=5, pady=5)


fr_buttons.grid(row=0, column=0, sticky="ns")
txt_edit.grid(row=0, column=1, sticky="nsew")

window.mainloop()

点赞0


评论