整個代碼在jupyter notebook下運行
整個系統需要用到的庫文件
#import pymysql
from tkinter import *
import tkinter as tk
import tkinter.messagebox as tkMessageBox
from PIL import Image, ImageFont,ImageTk
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np
import xlrd
import pandas as pd
import subprocess
若要使用數據庫進行註冊功能的實現的話,推薦使用python自帶的sqlite3,這個比MySQL方便;如果使用MySQL需要安裝pymysql庫
一、登陸界面
root = tk.Tk()
root.title('登陸')
f = Figure(figsize=(2.52, 2.56), dpi=100)
f_plot = f.add_subplot(111)#定義畫布中的位置
e1 = tk.StringVar(root,value='')
e2 = tk.StringVar(root,value='')
tk.Label(root, text="賬號:",font=20).grid(row=0, column=0)
tk.Label(root, text="密碼:",font=20).grid(row=1, column=0)
entry_user =tk.Entry(root,textvariable=e1).grid(row=0, column=1)
entry_passwd=tk.Entry(root, show="●",textvariable=e2).grid(row=1, column=1)
tk.Button(root, text="登 陸", font=12,
width=10, command=show).grid(row=2, column=0)
tk.Button(root, text="注 冊", font=12,
width=10, command=register
).grid(row=2, column=1)
tk.Button(root, text="退 出", font=12,
width=10, command=root.destroy).grid(row=2, column=2)
root.mainloop()
效果圖:
這裏註冊功能沒有做,有興趣的小夥伴可以自己嘗試一下
二、登陸功能界面
if e1.get() == '1'and e2.get() == '1':
str1 = tk.messagebox.showinfo(title='提示',
message = '恭喜密碼正確')
if str1 == 'ok':
root.destroy()
root1 = tk.Tk()
root1.title('橋樑監測信息關聯分析可視化系統')
canvas = tk.Canvas(root1,
width = 800,
height = 470,
bg = 'black')
image = Image.open("圖片位置")
im = ImageTk.PhotoImage(image)
canvas.create_image(400,235,image = im)
canvas.pack()
buttonOk = tk.Button(root1,text = '****************▂▃▅▆▇▇▇▇數據可視化(週期估算)▇▇▇▇▆▅▃▂****************'
,command=UI)
buttonOk.place(x = 0,y = 370,width =800,height = 50)
buttonOk = tk.Button(root1,text = '****************▂▃▅▆▇▇▇▇選擇互相關(相關係數)▇▇▇▇▆▅▃▂****************'
,command=UI2)
buttonOk.place(x = 0,y = 420,width =800,height = 50)
root1.mainloop()
else :
string_a = '密碼或賬號錯誤 ,請重新輸入!'
tkMessagebox.showinfo(title='提示', message = string_a)
這裏我直接設置了賬號和密碼:1,1
三、數據自相關功能
1.定義一個界面
def UI():
# data = xlrd.open_workbook(r'F:/交通物聯網/naodu625_strain_temp.xls')
# sh1 = data.sheet_by_name('溫度')
# sh2 = data.sheet_by_name('撓度')
# sh3 = data.sheet_by_name('應變')
bk = xlrd.open_workbook('F:/交通物聯網/naodu625_strain_temp.xls')
#通過索引順序獲取 sheet
sh = bk.sheets()[0]
root2=tk.Tk()
root2.title('數據可視化')
# var = tk.StringVar()
root2.geometry("500x500")
canvs = FigureCanvasTkAgg(f, root2)#f是定義的圖像,root是tkinter中畫布的定義位置
canvs.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
Label(root2, text="*位移點A~R,溫度點A~M,應變點1~44*",font=20).pack()
Label(root2, text="選擇溫度",font=20).pack()
test1=Entry(root2)
test1.pack()
Button(root2, text='選擇測試點', command=draw_T).pack()
Label(root2, text="選擇位移",font=20).pack()
test2=Entry(root2)
test2.pack()
Button(root2, text='選擇測試點', command=draw_D).pack()
Label(root2, text="選擇應變",font=20).pack()
test3=Entry(root2)
test3.pack()
Button(root2, text='選擇測試點', command=draw_Y).pack()
root2.mainloop()
2.溫度可視化界面
def draw_T():
f_plot.clear()
t = ord(test1.get()) #用字母表示時
if t in (65,82):
test_TEM=t-65
if test_TEM<13:
col_test_TEM = sh.col_values(test_TEM+19,start_rowx=1)
x=np.arange(0,1078,1)
y=np.array(col_test_TEM)
f_plot.set(title='temprature_self',
xlabel='time/10min',ylabel='temprature')
f_plot.plot(x,y)
canvs.draw()
else:
tk.messagebox.showinfo(title='提示',message='*位移點A~R,溫度點A~M,應變點1~44*')
3.位移可視化界面
def draw_D():
f_plot.clear()
d = ord(test2.get()) #用字母表示時
if d in (65,77):
test_VD = d-65
if test_VD<18:
col_test_VD = sh.col_values(test_VD+19,start_rowx=1)
x=np.arange(0,1078,1)
y=np.array(col_test_VD)
f_plot.set(title='ver_d_self',
xlabel='time/10min',ylabel='ver_d')
f_plot.plot(x,y)
canvs.draw()
else:
tk.messagebox.showinfo(title='提示', message = '*位移點A~R,溫度點A~M,應變點1~44*')
4.應變可視化界面
def draw_Y():
f_plot.clear()
#y = ord(var.get()) #用字母表示時
if test3.get().isdigit():
test_YB = int(test3.get())-1
else:
tk.messagebox.showinfo(title='提示', message = '*位移點A~R,溫度點A~M,應變點1~44*')
if test_YB<44 and test_YB>=0:
col_test_YB = sh.col_values(test_YB+19,start_rowx=1)
x=np.arange(0,1078,1)
y=np.array(col_test_YB)
f_plot.set(title='YB_self',xlabel='time/10min',ylabel='YingBian')
f_plot.plot(x,y)
canvs.draw()
else:
str1 = tkMessagebox.showinfo(title='提示',message = '輸入範圍錯誤,請重新輸入')
if str1 == 'ok':
return
上述功能已經實驗過,可以正常運行,不過需要自己進行整理
四、數據互相關功能
1.定義界面
def UI2():
data = xlrd.open_workbook(r'F:/交通物聯網/naodu625_strain_temp.xls')
sh1 = data.sheet_by_name('溫度')
sh3 = data.sheet_by_name('撓度')
sh2 = data.sheet_by_name('應變')
root3=tk.Tk()
root3.title('數據可視化')
# var = tk.StringVar()
root3.geometry("700x700")
f = Figure(figsize=(2.52, 2.56), dpi=100)#figsize定義圖像大小,dpi定義像素
f_plot1 = f.add_subplot(111)#定義畫布中的位置
canvs = FigureCanvasTkAgg(f, root3)#f是定義的圖像,root是tkinter中畫布的定義位置
canvs.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
Label(root3,text="*位移點A-R,溫度點A-M,應變點1-44*",font=20).pack()
Label(root3,text="選擇溫度",font=20).pack()
test1=Entry(root3)
test1.pack()
#複選框
chVarDis =IntVar(root3) # 用來獲取複選框是否被勾選,通過chVarDis.get()來獲取其的狀態,其狀態值爲int類型 勾選爲1 未勾選爲0
check1 = Checkbutton(root3, text="在TXT中顯示具體相關係數", variable=chVarDis) # text爲該複選框後面顯示的名稱, variable將該複選框的狀態賦值給一個變量,當state='disabled'時,該複選框爲灰色,不能點的狀態
check1.deselect() # 該複選框是否勾選,select爲勾選, deselect爲不勾選
check1.pack()
Button(root3, text='選擇測試點', command=draw_D_T).pack()
Label(root3,text="選擇位移",font=20).pack()
test2=Entry(root3)
test2.pack()
chvarUn =IntVar(root3)
check2 =Checkbutton(root3, text="在TXT中顯示具體相關係數", variable=chvarUn)
check2.deselect()
check2.pack()
Button(root3,text='選擇測試點',command=draw_D_Y).pack()
Label(root3,text="選擇應變",font=20).pack()
test3=Entry(root3)
test3.pack()
chvarEn =IntVar(root3)
check3 =Checkbutton(root3, text="在TXT中顯示具體相關係數", variable=chvarEn)
check3.deselect()
check3.pack()
Button(root3, text='選擇測試點',command=draw_Y_T).pack()
2.位移&溫度互相關功能
def draw_D_T():
f_plot1.clear()
d1 = ord(test1.get())
if d1>82:
str1 = tk.messagebox.showinfo(title='提示',message = '輸入範圍錯誤,請重新輸入')
if str1 == 'ok':
return
test_VD1 = d1-65
if test_VD1<18:
col_test_VD1 = sh3.col_values(test_VD1)
s_test=pd.Series(col_test_VD1[1:])
corr_nm1 = []
X=[]
open('D_T.txt','w').close()
for j in range(1,12):
X.insert(j,j)
col3 = sh1.col_values(j)
s3=pd.Series(col3[1:])
corr_test=s_test.corr(s3)
if corr_test<0:
corr_nm1.append(-corr_test)
else:
corr_nm1.append(corr_test)
if chVarDis.get()==1:
result2txt=str('位移'+str(test1.get()) +'測點&溫度'+str(j) +'測點的相關係數:'+str(corr_nm1[j-1])) # data是前面運行出的數據,先將其轉爲字符串才能寫入
with open('D_T.txt','a',encoding='utf8') as file_handle: # .txt可以不自己新建,代碼會自動新建
file_handle.write(result2txt) # 寫入
file_handle.write('\n') # 有時放在循環裏面需要自動轉行,不然會覆蓋上一條數據
if j==11:
subprocess.Popen("notepad.exe D_T.txt")
f_plot1.set(title='ver_d&temprature',
xlabel='ver_d&temperature',ylabel='corr_num')
f_plot1.bar(x=X, height=corr_nm1,width=0.5,)
canvs.draw()
3.位移&應變互相關功能
def draw_D_Y():
f_plot1.clear()
d2 = ord(test2.get())
if d2>77:
str1 = tk.messagebox.showinfo(title='提示',message = '輸入範圍錯誤,請重新輸入')
if str1 == 'ok':
return
test_VD2 = d2-65
if test_VD2<18:
col_test_VD2 = sh3.col_values(test_VD2)
s_test1=pd.Series(col_test_VD2[1:])
corr_nm2 = []
X=[]
open('D_Y.txt', 'w').close()
for i in range(0,44):
X.insert(i,i)
col_y = sh2.col_values(i)
s_y=pd.Series(col_y[1:])
corr_test1=s_test1.corr(s_y)
if corr_test1<0:
corr_nm2.append(-corr_test1)
else :
corr_nm2.append(corr_test1)
if chvarUn.get()==1:
result2txt=str('位移'+str(test2.get()) +'測點&應變'+str(i+1) +'測點的相關係數:'+str(corr_nm2[i])) # data是前面運行出的數據,先將其轉爲字符串才能寫入
with open('D_Y.txt','a',encoding='utf8') as file_handle: # .txt可以不自己新建,代碼會自動新建
file_handle.write(result2txt) # 寫入
file_handle.write('\n') # 有時放在循環裏面需要自動轉行,不然會覆蓋上一條數據
if i==43:
subprocess.Popen("notepad.exe D_Y.txt")
f_plot1.set(title='ver_d&YB',
xlabel='ver_d&YB',ylabel='corr_num')
f_plot1.bar(x=X,height=corr_nm2,width=0.5,)
canvs.draw()
4.應變&溫度互相關功能
def draw_Y_T():
f_plot1.clear()
if test3.get().isdigit():
test_YB1 = int(test3.get())-1
else :
messagebox.showinfo(title='提示',message = '輸入的測試點超出範圍,請重新輸入!')
if test_YB1<44 and test_YB1>=0:
col_test_YB1 = sh2.col_values(test_YB1)
s_test2=pd.Series(col_test_YB1[1:])
corr_nm3 = []
X=[]
open('Y_T.txt', 'w').close()
for k in range(1,12):
X.insert(k-1,k-1)
col_yt = sh3.col_values(k)
s_yt=pd.Series(col_yt[1:])
corr_test2=s_test2.corr(s_yt)
if corr_test2<0:
corr_nm3.append(-corr_test2)
else :
corr_nm3.append(corr_test2)
if chvarEn.get()==1:
result2txt=str('應變'+str(test3.get()) +'測點&溫度'+str(k) +'測點的相關係數:'+str(corr_nm3[k-1])) # data是前面運行出的數據,先將其轉爲字符串才能寫入
with open('Y_T.txt','a',encoding='utf8') as file_handle: # .txt可以不自己新建,代碼會自動新建
file_handle.write(result2txt) # 寫入
file_handle.write('\n') # 有時放在循環裏面需要自動轉行,不然會覆蓋上一條數據
if k==11:
subprocess.Popen("notepad.exe Y_T.txt")
f_plot1.set(title='YB&temprature', xlabel='YB&temperature',ylabel='corr_num')
f_plot1.bar(x=X,height=corr_nm3,width=0.5,)
canvs.draw()
else:
str1 = tk.messagebox.showinfo(title='提示',message = '輸入範圍錯誤,請重新輸入')
if str1 == 'ok':
return
同樣這裏的代碼同樣需要整理才能成功運行
若有小夥伴找到處理數據更加簡便的方法也請告訴我,我會將源碼和數據上傳,源碼比這個囉嗦,而且還有沒有用到的,比如註冊功能的代碼,感謝各位看官的指教!