用户:
FreeWorld查看:0 回复:4 评论:0 创建时间:2024-07-22T12:13:12
本人花费5个月做出记事本
高度还原记事本功能
还支持 2D ,3D 图形,计算,日期等功能
注意!!!代码仅供学习,严禁抄袭,复制,转卖等,否则举报!!!
代码如下:
import tkinter
import tkinter.filedialog
import tkinter.messagebox
import os
import sys
import random
import numpy as np
import matplotlib.pyplot as p3
from mpl_toolkits.mplot3d import Axes3D
def open_file():
filename = tkinter.filedialog.askopenfilename(title='打开文件', filetypes=[('文本文件', '*.txt'), ('所有文件', '*')])
if filename:
with open(filename, 'r', encoding='utf-8') as f:
content = f.read()
text.insert(tkinter.INSERT, content)
def save_file():
filename = tkinter.filedialog.asksaveasfilename(title='保存文件', filetypes=[('文本文件', '*.txt'), ('所有文件', '*')])
if filename:
content = text.get(1.0, tkinter.END)
with open(filename, 'w', encoding='utf-8') as f:
f.write(content)
def exit_app():
if tkinter.messagebox.askokcancel('退出', '确定退出吗?'):
root.destroy()
def about_app():
tkinter.messagebox.showinfo('关于', '记事本 v6.0')
tkinter.messagebox.showwarning('警告', '这是一个警告消息!')
tkinter.messagebox.showerror('错误', '这是一个错误消息!')
tkinter.messagebox.askquestion('询问', '你喜欢这个软件吗?')
tkinter.messagebox.askokcancel('确认', '你确定要这样做吗?')
tkinter.messagebox.askretrycancel('重试', '你要重试吗?')
tkinter.messagebox.askyesno('是否', '你要这样做吗?')
tkinter.messagebox.askyesnocancel('是否', '你要这样做吗?')
tkinter.messagebox.showinfo('结果', '你选择了“确定”')
def help_app():
tkinter.messagebox.showinfo('帮助', '请阅读帮助文档!')
tkinter.messagebox.showwarning('警告', '这是一个警告消息!')
tkinter.messagebox.showerror('错误', '这是一个错误消息!')
tkinter.messagebox.askquestion('询问', '你喜欢这个软件吗?')
tkinter.messagebox.askokcancel('确认', '你确定要这样做吗?')
tkinter.messagebox.askretrycancel('重试', '你要重试吗?')
tkinter.messagebox.askyesno('是否', '你要这样做吗?')
tkinter.messagebox.askyesnocancel('是否', '你要这样做吗?')
tkinter.messagebox.showinfo('结果', '你选择了“确定”')
def cut_text():
text.event_generate('<<Cut>>')
def copy_text():
text.event_generate('<<Copy>>')
def paste_text():
text.event_generate('<<Paste>>')
def create_toolbar(parent):
toolbar = tkinter.Frame(parent)
toolbar.pack(side=tkinter.TOP, fill=tkinter.X)
# 新建按钮
new_btn = tkinter.Button(toolbar, text='新建', command=new_file)
new_btn.pack(side=tkinter.LEFT)
# 打开按钮
open_btn = tkinter.Button(toolbar, text='打开', command=open_file)
open_btn.pack(side=tkinter.LEFT)
# 保存按钮
save_btn = tkinter.Button(toolbar, text='保存', command=save_file)
save_btn.pack(side=tkinter.LEFT)
# 分割线
line = tkinter.Frame(toolbar, width=2, height=24, bg='black')
line.pack(side=tkinter.LEFT, padx=2, pady=2)
# 剪切按钮
cut_btn = tkinter.Button(toolbar, text='剪切', command=cut_text)
cut_btn.pack(side=tkinter.LEFT)
# 复制按钮
copy_btn = tkinter.Button(toolbar, text='复制', command=copy_text)
copy_btn.pack(side=tkinter.LEFT)
# 粘贴按钮
paste_btn = tkinter.Button(toolbar, text='粘贴', command=paste_text)
paste_btn.pack(side=tkinter.LEFT)
# 分割线
line = tkinter.Frame(toolbar, width=2, height=24, bg='black')
line.pack(side=tkinter.LEFT, padx=2, pady=2)
# 帮助按钮
help_btn = tkinter.Button(toolbar, text='帮助', command=help_app)
help_btn.pack(side=tkinter.LEFT)
# 关于按钮
about_btn = tkinter.Button(toolbar, text='关于', command=about_app)
about_btn.pack(side=tkinter.LEFT)
# 退出按钮
exit_btn = tkinter.Button(toolbar, text='退出', command=exit_app)
exit_btn.pack(side=tkinter.LEFT)
# 状态栏
statusbar = tkinter.Label(parent, text='状态栏', bd=1, relief=tkinter.SUNKEN, anchor=tkinter.W)
statusbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
# 字符略缩图
def create_toolbar(parent):
toolbar = tkinter.Frame(parent)
toolbar.pack(side=tkinter.TOP, fill=tkinter.X)
# 新建按钮
def new_file():
global new_file
if new_file:
text.delete(1.0, tkinter.END)
new_file = False
else:
tkinter.messagebox.showinfo('提示', '文件已保存,是否新建?')
new_btn = tkinter.Button(toolbar, text='新建', command=new_file)
new_btn.pack(side=tkinter.LEFT)
# 打开按钮
def open_file():
filename = tkinter.filedialog.askopenfilename(title='打开文件', filetypes=[('文本文件', '*.txt'), ('所有文件', '*')])
if filename:
with open(filename, 'r', encoding='utf-8') as f:
content = f.read()
text.delete(1.0, tkinter.END)
text.insert(tkinter.INSERT, content)
open_btn = tkinter.Button(toolbar, text='打开', command=open_file)
open_btn.pack(side=tkinter.LEFT)
# 保存按钮
def save_file():
filename = tkinter.filedialog.asksaveasfilename(title='保存文件', filetypes=[('文本文件', '*.txt'), ('所有文件', '*')])
if filename:
content = text.get(1.0, tkinter.END)
with open(filename, 'w', encoding='utf-8') as f:
f.write(content)
save_btn = tkinter.Button(toolbar, text='保存', command=save_file)
save_btn.pack(side=tkinter.LEFT)
# 分割线
line = tkinter.Frame(toolbar, width=2, height=24, bg='black')
line.pack(side=tkinter.LEFT, padx=2, pady=2)
# 剪切按钮
def cut_text():
text.event_generate('<<Cut>>')
cut_btn = tkinter.Button(toolbar, text='剪切', command=cut_text)
cut_btn.pack(side=tkinter.LEFT)
# 复制按钮
def copy_text():
text.event_generate('<<Copy>>')
copy_btn = tkinter.Button(toolbar, text='复制', command=copy_text)
copy_btn.pack(side=tkinter.LEFT)
# 粘贴按钮
def paste_text():
text.event_generate('<<Paste>>')
paste_btn = tkinter.Button(toolbar, text='粘贴', command=paste_text)
paste_btn.pack(side=tkinter.LEFT)
# 分割线
line = tkinter.Frame(toolbar, width=2, height=24, bg='black')
line.pack(side=tkinter.LEFT, padx=2, pady=2)
# 帮助按钮
def help_app():
tkinter.messagebox.showinfo('帮助', '请阅读帮助文档!')
help_btn = tkinter.Button(toolbar, text='帮助', command=help_app)
help_btn.pack(side=tkinter.LEFT)
# 关于按钮
def about_app():
tkinter.messagebox.showinfo('关于', '记事本 v26.0')
about_btn = tkinter.Button(toolbar, text='关于', command=about_app)
about_btn.pack(side=tkinter.LEFT)
# 退出按钮
def exit_app():
if tkinter.messagebox.askokcancel('退出', '确定退出吗?'):
root.destroy()
exit_btn = tkinter.Button(toolbar, text='退出', command=exit_app)
exit_btn.pack(side=tkinter.LEFT)
# 状态栏
statusbar = tkinter.Label(parent, text='状态栏', bd=1, relief=tkinter.SUNKEN, anchor=tkinter.W)
statusbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
# 背景颜色设置按钮,默认白色,点击按钮切换黑色,文字颜色切换为白色
def change_color():
if text.config('background')[-1] == 'white':
text.config(background='black', foreground='white')
color_btn.config(text='白色')
else:
text.config(background='white', foreground='black')
color_btn.config(text='黑色')
color_btn = tkinter.Button(toolbar, text='黑色', command=change_color)
color_btn.pack(side=tkinter.RIGHT)
# 绑定快捷键
root.bind('<Control-n>', new_file)
root.bind('<Control-o>', open_file)
root.bind('<Control-s>', save_file)
root.bind('<Control-q>', exit_app)
root.bind('<Control-h>', help_app)
root.bind('<Control-a>', about_app)
root.bind('<Control-c>', cut_text)
root.bind('<Control-x>', copy_text)
root.bind('<Control-v>', paste_text)
# 状态栏显示当前行列号
def update_status_text(event):
set_status_text(f'行数:{text.index(tkinter.INSERT).split(".")[0]} 列数:{text.index(tkinter.INSERT).split(".")[1]}')
text.bind('<KeyRelease>', update_status_text)
text.bind('<ButtonRelease-1>', update_status_text)
def set_status_text(text):
statusbar.config(text=text)
statusbar.update_idletasks()
def update_status_text(event):
set_status_text(f'行数:{text.index(tkinter.INSERT).split(".")[0]} 列数:{text.index(tkinter.INSERT).split(".")[1]}')
text.bind('<Control-n>', new_file)
text.bind('<Control-o>', open_file)
text.bind('<Control-s>', save_file)
text.bind('<Control-q>', exit_app)
text.bind('<Control-h>', help_app)
text.bind('<Control-a>', about_app)
text.bind('<Control-c>', cut_text)
text.bind('<Control-x>', copy_text)
text.bind('<Control-v>', paste_text)
text.bind('<KeyRelease>', update_status_text)
text.bind('<ButtonRelease-1>', update_status_text)
root = tkinter.Tk()
root.title('记事本')
# 菜单栏
menubar = tkinter.Menu(root)
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label='打开', command=open_file)
filemenu.add_command(label='保存', command=save_file)
filemenu.add_separator()
filemenu.add_command(label='退出', command=exit_app)
menubar.add_cascade(label='文件', menu=filemenu)
root.config(menu=menubar)
# 编辑菜单
editmenu = tkinter.Menu(menubar, tearoff=0)
editmenu.add_command(label='剪切', command=cut_text)
editmenu.add_command(label='复制', command=copy_text)
editmenu.add_command(label='粘贴', command=paste_text)
menubar.add_cascade(label='编辑', menu=editmenu)
# 帮助菜单
helpmenu = tkinter.Menu(menubar, tearoff=0)
helpmenu.add_command(label='帮助', command=help_app)
helpmenu.add_command(label='关于', command=about_app)
menubar.add_cascade(label='帮助', menu=helpmenu)
root.config(menu=menubar)
# 查看菜单,缩放功能
viewmenu = tkinter.Menu(menubar, tearoff=0)
def zoom_in():
text.config(font=('Arial', 16))
def zoom_out():
text.config(font=('Arial', 12))
viewmenu.add_command(label='放大', command=zoom_in)
viewmenu.add_command(label='缩小', command=zoom_out)
menubar.add_cascade(label='查看', menu=viewmenu)
# 格式菜单,字体种类切换
fontmenu = tkinter.Menu(menubar, tearoff=0)
def change_font(font):
text.config(font=font)
fontmenu.add_command(label='Arial', command=lambda: change_font(('Arial', 12)))
fontmenu.add_command(label='Times New Roman', command=lambda: change_font(('Times New Roman', 12)))
fontmenu.add_command(label='Courier New', command=lambda: change_font(('Courier New', 12)))
menubar.add_cascade(label='格式', menu=fontmenu)
# 主题菜单,切换主题
thememenu = tkinter.Menu(menubar, tearoff=0)
def change_theme(theme):
text.config(bg=theme['bg'], fg=theme['fg'], insertbackground=theme['insertbackground'])
thememenu.add_command(label='默认', command=lambda: change_theme({'bg': 'white', 'fg': 'black', 'insertbackground': 'black'}))
thememenu.add_command(label='黑色', command=lambda: change_theme({'bg': 'black', 'fg': 'white', 'insertbackground': 'white'}))
thememenu.add_command(label='深色', command=lambda: change_theme({'bg': 'gray', 'fg': 'white', 'insertbackground': 'white'}))
menubar.add_cascade(label='主题', menu=thememenu)
# 绘图菜单,用于绘制2D,3D图形
graphmenu = tkinter.Menu(menubar, tearoff=0)
def show_2d():
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x, y)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('sin(x)')
plt.show()
def show_3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 绘制3D图形
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow')
ax.contourf(x, y, z, zdir='z', offset=-2, cmap='rainbow')
ax.contourf(x, y, z, zdir='x', offset=-2, cmap='rainbow')
ax.contourf(x, y, z, zdir='y', offset=2, cmap='rainbow')
ax.set_zlim(-2, 1)
# 设置坐标轴
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
graphmenu.add_command(label='2D图形', command=show_2d)
graphmenu.add_command(label='3D图形', command=show_3d)
menubar.add_cascade(label='绘图', menu=graphmenu)
# 计算菜单,用于计算表达式
calculamenu = tkinter.Menu(menubar, tearoff=0)
def calculate():
expression = tkinter.simpledialog.askstring('计算器', '请输入表达式:')
if expression:
try:
result = eval(expression)
tkinter.messagebox.showinfo('计算结果', f'结果:{result}')
except:
tkinter.messagebox.showerror('错误', '表达式有误!')
calculamenu.add_command(label='计算', command=calculate)
menubar.add_cascade(label='计算', menu=calculamenu)
# 工具菜单,用于一些工具功能
tool喵enu = tkinter.Menu(menubar, tearoff=0)
def show_time():
import time
tkinter.messagebox.showinfo('时间', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
tool喵enu.add_command(label='时间', command=show_time)
menubar.add_cascade(label='工具', menu=tool喵enu)
# 开始菜单,所有功能按钮
startmenu = tkinter.Menu(menubar, tearoff=0)
def new_file():
global new_file
if new_file:
text.delete(1.0, tkinter.END)
new_file = False
else:
tkinter.messagebox.showinfo('提示', '文件已保存,是否新建?')
def open_file():
filename = tkinter.filedialog.askopenfilename(title='打开文件', filetypes=[('文本文件', '*.txt'), ('所有文件', '*')])
if filename:
with open(filename, 'r', encoding='utf-8') as f:
content = f.read()
text.delete(1.0, tkinter.END)
text.insert(tkinter.INSERT, content)
def save_file():
filename = tkinter.filedialog.asksaveasfilename(title='保存文件', filetypes=[('文本文件', '*.txt'), ('所有文件', '*')])
if filename:
content = text.get(1.0, tkinter.END)
with open(filename, 'w', encoding='utf-8') as f:
f.write(content)
def cut_text():
text.event_generate('<<Cut>>')
def copy_text():
text.event_generate('<<Copy>>')
def paste_text():
text.event_generate('<<Paste>>')
def help_app():
tkinter.messagebox.showinfo('帮助', '请阅读帮助文档!')
def about_app():
tkinter.messagebox.showinfo('关于', '记事本 v26.0')
def exit_app():
if tkinter.messagebox.askokcancel('退出', '确定退出吗?'):
root.destroy()
startmenu.add_command(label='新建', command=new_file)
startmenu.add_command(label='打开', command=open_file)
startmenu.add_command(label='保存', command=save_file)
startmenu.add_separator()
startmenu.add_command(label='剪切', command=cut_text)
startmenu.add_command(label='复制', command=copy_text)
startmenu.add_command(label='粘贴', command=paste_text)
startmenu.add_separator()
startmenu.add_command(label='帮助', command=help_app)
startmenu.add_command(label='关于', command=about_app)
startmenu.add_separator()
startmenu.add_command(label='退出', command=exit_app)
menubar.add_cascade(label='开始', menu=startmenu)
# 3D按钮,点击弹出一个窗口,显示3D图片,用mpl_toolkits.mplot3d.axes3d.Axes3D画图
def show_3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 绘制3D图形
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow')
ax.contourf(x, y, z, zdir='z', offset=-2, cmap='rainbow')
ax.contourf(x, y, z, zdir='x', offset=-2, cmap='rainbow')
ax.contourf(x, y, z, zdir='y', offset=2, cmap='rainbow')
ax.set_zlim(-2, 1)
# 设置坐标轴
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
startmenu.add_separator()
startmenu.add_command(label='3D', command=show_3d)
root.config(menu=menubar)
# 绘制2D图形,用matplotlib.pyplot画图
def show_2d():
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x, y)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('sin(x)')
plt.show()
startmenu.add_separator()
startmenu.add_command(label='2D', command=show_2d)
startmenu.add_separator()
startmenu.add_command(label='3D', command=show_3d)
# 绘制2D散点图,用matplotlib.pyplot画图
def show_scatter():
import matplotlib.pyplot as plt
x = np.random.rand(100)
y = np.random.rand(100)
plt.scatter(x, y)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('散点图')
plt.show()
startmenu.add_separator()
startmenu.add_command(label='散点图', command=show_scatter)
# 绘制3D散点图,用matplotlib.pyplot画图
def show_scatter3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
ax.scatter(x, y, z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('3D散点图')
plt.show()
startmenu.add_separator()
startmenu.add_command(label='3D散点图', command=show_scatter3d)
# 绘制等高线图,用matplotlib.pyplot画图
def show_contour():
import matplotlib.pyplot as plt
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
plt.contour(x, y, z)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('等高线图')
plt.show()
startmenu.add_separator()
startmenu.add_command(label='等高线图', command=show_contour)
# 绘制3D等高线图,用matplotlib.pyplot画图
def show_contour3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
ax.contour3D(x, y, z, 50, cmap='rainbow')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('3D等高线图')
plt.show()
startmenu.add_separator()
startmenu.add_command(label='3D等高线图', command=show_contour3d)
# 绘制等值线图,用matplotlib.pyplot画图
def show_contourf():
import matplotlib.pyplot as plt
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
plt.contourf(x, y, z)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('等值线图')
plt.show()
startmenu.add_separator()
startmenu.add_command(label='等值线图', command=show_contourf)
# 绘制3D等值线图,用matplotlib.pyplot画图
def show_contourf3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
ax.contourf3D(x, y, z, 50, cmap='rainbow')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('3D等值线图')
plt.show()
startmenu.add_separator()
startmenu.add_command(label='3D等值线图', command=show_contourf3d)
# 绘制3D线图,用matplotlib.pyplot画图
def show_line3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow')
ax.contourf(x, y, z, zdir='z', offset=-2, cmap='rainbow')
ax.contourf(x, y, z, zdir='x', offset=-2, cmap='rainbow')
ax.contourf(x, y, z, zdir='y', offset=2, cmap='rainbow')
ax.set_zlim(-2, 1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('3D线图')
plt.show()
startmenu.add_separator()
startmenu.add_command(label='3D线图', command=show_line3d)
# 绘制3D面图,用matplotlib.pyplot画图
def show_surface3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('3D面图')
plt.show()
startmenu.add_separator()
startmenu.add_command(label='3D面图', command=show_surface3d)
# 绘制3D等值面图,用matplotlib.pyplot画图
def show_surfacef3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow')
ax.contourf(x, y, z, zdir='z', offset=-2, cmap='rainbow')
ax.contourf(x, y, z, zdir='x', offset=-2, cmap='rainbow')
ax.contourf(x, y, z, zdir='y', offset=2, cmap='rainbow')
ax.set_zlim(-2, 1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('3D等值面图')
plt.show()
startmenu.add_separator()
startmenu.add_command(label='3D等值面图', command=show_surfacef3d)
# 3D菜单,用于一些3D功能
threedmenu = tkinter.Menu(menubar, tearoff=0)
def show_3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 绘制3D图形
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow')
ax.contourf(x, y, z, zdir='z', offset=-2, cmap='rainbow')
ax.contourf(x, y, z, zdir='x', offset=-2, cmap='rainbow')
ax.contourf(x, y, z, zdir='y', offset=2, cmap='rainbow')
ax.set_zlim(-2, 1)
# 设置坐标轴
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('3D图形')
plt.show()
threedmenu.add_command(label='3D图形', command=show_3d)
menubar.add_cascade(label='3D', menu=threedmenu)
# 3D菜单按钮2,在3D菜单下
threedmenu2 = tkinter.Menu(threedmenu, tearoff=0)
def show_scatter3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
ax.scatter(x, y, z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('3D散点图')
plt.show()
threedmenu2.add_command(label='3D散点图', command=show_scatter3d)
threedmenu.add_cascade(label='3D散点图', menu=threedmenu2)
# 3D菜单按钮3,在3D菜单下
threedmenu3 = tkinter.Menu(threedmenu, tearoff=0)
def show_contour3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
ax.contour3D(x, y, z, 50, cmap='rainbow')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('3D等高线图')
plt.show()
threedmenu3.add_command(label='3D等高线图', command=show_contour3d)
threedmenu.add_cascade(label='3D等高线图', menu=threedmenu3)
# 3D菜单按钮4,在3D菜单下
threedmenu4 = tkinter.Menu(threedmenu, tearoff=0)
def show_contourf3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
ax.contourf3D(x, y, z, 50, cmap='rainbow')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('3D等值线图')
plt.show()
threedmenu4.add_command(label='3D等值线图', command=show_contourf3d)
threedmenu.add_cascade(label='3D等值线图', menu=threedmenu4)
# 3D菜单按钮,在3D菜单下
threedmenu6 = tkinter.Menu(threedmenu, tearoff=0)
def show_line3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow')
ax.contourf(x, y, z, zdir='z', offset=-2, cmap='rainbow')
ax.contourf(x, y, z, zdir='x', offset=-2, cmap='rainbow')
ax.contourf(x, y, z, zdir='y', offset=2, cmap='rainbow')
ax.set_zlim(-2, 1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('3D线图')
plt.show()
threedmenu6.add_command(label='3D线图', command=show_line3d)
threedmenu.add_cascade(label='3D线图', menu=threedmenu6)
# 3D菜单按钮7,在3D菜单下
threedmenu7 = tkinter.Menu(threedmenu, tearoff=0)
def show_surface3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('3D面图')
plt.show()
threedmenu7.add_command(label='3D面图', command=show_surface3d)
threedmenu.add_cascade(label='3D面图', menu=threedmenu7)
# 3D菜单按钮8,在3D菜单下
threedmenu8 = tkinter.Menu(threedmenu, tearoff=0)
def show_surfacef3d():
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow')
ax.contourf(x, y, z, zdir='z', offset=-2, cmap='rainbow')
ax.contourf(x, y, z, zdir='x', offset=-2, cmap='rainbow')
ax.contourf(x, y, z, zdir='y', offset=2, cmap='rainbow')
ax.set_zlim(-2, 1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('3D等值面图')
plt.show()
threedmenu8.add_command(label='3D等值面图', command=show_surfacef3d)
threedmenu.add_cascade(label='3D等值面图', menu=threedmenu8)
# 2D菜单,用于一些2D功能
twodmenu = tkinter.Menu(menubar, tearoff=0)
def show_2d():
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(-5, 5, 0.25)
y = np.sin(np.sqrt(x**2))
ax.plot(x, y)
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.title('2D图形')
plt.show()
twodmenu.add_command(label='2D图形', command=show_2d)
menubar.add_cascade(label='2D', menu=twodmenu)
# 2D菜单按钮2,在2D菜单下
twodmenu2 = tkinter.Menu(twodmenu, tearoff=0)
def show_scatter2d():
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.random.rand(100)
y = np.random.rand(100)
ax.scatter(x, y)
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.title('2D散点图')
plt.show()
twodmenu2.add_command(label='2D散点图', command=show_scatter2d)
twodmenu.add_cascade(label='2D散点图', menu=twodmenu2)
# 2D菜单按钮5,在2D菜单下
twodmenu5 = tkinter.Menu(twodmenu, tearoff=0)
def show_line2d():
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(-5, 5, 0.25)
y = np.sin(np.sqrt(x**2))
ax.plot(x, y)
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.title('2D线图')
plt.show()
twodmenu5.add_command(label='2D线图', command=show_line2d)
twodmenu.add_cascade(label='2D线图', menu=twodmenu5)
# 2D菜单按钮6,在2D菜单下
twodmenu6 = tkinter.Menu(twodmenu, tearoff=0)
def show_bar2d():
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(5)
y = np.random.rand(5)
ax.bar(x, y)
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.title('2D条形图')
plt.show()
twodmenu6.add_command(label='2D条形图', command=show_bar2d)
twodmenu.add_cascade(label='2D条形图', menu=twodmenu6)
# 2D菜单按钮7,在2D菜单下
twodmenu7 = tkinter.Menu(twodmenu, tearoff=0)
def show_pie2d():
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(5)
y = np.random.rand(5)
ax.pie(y, labels=x)
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.title('2D饼图')
plt.show()
twodmenu7.add_command(label='2D饼图', command=show_pie2d)
twodmenu.add_cascade(label='2D饼图', menu=twodmenu7)
# 2D菜单按钮8,在2D菜单下
twodmenu8 = tkinter.Menu(twodmenu, tearoff=0)
def show_hist2d():
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.random.rand(100)
ax.hist(x, bins=20)
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.title('2D直方图')
plt.show()
twodmenu8.add_command(label='2D直方图', command=show_hist2d)
twodmenu.add_cascade(label='2D直方图', menu=twodmenu8)
# 2D菜单按钮9,在2D菜单下
twodmenu9 = tkinter.Menu(twodmenu, tearoff=0)
def show_box2d():
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.random.rand(100)
ax.boxplot(x)
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.title('2D箱线图')
plt.show()
twodmenu9.add_command(label='2D箱线图', command=show_box2d)
twodmenu.add_cascade(label='2D箱线图', menu=twodmenu9)
# 2D菜单按钮10,在2D菜单下
twodmenu10 = tkinter.Menu(twodmenu, tearoff=0)
def show_errorbar2d():
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(5)
y = np.random.rand(5)
yerr = np.random.rand(5)
ax.errorbar(x, y, yerr=yerr)
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.title('2D误差线图')
plt.show()
twodmenu10.add_command(label='2D误差线图', command=show_errorbar2d)
twodmenu.add_cascade(label='2D误差线图', menu=twodmenu10)
# 工具栏
toolbar = tkinter.Frame(root)
toolbar.pack(side=tkinter.TOP, fill=tkinter.X)
# 文本框
text = tkinter.Text(root)
text.pack(fill=tkinter.BOTH, expand=1)
new_file = True
if len(sys.argv) > 1:
with open(sys.argv[1], 'r', encoding='utf-8') as f:
content = f.read()
text.insert(tkinter.INSERT, content)
new_file = False
# 工具栏
create_toolbar(root)
if new_file:
text.focus_set()
else:
text.mark_set(tkinter.INSERT, '1.0')
text.see(tkinter.INSERT)
# 最大化显示
root.state('zoomed')
# 主循环
root.mainloop()