Untitled
unknown
plain_text
3 years ago
7.5 kB
5
Indexable
from tkinter import * from pymodbus.client.sync import ModbusTcpClient from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk) from datetime import datetime from matplotlib import pyplot as plt from matplotlib.animation import FuncAnimation import openpyxl from tkinter import filedialog import tkinter as tk from tkinter import ttk # plot function is created for # plotting the graph in # tkinter window wb = openpyxl.load_workbook('temp_values_3.xlsx') # Get workbook active sheet # from the active attribute. # Create browse file window def browseFiles(): filename = filedialog.askopenfilename(initialdir = "/", title = "Select a File", filetypes = (("Text files","*.txt*"),( "all files","*.*"))) # Change label contents label_file_explorer.configure(text="File Opened: "+filename) # Create the root window window = Tk() # Set window title window.title('File Explorer') # Set window size window.geometry("500x500") #Set window background color window.config(background = "white") # Create a File Explorer label label_file_explorer = Label(window,text = "File Explorer using Tkinter", width = 100, height = 4,fg = "blue") # create button to browse files button_explore = Button(window,text = "Browse Files", command = browseFiles) # create button to exit button_exit = Button(window,text = "Exit", command = exit) # Grid method is chosen for placing the widgets #at respective positions in a table like structure #by specifying rows and columns label_file_explorer.grid(column = 1, row = 1) button_explore.grid(column = 1, row = 2) button_exit.grid(column = 1,row = 3) # excel sheet sheet = wb.active # x-axis values x_1=[] x_2=[] x_3=[] x_4=[] x_5=[] x_6=[] x_7=[] x_8=[] # y-axis values y=[] y_2=[] y_3=[] y_4=[] y_5=[] y_6=[] y_7=[] y_8=[] j=0 row=sheet.max_row #number of rows column=1 # connect to the module using its IP adress client = ModbusTcpClient('192.168.127.254',502) print(client.connect()) # adress of channel register reg_1= 2049 reg_2= 2051 reg_3= 2053 reg_4= 2055 reg_5= 2057 reg_6= 2059 reg_7= 2061 reg_8= 2063 #window size of graph window fig= Figure(figsize = (10,10), dpi = 100) #graph plot of different channels respectively plot1 = fig.add_subplot(3,3,1 , facecolor='orange') #set colur of the graph plot1.set_title('Channel 1', color='green') #set colour of the tittle plot2 = fig.add_subplot(3,3,2) plot2.set_title('Channel 2') plot3 = fig.add_subplot(3,3,3) plot3.set_title('Channel 3') plot4 = fig.add_subplot(3,3,4) plot4.set_title('Channel 4') plot5 = fig.add_subplot(3,3,5) plot5.set_title('Channel 5') plot6 = fig.add_subplot(3,3,6) plot6.set_title('Channel 6') plot7 = fig.add_subplot(3,3,7) plot7.set_title('Channel 7') plot8 = fig.add_subplot(3,3,8) plot8.set_title('Channel 8') # run run = False # start button def start(): global run run= True # stop button def stop(): global run run= False # append data in excel sheet for respective register (Channel) def data(i): global row,column,j if run: # read values Reg_1=client.read_input_registers(reg_1) # Real time data fron input register x_1.append(Reg_1.registers) # values of given register values in x y.append(datetime.now()) # current date and time in y Reg_2=client.read_input_registers(reg_2) x_2.append(Reg_2.registers) y_2.append(datetime.now()) Reg_3=client.read_input_registers(reg_3) x_3.append(Reg_3.registers) y_3.append(datetime.now()) Reg_4=client.read_input_registers(reg_4) x_4.append(Reg_4.registers) y_4.append(datetime.now()) Reg_5=client.read_input_registers(reg_5) x_5.append(Reg_5.registers) y_5.append(datetime.now()) Reg_6=client.read_input_registers(reg_6) x_6.append(Reg_6.registers) y_6.append(datetime.now()) Reg_7=client.read_input_registers(reg_7) x_7.append(Reg_7.registers) y_7.append(datetime.now()) Reg_8=client.read_input_registers(reg_8) x_8.append(Reg_8.registers) y_8.append(datetime.now()) # plot graph of different channels plot1.cla() plot1.plot(y,x_1, color= 'green') # plot graph using the values of x and y axis , colur of the graph plot1.align_xlabelsylabel('Time') # label of x axis plot1.align_ylabelsylabel('Temperature') # label of y axis plot2.cla() plot2.plot(y_2,x_2) plot3.cla() plot3.plot(y_3,x_3 ) plot4.cla() plot4.plot(y_4,x_4) plot5.cla() plot5.plot(y_5,x_5) plot6.cla() plot6.plot(y_6,x_6) plot7.cla() plot7.plot(y_7,x_7) plot8.cla() plot8.plot(y_8,x_8) # store the appended values of different channels in the excel sheet active workbook sheet.cell(row, 1).value=y[j] sheet.cell(row, 2).value=x_1[j][0] sheet.cell(row, 3).value=x_2[j][0] sheet.cell(row, 4).value=x_3[j][0] sheet.cell(row, 5).value=x_4[j][0] sheet.cell(row, 6).value=x_5[j][0] sheet.cell(row, 7).value=x_6[j][0] sheet.cell(row, 8).value=x_7[j][0] sheet.cell(row, 9).value=x_8[j][0] row+=1 # rows diffined for the storage of data j+=1 # Columns diffined for the storage of data wb.save('temp_values_3.xlsx') #Name of the excel sheet where the data is stored # plotting of the graph def plot(): canvas = FigureCanvasTkAgg(fig,master = window) canvas.draw() # placing the canvas on the Tkinter window canvas.get_tk_widget().pack() # creating the Matplotlib toolbar toolbar = NavigationToolbar2Tk(canvas,window) toolbar.update() # placing the toolbar on the Tkinter window canvas.get_tk_widget().pack() # the main Tkinter window window = Tk() # setting the title window.title('Plotting in Tkinter') # Creating window window.geometry("8000x8000") # dimension of the window window.config(bg='light blue') #colour of the window #Creating buttons # start button, text of the button, colur of the button, command of the button w = Button (window,text = "Start" ,bg='green', command= start) # stop button, text written on the button, colour of the button, command of the button v = Button (window,text = "Stop",bg='red', command = stop ) # button that displays the plot v.pack() #start button pack w.pack() #stop button pack g=plot() #buttons defined in the plot # function animation to display graph in real time ani= FuncAnimation(fig,data,1000) # run the gui window.mainloop()
Editor is loading...