質問編集履歴

1

質問内容を更新しました。

2021/10/29 11:43

投稿

hinata_0123
hinata_0123

スコア1

test CHANGED
File without changes
test CHANGED
@@ -2,361 +2,365 @@
2
2
 
3
3
  コード
4
4
 
5
+
6
+
7
+ import tkinter as tk
8
+
9
+ # tkinterモジュールをtkとしてインポート
10
+
11
+ import tkinter.ttk as ttk
12
+
13
+ # tkinterのttkをttkとしてインポート
14
+
15
+ import matplotlib.pyplot as plt
16
+
17
+ # matplotlibモジュールのpyplotをpltとしてインポート
18
+
19
+ from matplotlib.backends.backend_tkagg import (
20
+
21
+ FigureCanvasTkAgg, NavigationToolbar2Tk)
22
+
23
+ # FigureCanvasTkAggとNavigationToolbar2Tkのインポート
24
+
25
+ from random import random
26
+
27
+ # randomモジュールからrandom関数をインポート
28
+
29
+ from datetime import datetime
30
+
31
+ # datetimeモジュールからdatetime関数のインポート
32
+
33
+ from functools import partial
34
+
35
+ # functoolsモジュールからpartialのインポート
36
+
37
+ import pymysql.cursors
38
+
39
+ # pymysqlモジュールのcursorsをインポート
40
+
41
+ #--------------------------------------------------------------------
42
+
43
+ def select(yyyy,MM,dd,lid): # select関数
44
+
45
+ connection = pymysql.connect(
46
+
47
+ host='localhost',
48
+
49
+ user='hit',
50
+
51
+ password='hit',
52
+
53
+ db='THT',
54
+
55
+ charset='utf8',
56
+
57
+ cursorclass=pymysql.cursors.DictCursor)
58
+
59
+
60
+
61
+ with connection.cursor() as cursor:
62
+
63
+ FROM = yyyy+"-"+MM+"-"+dd+" 00:00:00"
64
+
65
+ TO = yyyy+"-"+MM+"-"+dd+" 23:00:00"
66
+
67
+ sql = "select * from thData where lid="+str(lid)
68
+
69
+ sql += " and date>='"+FROM+"' and date<='"+TO+"'"
70
+
71
+ print("sql=",sql)
72
+
73
+
74
+
75
+ cursor.execute(sql)
76
+
77
+ results = cursor.fetchall()
78
+
79
+ #for r in results: print(r)
80
+
81
+ return results
82
+
83
+ #--------------------------------------------------------------------
84
+
85
+ def make_graph(): # make_graph関数
86
+
87
+ #print("<< make_graph start >>")
88
+
89
+ x = [0]*24
90
+
91
+ y1 = [None]*24
92
+
93
+ y2 = [0]*24
94
+
95
+ yyyy = cb1.get()
96
+
97
+ m = int(cb2.get())
98
+
99
+ d = int(cb3.get())
100
+
101
+ MM = "{:02d}".format(m)
102
+
103
+ dd = "{:02d}".format(d)
104
+
105
+ lid = 1
106
+
107
+
108
+
109
+ #print("yyyy=",yyyy," ",type(yyyy))
110
+
111
+ #print("MM=",MM," ",type(MM))
112
+
113
+ #print("dd=",dd," ",type(dd))
114
+
115
+
116
+
117
+ #--------------------------------
118
+
119
+ results = select(yyyy,MM,dd,lid)
120
+
121
+ #--------------------------------
122
+
123
+ for i in range(0,24): x[i]=i
124
+
125
+ num = len(results)
126
+
127
+ print("num=",num)
128
+
129
+ if num == 0: y1 = [None]*24
130
+
131
+
132
+
133
+ for i in range(num):
134
+
135
+ if i > 23: break
136
+
137
+
138
+
139
+ temp = results[i].get('temp')
140
+
141
+ hcount = results[i].get('hcount')
142
+
143
+ print(i," ",temp," ",hcount)
144
+
145
+ y1[i] = temp
146
+
147
+ y2[i] = hcount
148
+
149
+
150
+
151
+ ax1.cla()
152
+
153
+ ax1.set_xlabel('Hour (H)')
154
+
155
+ ax1.set_ylabel('Temperature (deg)')
156
+
157
+ ax1.set_ylim(-5,25)
158
+
159
+ ax1.plot(x,y1,color="blue",marker="o")
160
+
161
+
162
+
163
+ ax2.cla()
164
+
165
+ ax2.set_ylabel('Human detected (number)',color="red")
166
+
167
+ ax2.set_ylim(0,65)
168
+
169
+ ax2.bar(x,y2,color="red")
170
+
171
+
172
+
173
+ canvas.draw()
174
+
175
+ #print("<< make_graph end >>")
176
+
177
+
178
+
179
+ #-----------------------------------------------------------------
180
+
181
+ x = [0]*24
182
+
183
+ y1 = [None]*24
184
+
185
+ y2 = [0]*24
186
+
187
+
188
+
189
+ root = tk.Tk()
190
+
191
+ root.title("TEST")
192
+
193
+ root.geometry("650x580")
194
+
195
+
196
+
197
+ frame1 = tk.LabelFrame(
198
+
199
+ root,labelanchor="nw",
200
+
201
+ text="Temperture & Human Sensor",foreground="green")
202
+
203
+ # フレーム1
204
+
205
+ frame1.grid(rowspan=2,column=0)
206
+
207
+
208
+
209
+ frame2=tk.LabelFrame(root,text="Control", foreground="red")
210
+
211
+
212
+
213
+ frame2.grid(row=2,column=0,sticky="nwse")
214
+
215
+
216
+
217
+ fig = plt.Figure()
218
+
219
+ ax1 = fig.add_subplot(111)
220
+
221
+
222
+
223
+ ax1.plot(x,y1)
224
+
225
+ ax1.set_xlabel('Hour (H)')
226
+
227
+ ax1.set_ylabel('Temperature (deg)')
228
+
229
+ ax1.set_ylim(-5,25)
230
+
231
+
232
+
233
+ ax2 = ax1.twinx()
234
+
235
+ ax2.bar(x,y2,color="red")
236
+
237
+ ax2.set_ylabel('Human detected (number)',color="red")
238
+
239
+ ax2.set_ylim(0,65)
240
+
241
+
242
+
243
+ canvas = FigureCanvasTkAgg(fig, master=frame1)
244
+
245
+ # キャンバスのインスタンス化
246
+
247
+ canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
248
+
249
+
250
+
251
+ now = datetime.now()
252
+
253
+ date = "{:%Y-%m-%d}".format(now)
254
+
255
+ month = now.month
256
+
257
+ day = now.day
258
+
259
+
260
+
261
+ years = ["2020","2021","2022","2023","2024","2025"]
262
+
263
+ # 年(コンボボックス)
264
+
265
+ months = [1]*12
266
+
267
+ for i in range(12) : months[i]=i+1
268
+
269
+ days = [1]*31
270
+
271
+ for i in range(31) : days[i]=i+1
272
+
273
+ devices = ["810C3B31","790A2D12"]
274
+
275
+
276
+
277
+ lb1 = tk.Label(frame2, text="year:",bg="cyan")
278
+
279
+ cb1 = ttk.Combobox(frame2, values=years,width=6,justify=tk.CENTER)
280
+
281
+ cb1.current(0)
282
+
283
+
284
+
285
+ lb2 = tk.Label(frame2, text="month:",bg="cyan")
286
+
287
+ cb2 = ttk.Combobox(frame2, values = months,width=6,justify=tk.CENTER)
288
+
289
+ cb2.current(month-1)
290
+
291
+
292
+
293
+ lb3 = tk.Label(frame2, text="day:",bg="cyan") # ラベル3(lb3)
294
+
295
+ cb3 = ttk.Combobox(frame2, values = days,width=6,justify=tk.CENTER)
296
+
297
+ cb3.current(day-1)
298
+
299
+ lb4 = tk.Label(frame2, text="Device ID :",bg="cyan")
300
+
301
+ cb4 = ttk.Combobox(frame2, values= devices,width=6,justify=tk.CENTER)
302
+
303
+ cb4.current(0)
304
+
305
+
306
+
307
+ y = cb1.current()
308
+
309
+ m = cb2.current()
310
+
311
+ d = cb3.current()
312
+
313
+ selected_date = str(years[y])+"-"+str(months[m])+"-"+str(days[d])
314
+
315
+ print("selected_date=",selected_date)
316
+
317
+
318
+
319
+
320
+
321
+
322
+
323
+ print("cb1=",cb1.get()," ",end="")
324
+
325
+ print("cb2=",cb2.get()," ",end="")
326
+
327
+ print("cb3=",cb3.get())
328
+
329
+
330
+
331
+ bt1 = tk.Button(frame2, text="Display", bg="yellow",command=make_graph)
332
+
333
+
334
+
335
+ lb1.grid(row=1, column=0 ,padx=5, pady=5)
336
+
337
+ cb1.grid(row=1, column=1 ,padx=5, pady=5)
338
+
339
+ lb2.grid(row=1, column=2 ,padx=5, pady=5)
340
+
341
+ cb2.grid(row=1, column=3 ,padx=5, pady=5)
342
+
343
+ lb3.grid(row=1, column=4 ,padx=5, pady=5)
344
+
345
+ cb3.grid(row=1, column=5 ,padx=5, pady=5)
346
+
347
+ lb4.grid(row=1, column=6 ,padx=5, pady=5)
348
+
349
+ cb4.grid(row=1, column=7 ,padx=5, pady=5)
350
+
351
+ bt1.grid(row=1, column=8 ,padx=5, pady=5)
352
+
353
+
354
+
355
+ make_graph()
356
+
357
+
358
+
359
+ root.mainloop()
360
+
5
361
  ```
6
362
 
7
- import tkinter as tk
363
+
8
-
9
- # tkinterモジュールをtkとしてインポート
10
-
11
- import tkinter.ttk as ttk
12
-
13
- # tkinterのttkをttkとしてインポート
14
-
15
- import matplotlib.pyplot as plt
16
-
17
- # matplotlibモジュールのpyplotをpltとしてインポート
18
-
19
- from matplotlib.backends.backend_tkagg import (
20
-
21
- FigureCanvasTkAgg, NavigationToolbar2Tk)
22
-
23
- # FigureCanvasTkAggとNavigationToolbar2Tkのインポート
24
-
25
- from random import random
26
-
27
- # randomモジュールからrandom関数をインポート
28
-
29
- from datetime import datetime
30
-
31
- # datetimeモジュールからdatetime関数のインポート
32
-
33
- from functools import partial
34
-
35
- # functoolsモジュールからpartialのインポート
36
-
37
- import pymysql.cursors
38
-
39
- # pymysqlモジュールのcursorsをインポート
40
-
41
- #--------------------------------------------------------------------
42
-
43
- def select(yyyy,MM,dd,lid): # select関数
44
-
45
- connection = pymysql.connect(
46
-
47
- host='localhost',
48
-
49
- user='hit',
50
-
51
- password='hit',
52
-
53
- db='THT',
54
-
55
- charset='utf8',
56
-
57
- cursorclass=pymysql.cursors.DictCursor)
58
-
59
-
60
-
61
- with connection.cursor() as cursor:
62
-
63
- FROM = yyyy+"-"+MM+"-"+dd+" 00:00:00"
64
-
65
- TO = yyyy+"-"+MM+"-"+dd+" 23:00:00"
66
-
67
- sql = "select * from thData where lid="+str(lid)
68
-
69
- sql += " and date>='"+FROM+"' and date<='"+TO+"'"
70
-
71
- print("sql=",sql)
72
-
73
-
74
-
75
- cursor.execute(sql)
76
-
77
- results = cursor.fetchall()
78
-
79
- #for r in results: print(r)
80
-
81
- return results
82
-
83
- #--------------------------------------------------------------------
84
-
85
- def make_graph(): # make_graph関数
86
-
87
- #print("<< make_graph start >>")
88
-
89
- x = [0]*24
90
-
91
- y1 = [None]*24
92
-
93
- y2 = [0]*24
94
-
95
- yyyy = cb1.get()
96
-
97
- m = int(cb2.get())
98
-
99
- d = int(cb3.get())
100
-
101
- MM = "{:02d}".format(m)
102
-
103
- dd = "{:02d}".format(d)
104
-
105
- lid = 1
106
-
107
-
108
-
109
- #print("yyyy=",yyyy," ",type(yyyy))
110
-
111
- #print("MM=",MM," ",type(MM))
112
-
113
- #print("dd=",dd," ",type(dd))
114
-
115
-
116
-
117
- #--------------------------------
118
-
119
- results = select(yyyy,MM,dd,lid)
120
-
121
- #--------------------------------
122
-
123
- for i in range(0,24): x[i]=i
124
-
125
- num = len(results)
126
-
127
- print("num=",num)
128
-
129
- if num == 0: y1 = [None]*24
130
-
131
-
132
-
133
- for i in range(num):
134
-
135
- if i > 23: break
136
-
137
-
138
-
139
- temp = results[i].get('temp')
140
-
141
- hcount = results[i].get('hcount')
142
-
143
- print(i," ",temp," ",hcount)
144
-
145
- y1[i] = temp
146
-
147
- y2[i] = hcount
148
-
149
-
150
-
151
- ax1.cla()
152
-
153
- ax1.set_xlabel('Hour (H)')
154
-
155
- ax1.set_ylabel('Temperature (deg)')
156
-
157
- ax1.set_ylim(-5,25)
158
-
159
- ax1.plot(x,y1,color="blue",marker="o")
160
-
161
-
162
-
163
- ax2.cla()
164
-
165
- ax2.set_ylabel('Human detected (number)',color="red")
166
-
167
- ax2.set_ylim(0,65)
168
-
169
- ax2.bar(x,y2,color="red")
170
-
171
-
172
-
173
- canvas.draw()
174
-
175
- #print("<< make_graph end >>")
176
-
177
-
178
-
179
- #-----------------------------------------------------------------
180
-
181
- x = [0]*24
182
-
183
- y1 = [None]*24
184
-
185
- y2 = [0]*24
186
-
187
-
188
-
189
- root = tk.Tk()
190
-
191
- root.title("TEST")
192
-
193
- root.geometry("650x580")
194
-
195
-
196
-
197
- frame1 = tk.LabelFrame(
198
-
199
- root,labelanchor="nw",
200
-
201
- text="Temperture & Human Sensor",foreground="green")
202
-
203
- # フレーム1
204
-
205
- frame1.grid(rowspan=2,column=0)
206
-
207
-
208
-
209
- frame2=tk.LabelFrame(root,text="Control", foreground="red")
210
-
211
-
212
-
213
- frame2.grid(row=2,column=0,sticky="nwse")
214
-
215
-
216
-
217
- fig = plt.Figure()
218
-
219
- ax1 = fig.add_subplot(111)
220
-
221
-
222
-
223
- ax1.plot(x,y1)
224
-
225
- ax1.set_xlabel('Hour (H)')
226
-
227
- ax1.set_ylabel('Temperature (deg)')
228
-
229
- ax1.set_ylim(-5,25)
230
-
231
-
232
-
233
- ax2 = ax1.twinx()
234
-
235
- ax2.bar(x,y2,color="red")
236
-
237
- ax2.set_ylabel('Human detected (number)',color="red")
238
-
239
- ax2.set_ylim(0,65)
240
-
241
-
242
-
243
- canvas = FigureCanvasTkAgg(fig, master=frame1)
244
-
245
- # キャンバスのインスタンス化
246
-
247
- canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
248
-
249
-
250
-
251
- now = datetime.now()
252
-
253
- date = "{:%Y-%m-%d}".format(now)
254
-
255
- month = now.month
256
-
257
- day = now.day
258
-
259
-
260
-
261
- years = ["2020","2021","2022","2023","2024","2025"]
262
-
263
- # 年(コンボボックス)
264
-
265
- months = [1]*12
266
-
267
- for i in range(12) : months[i]=i+1
268
-
269
- days = [1]*31
270
-
271
- for i in range(31) : days[i]=i+1
272
-
273
- devices = ["810C3B31","790A2D12"]
274
-
275
-
276
-
277
- lb1 = tk.Label(frame2, text="year:",bg="cyan")
278
-
279
- cb1 = ttk.Combobox(frame2, values=years,width=6,justify=tk.CENTER)
280
-
281
- cb1.current(0)
282
-
283
-
284
-
285
- lb2 = tk.Label(frame2, text="month:",bg="cyan")
286
-
287
- cb2 = ttk.Combobox(frame2, values = months,width=6,justify=tk.CENTER)
288
-
289
- cb2.current(month-1)
290
-
291
-
292
-
293
- lb3 = tk.Label(frame2, text="day:",bg="cyan") # ラベル3(lb3)
294
-
295
- cb3 = ttk.Combobox(frame2, values = days,width=6,justify=tk.CENTER)
296
-
297
- cb3.current(day-1)
298
-
299
- lb4 = tk.Label(frame2, text="Device ID :",bg="cyan")
300
-
301
- cb4 = ttk.Combobox(frame2, values= devices,width=6,justify=tk.CENTER)
302
-
303
- cb4.current(0)
304
-
305
-
306
-
307
- y = cb1.current()
308
-
309
- m = cb2.current()
310
-
311
- d = cb3.current()
312
-
313
- selected_date = str(years[y])+"-"+str(months[m])+"-"+str(days[d])
314
-
315
- print("selected_date=",selected_date)
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
- print("cb1=",cb1.get()," ",end="")
324
-
325
- print("cb2=",cb2.get()," ",end="")
326
-
327
- print("cb3=",cb3.get())
328
-
329
-
330
-
331
- bt1 = tk.Button(frame2, text="Display", bg="yellow",command=make_graph)
332
-
333
-
334
-
335
- lb1.grid(row=1, column=0 ,padx=5, pady=5)
336
-
337
- cb1.grid(row=1, column=1 ,padx=5, pady=5)
338
-
339
- lb2.grid(row=1, column=2 ,padx=5, pady=5)
340
-
341
- cb2.grid(row=1, column=3 ,padx=5, pady=5)
342
-
343
- lb3.grid(row=1, column=4 ,padx=5, pady=5)
344
-
345
- cb3.grid(row=1, column=5 ,padx=5, pady=5)
346
-
347
- lb4.grid(row=1, column=6 ,padx=5, pady=5)
348
-
349
- cb4.grid(row=1, column=7 ,padx=5, pady=5)
350
-
351
- bt1.grid(row=1, column=8 ,padx=5, pady=5)
352
-
353
-
354
-
355
- make_graph()
356
-
357
-
358
-
359
- root.mainloop()
360
364
 
361
365
 
362
366