# -*- coding: utf-8 -*-
import tkinter as tk
import serial as sl
import time
import datetime as dt
class App(tk.Frame):
finFlag = True
serial = None
def __init__(self, master=None):
super().__init__(master)
self.master.protocol('WM_DELETE_WINDOW', self.On_Close)
#Here, decide the serial communication setting arbitrarily
self.serial = sl.Serial('COM7', 19200, timeout=0)
while( self.serial.is_open is False):
time.sleep(100)
self.createPage()
self.after(10, self.readData)
def createPage(self):
self.page1 = tk.Frame(self.master)
self.page1.pack(pady=5, padx=30)
self.timelabel = tk.Label(self.page1)
self.timelabel.grid(column=0, row=0)
self.timelabel['text'] = dt.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
self.label = tk.Label(self.page1, font=("",20), relief='ridge')
self.label.grid(column=1, row=0)
self.label['text'] = '00.0A'
def readData(self):
line = None
self.timelabel['text'] = dt.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
if ( self.serial.is_open):
line = self.serial.readline()
line = line.strip().decode('utf-8')
if( (line is not None) and (line != '')):
print(line)
self.label['text'] = line
if ( self.finFlag ):
self.after(10, self.readData)
def On_Close(self):
self.finFlag = False
self.serial.close()
self.master.destroy()
def main():
root = tk.Tk()
app = App(root)
app.mainloop()
if __name__ == '__main__':
main()
Recommended Posts