内容摘要:
tk是一个轻量的ui设计工具,下面的程序可以读取本地txt文件并画图(matplot),以及生成的数据画图,是动态画图的。
红色字体部分是kt的一个框架。包括设置按钮,以及触发按钮之后所对应的显示框。
蓝色部分是matplot部分,通过这部分可以实现画图,里面主要涉及如何分窗口,以及如何把最后的图显示到kt里。
from matplotlib import pyplot as plt
import tkinter
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import json
from tkinter import *
import requests
import os
#【1】tkinter https://blog.csdn.net/Y_principal/article/details/106080676
#【1.1】创建tkinter的主窗口
root = tkinter.Tk()
#【1.2】tkinter窗口名
root.title("tk窗口(翻译以及画图)")
#【1.3】设置tkinter窗口名大小
root.geometry('500x100') # 宽 高 这里的乘是小x
#【1.7.1】翻译按钮出发的函数
def fanyi():
content = entry1.get().strip() # 把翻框里需要翻译的内容get过来
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
data = { # data是截图里的from data里的内容
'i': content,
'doctype': 'json'
}
r = requests.post(url, data=data) # requests的post,和get的主要区别,个人觉得是post可以用来提交一些数据,看附录部分解释
ret = r.content.decode()
result = json.loads(ret)
# print(result)
res.set(result['translateResult'][0][0]['tgt'])
# print(content)
#【1.4】设置标签,可以不要 https://blog.csdn.net/Y_principal/article/details/106080676
# Label(root, text="输入任容:", font=('GB2312', 18), fg='blue').grid(row=1, column=0)
# Label(root, text="结果:", font=('GB2312', 18), fg='blue').grid(row=2, column=0)
#【1.5】设置文本框
res_entry1 = StringVar()
#entry1=Text(root, width=25,height=3,font=('GB2312', 18))
entry1 = Entry(root, font=('GB2312', 18),width=30,textvariable=res_entry1)
res_entry1.set('input your text here')#给一个默认显示
entry1.grid(row=1, column=0)
#【1.6】
res = StringVar()
#entry2=Text(root, width=25,height=3,font=('GB2312', 18))
entry2 = Entry(root, font=('GB2312', 18), textvariable=res,width=30)
res.set('翻译结果')
entry2.grid(row=2, column=0)
#【1.7】 设置按钮https://blog.csdn.net/Y_principal/article/details/106080676
Button(root, text="翻译", width=10, font=('GB2312', 18), command=fanyi).grid(row=4, column=0, sticky=W)
Button(root, text="退出", width=10, font=('GB2312', 18), command=root.quit).grid(row=4, column=0, sticky=E)
# 生成用于绘sin图的数据,这个接口可以用于以后实时数据的输入
#【2】matplot部分https://blog.csdn.net/Y_principal/article/details/106080676
#【2.1】matplot部分,创建一个figure
f=plt.figure(figsize=(5, 4))
#【2.2】把figure分为三行一列
a = plt.subplot(2, 1, 1)
plt.xlabel('x time')
plt.ylabel('y cm')
b = plt.subplot(2, 1, 2)
plt.xlabel('x time')
plt.ylabel('y cm')
#c = plt.subplot(3, 1, 3)
#【2.3】为第一行窗口创建名字
a.set_title("a")
#【2.4】创建变量,xyhttps://blog.csdn.net/Y_principal/article/details/106080676
x = np.arange(0, 50, 0.01)
y = np.sin(2 * np.pi * x)
# #【2.5】matplot部分,打印第一行的窗口显示内容
a.plot(x,y,'-r')
#
#【3】matplot画折线段动图部分,这部分留做本地数据的读入
#开启interactive mode 成功的关键函数
plt.ion()
#【3.1】读取本地文件的名字
try:
text =open("tk_test_input.txt",'r')
text.close()
except IOError:
text = open("tk_test_input.txt",'w')
data=['1,2','2,4','3,7','4,9','5,10','6,15','90,100','100,150','220,400','300,600']
for i in data:
text.write(i+'\n')
text.close()
filename = 'tk_test_input.txt'
step, dis, gan = [], [], []
with open(filename, 'r') as line:
# 【3.2】按行读取
lines = line.readlines()
# j用于判断读了多少条
j = 0#https://blog.csdn.net/Y_principal/article/details/106080676
for line in lines:
#plt.clf() #清空画布上的所有内容
#【3.3】分割txt文档里的内容
t = line.split(',')
print(t)
# t = temp[1].split(',')
# step.append(j)
j = j + 1
# #【3.4】把dis和gan作为x和y轴
dis.append(float(t[0]))
gan.append(float(t[1]))
print(t[1])
# #【3.5】matplot显示在第二行
b.plot(dis,gan,'-r')
# #【3.6】把这个图显示在matplot部分的第二行
plt.pause(0.1)
plt.show() # 展示
#【1.8】tkinter部分的窗口显示,这部分应该就是用于把a和b两个窗口显示到tk的ui界面窗口上去。
#将绘制的图形显示到tkinter---创建属于root的canvas画布,并将图f置于画布上
canvas = FigureCanvasTkAgg(f, master=root)
canvas.draw() # 注意show方法已经过时了,这里改用draw
#【1.9】下面两种布局方式二选一
canvas.get_tk_widget().grid(row=5, column=0)
# canvas.get_tk_widget().pack(side=tkinter.TOP, # 上对齐
# fill=tkinter.BOTH, # 填充方式
# expand=tkinter.YES) # 随窗口大小调整而调整
# 主循环https://blog.csdn.net/Y_principal/article/details/106080676
#【1.10】tkinter 必须有的部分
root.mainloop()
搞了一个小工具,哈哈哈纯属娱乐O(∩_∩)O哈哈~
仅供学习下交流,如有侵权联系删除。
from matplotlib import pyplot as plt
import tkinter
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import json
from tkinter import *
import requests
import os
#【1】tkinter https://blog.csdn.net/Y_principal/article/details/106080676
#【1.1】创建tkinter的主窗口
root = tkinter.Tk()
#【1.2】tkinter窗口名
root.title("tk窗口(翻译以及画图)")
#【1.3】设置tkinter窗口名大小
root.geometry('500x100') # 宽 高 这里的乘是小x
#【1.7.1】翻译按钮出发的函数
def fanyi():
content = entry1.get().strip() # 把翻框里需要翻译的内容get过来
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
data = { # data是截图里的from data里的内容
'i': content,
'doctype': 'json'
}
r = requests.post(url, data=data) # requests的post,和get的主要区别,个人觉得是post可以用来提交一些数据,看附录部分解释
ret = r.content.decode()
result = json.loads(ret)
# print(result)
res.set(result['translateResult'][0][0]['tgt'])
# print(content)
#【1.4】设置标签,可以不要 https://blog.csdn.net/Y_principal/article/details/106080676
# Label(root, text="输入任容:", font=('GB2312', 18), fg='blue').grid(row=1, column=0)
# Label(root, text="结果:", font=('GB2312', 18), fg='blue').grid(row=2, column=0)
#【1.5】设置文本框
res_entry1 = StringVar()
#entry1=Text(root, width=25,height=3,font=('GB2312', 18))
entry1 = Entry(root, font=('GB2312', 18),width=30,textvariable=res_entry1)
res_entry1.set('input your text here')#给一个默认显示
entry1.grid(row=1, column=0)
#【1.6】
res = StringVar()
#entry2=Text(root, width=25,height=3,font=('GB2312', 18))
entry2 = Entry(root, font=('GB2312', 18), textvariable=res,width=30)
res.set('翻译结果')
entry2.grid(row=2, column=0)
#【1.7】 设置按钮https://blog.csdn.net/Y_principal/article/details/106080676
Button(root, text="翻译", width=10, font=('GB2312', 18), command=fanyi).grid(row=4, column=0, sticky=W)
Button(root, text="退出", width=10, font=('GB2312', 18), command=root.quit).grid(row=4, column=0, sticky=E)
# 生成用于绘sin图的数据,这个接口可以用于以后实时数据的输入
#【2】matplot部分https://blog.csdn.net/Y_principal/article/details/106080676
#【2.1】matplot部分,创建一个figure
f=plt.figure(figsize=(5, 4))
#【2.2】把figure分为三行一列
a = plt.subplot(2, 1, 1)
plt.xlabel('x time')
plt.ylabel('y cm')
b = plt.subplot(2, 1, 2)
plt.xlabel('x time')
plt.ylabel('y cm')
#c = plt.subplot(3, 1, 3)
#【2.3】为第一行窗口创建名字
a.set_title("a")
#【2.4】创建变量,xyhttps://blog.csdn.net/Y_principal/article/details/106080676
x = np.arange(0, 50, 0.01)
y = np.sin(2 * np.pi * x)
# #【2.5】matplot部分,打印第一行的窗口显示内容
a.plot(x,y,'-r')
#
#【3】matplot画折线段动图部分,这部分留做本地数据的读入
#开启interactive mode 成功的关键函数
plt.ion()
#【3.1】读取本地文件的名字
try:
text =open("tk_test_input.txt",'r')
text.close()
except IOError:
text = open("tk_test_input.txt",'w')
data=['1,2','2,4','3,7','4,9','5,10','6,15','90,100','100,150','220,400','300,600']
for i in data:
text.write(i+'\n')
text.close()
filename = 'tk_test_input.txt'
step, dis, gan = [], [], []
with open(filename, 'r') as line:
# 【3.2】按行读取
lines = line.readlines()
# j用于判断读了多少条
j = 0#https://blog.csdn.net/Y_principal/article/details/106080676
for line in lines:
#plt.clf() #清空画布上的所有内容
#【3.3】分割txt文档里的内容
t = line.split(',')
print(t)
# t = temp[1].split(',')
# step.append(j)
j = j + 1
# #【3.4】把dis和gan作为x和y轴
dis.append(float(t[0]))
gan.append(float(t[1]))
print(t[1])
# #【3.5】matplot显示在第二行
b.plot(dis,gan,'-r')
# #【3.6】把这个图显示在matplot部分的第二行
plt.pause(0.1)
plt.show() # 展示
#【1.8】tkinter部分的窗口显示,这部分应该就是用于把a和b两个窗口显示到tk的ui界面窗口上去。
#将绘制的图形显示到tkinter---创建属于root的canvas画布,并将图f置于画布上
canvas = FigureCanvasTkAgg(f, master=root)
canvas.draw() # 注意show方法已经过时了,这里改用draw
#【1.9】下面两种布局方式二选一
canvas.get_tk_widget().grid(row=5, column=0)
# canvas.get_tk_widget().pack(side=tkinter.TOP, # 上对齐
# fill=tkinter.BOTH, # 填充方式
# expand=tkinter.YES) # 随窗口大小调整而调整
# 主循环https://blog.csdn.net/Y_principal/article/details/106080676
#【1.10】tkinter 必须有的部分
root.mainloop()
====================
打包
pyinstaller -F -w UI.PY