-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_gui.py
207 lines (174 loc) · 6.75 KB
/
main_gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import tkinter as tk #importing tkinter
import tkinter.font as font
from PIL import ImageTk, Image
import os
from datas import database
from tkinter import Menu
import serial
import time
import csv
import matplotlib
matplotlib.use("tkAgg")
import matplotlib.pyplot as plt
import numpy as np
y_var = np.array(np.zeros([20]))
fig = ""
ax = ""
line =""
times = 1
arr = []
ser = ""
def serial_read(root,port_num, baud_rate):
global ser
ser = serial.Serial()
ser.baudrate = baud_rate
ser.port = port_num
ser.port : port_num
ser.open()
root.after(2, serial_read2(root))
def serial_read2(root):
global y_var, arr, fig, ax, line, times, ser
try:
ser_bytes = ser.readline()
decoded_bytes = ser_bytes.decode('utf-8')
print("\n"+str(decoded_bytes))
arr.append(decoded_bytes)
print(len(arr))
print("============="+str(times))
if len(arr) == 5:
print(type(arr[2]))
print("Data needed:"+str(arr[2]))
y_var = np.append(y_var,int(arr[2]))
y_var = y_var[1:20+1]
line.set_ydata(y_var)
ax.relim()
ax.autoscale_view()
fig.canvas.draw()
fig.canvas.flush_events()
arr = []
times = times + 1
root.after(1, serial_read2(root))
except:
print("Keyboard Interrupt")
y_var = np.array(np.zeros([20]))
fig = ""
ax = ""
line =""
times = 1
arr = []
ser = ""
def open_data(window):
database(window)
def donothing():
return
#start reading the data from serial port
def read(window,port_num, baud_rate):
global fig, ax, line
y_var = np.array(np.zeros([20]))
plt.ion()
fig, ax = plt.subplots()
line, = ax.plot(y_var)
serial_read(window,port_num, baud_rate) #read the data
def main_read(win):
win.destroy()
#window description
window = tk.Tk(screenName="Main", baseName="What is this??", useTk=1)
window.title('Hello, Tkinter!')
window.geometry('500x300') # Size 200, 200
window.resizable(False, False) #dont resize the window
#creating menubar
menubar = Menu(window)
filemenu = Menu(menubar, tearoff=0)
#add submenu
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=donothing)
menubar.add_cascade(label="File", menu=filemenu) #add the menu
#action submenu
action_menu = Menu(menubar, tearoff=0)
action_menu.add_command(label="Read", command= lambda: main_read(window))
action_menu.add_command(label="Show database", command= lambda: main_database(window))
menubar.add_cascade(label="Action", menu=action_menu)
img = ImageTk.PhotoImage(Image.open("dave.jpg")) #icon image
panel = tk.Label(window, image = img) #set the image
panel.place(relx = 0.44, rely = 0.1)
#label and entry defining
port_num = tk.Label(window, text="Port Number")
port_num.place(relx = 0.05, rely = 0.42)
port_num_entry = tk.Entry(window, width = 29)
port_num_entry.place(relx=0.3, rely=0.42)
baud_rate_label = tk.Label(window, text="Baudrate")
baud_rate_label.place(relx = 0.05, rely = 0.52)
baud_rate_entry = tk.Entry(window, width = 29)
baud_rate_entry.place(relx=0.3, rely=0.52)
#button defining
button_read = tk.Button(window, text = "Read", width = 25, bg = 'green', command = lambda: read(window,port_num_entry.get(),baud_rate_entry.get()))
button_read.place(relx = 0.25, rely = 0.65)
button_stop = tk.Button(window, text='Stop', width=25, bg = 'green',command=window.destroy)
button_stop.place(relx=0.25, rely=0.78)
#defining the font of the button and label
myFont = font.Font(family='Helvetica', size=10, weight='bold')
button_stop['font'] = myFont
button_read['font'] = myFont
port_num_entry['font'] = myFont
baud_rate_entry['font'] = myFont
window.config(menu = menubar)
window.mainloop()
def main_database(win):
win.destroy()
#window description
window = tk.Tk(screenName="Main", baseName="What is this??", useTk=1)
window.title('Hello, Tkinter!')
window.geometry('500x400') # Size 200, 200
window.resizable(False, False) #dont resize the window
#creating menubar
menubar = Menu(window)
filemenu = Menu(menubar, tearoff=0)
#add submenu
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=donothing)
menubar.add_cascade(label="File", menu=filemenu) #add the menu
#action submenu
action_menu = Menu(menubar, tearoff=0)
action_menu.add_command(label="Read", command= lambda: main_read(window))
action_menu.add_command(label="Show database", command= lambda: main_database(window))
menubar.add_cascade(label="Action", menu=action_menu)
img = ImageTk.PhotoImage(Image.open("dave.jpg")) #icon image
panel = tk.Label(window, image = img) #set the image
panel.place(relx = 0.44, rely = 0.1)
#label and entry defining
database_hostName = tk.Label(window, text="Database Hostname:")
database_hostName.place(relx = 0.05, rely = 0.4)
database_hostName_entry = tk.Entry(window, width = 29)
database_hostName_entry.place(relx=0.3, rely=0.4)
database_username_label = tk.Label(window, text="Database Username:")
database_username_label.place(relx = 0.05, rely = 0.47)
database_username_entry = tk.Entry(window, width = 29)
database_username_entry.place(relx=0.3, rely=0.47)
password_label = tk.Label(window, text = "Database Password:")
password_label.place(relx = 0.05, rely = 0.54)
password_entry = tk.Entry(window, width = 29)
password_entry.place(relx = 0.3, rely = 0.54)
date_label = tk.Label(window, text = "Enter date:")
date_label.place(relx = 0.05, rely = 0.61)
date_entry = tk.Entry(window, width = 29)
date_entry.place(relx = 0.3, rely = 0.61)
#button defining
button_show_data = tk.Button(window, text='Show data', width=25, bg = 'green',command = lambda: open_data(window))
button_show_data.place(relx=0.25, rely=0.69)
button_stop = tk.Button(window, text='Stop', width=25, bg = 'green',command=window.destroy)
button_stop.place(relx=0.25, rely=0.78)
#defining the font of the button and label
myFont = font.Font(family='Helvetica', size=10, weight='bold')
button_stop['font'] = myFont
button_show_data['font'] = myFont
database_hostName_entry['font'] = myFont
database_username_entry['font'] = myFont
password_entry['font'] = myFont
date_entry['font'] = myFont
window.config(menu = menubar)
window.mainloop()
if __name__ == "__main__":
root = tk.Tk()
main_database(root)