python
1コード 2 3import tkinter as tk 4# tkinterモジュールをtkとしてインポート 5import tkinter.ttk as ttk 6# tkinterのttkをttkとしてインポート 7import matplotlib.pyplot as plt 8# matplotlibモジュールのpyplotをpltとしてインポート 9from matplotlib.backends.backend_tkagg import ( 10 FigureCanvasTkAgg, NavigationToolbar2Tk) 11# FigureCanvasTkAggとNavigationToolbar2Tkのインポート 12from random import random 13# randomモジュールからrandom関数をインポート 14from datetime import datetime 15 # datetimeモジュールからdatetime関数のインポート 16from functools import partial 17# functoolsモジュールからpartialのインポート 18import pymysql.cursors 19# pymysqlモジュールのcursorsをインポート 20#-------------------------------------------------------------------- 21def select(yyyy,MM,dd,lid): # select関数 22 connection = pymysql.connect( 23 host='localhost', 24 user='hit', 25 password='hit', 26 db='THT', 27 charset='utf8', 28 cursorclass=pymysql.cursors.DictCursor) 29 30 with connection.cursor() as cursor: 31 FROM = yyyy+"-"+MM+"-"+dd+" 00:00:00" 32 TO = yyyy+"-"+MM+"-"+dd+" 23:00:00" 33 sql = "select * from thData where lid="+str(lid) 34 sql += " and date>='"+FROM+"' and date<='"+TO+"'" 35 print("sql=",sql) 36 37 cursor.execute(sql) 38 results = cursor.fetchall() 39 #for r in results: print(r) 40 return results 41#-------------------------------------------------------------------- 42def make_graph(): # make_graph関数 43 #print("<< make_graph start >>") 44 x = [0]*24 45 y1 = [None]*24 46 y2 = [0]*24 47 yyyy = cb1.get() 48 m = int(cb2.get()) 49 d = int(cb3.get()) 50 MM = "{:02d}".format(m) 51 dd = "{:02d}".format(d) 52 lid = 1 53 54 #print("yyyy=",yyyy," ",type(yyyy)) 55 #print("MM=",MM," ",type(MM)) 56 #print("dd=",dd," ",type(dd)) 57 58 #-------------------------------- 59 results = select(yyyy,MM,dd,lid) 60 #-------------------------------- 61 for i in range(0,24): x[i]=i 62 num = len(results) 63 print("num=",num) 64 if num == 0: y1 = [None]*24 65 66 for i in range(num): 67 if i > 23: break 68 69 temp = results[i].get('temp') 70 hcount = results[i].get('hcount') 71 print(i," ",temp," ",hcount) 72 y1[i] = temp 73 y2[i] = hcount 74 75 ax1.cla() 76 ax1.set_xlabel('Hour (H)') 77 ax1.set_ylabel('Temperature (deg)') 78 ax1.set_ylim(-5,25) 79 ax1.plot(x,y1,color="blue",marker="o") 80 81 ax2.cla() 82 ax2.set_ylabel('Human detected (number)',color="red") 83 ax2.set_ylim(0,65) 84 ax2.bar(x,y2,color="red") 85 86 canvas.draw() 87 #print("<< make_graph end >>") 88 89#----------------------------------------------------------------- 90x = [0]*24 91y1 = [None]*24 92y2 = [0]*24 93 94root = tk.Tk() 95root.title("TEST") 96root.geometry("650x580") 97 98frame1 = tk.LabelFrame( 99root,labelanchor="nw", 100text="Temperture & Human Sensor",foreground="green") 101# フレーム1 102frame1.grid(rowspan=2,column=0) 103 104frame2=tk.LabelFrame(root,text="Control", foreground="red") 105 106frame2.grid(row=2,column=0,sticky="nwse") 107 108fig = plt.Figure() 109ax1 = fig.add_subplot(111) 110 111ax1.plot(x,y1) 112ax1.set_xlabel('Hour (H)') 113ax1.set_ylabel('Temperature (deg)') 114ax1.set_ylim(-5,25) 115 116ax2 = ax1.twinx() 117ax2.bar(x,y2,color="red") 118ax2.set_ylabel('Human detected (number)',color="red") 119ax2.set_ylim(0,65) 120 121canvas = FigureCanvasTkAgg(fig, master=frame1) 122# キャンバスのインスタンス化 123canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) 124 125now = datetime.now() 126date = "{:%Y-%m-%d}".format(now) 127month = now.month 128day = now.day 129 130years = ["2020","2021","2022","2023","2024","2025"] 131# 年(コンボボックス) 132months = [1]*12 133for i in range(12) : months[i]=i+1 134days = [1]*31 135for i in range(31) : days[i]=i+1 136devices = ["810C3B31","790A2D12"] 137 138lb1 = tk.Label(frame2, text="year:",bg="cyan") 139cb1 = ttk.Combobox(frame2, values=years,width=6,justify=tk.CENTER) 140cb1.current(0) 141 142lb2 = tk.Label(frame2, text="month:",bg="cyan") 143cb2 = ttk.Combobox(frame2, values = months,width=6,justify=tk.CENTER) 144cb2.current(month-1) 145 146lb3 = tk.Label(frame2, text="day:",bg="cyan") # ラベル3(lb3) 147cb3 = ttk.Combobox(frame2, values = days,width=6,justify=tk.CENTER) 148cb3.current(day-1) 149lb4 = tk.Label(frame2, text="Device ID :",bg="cyan") 150cb4 = ttk.Combobox(frame2, values= devices,width=6,justify=tk.CENTER) 151cb4.current(0) 152 153y = cb1.current() 154m = cb2.current() 155d = cb3.current() 156selected_date = str(years[y])+"-"+str(months[m])+"-"+str(days[d]) 157print("selected_date=",selected_date) 158 159 160 161print("cb1=",cb1.get()," ",end="") 162print("cb2=",cb2.get()," ",end="") 163print("cb3=",cb3.get()) 164 165bt1 = tk.Button(frame2, text="Display", bg="yellow",command=make_graph) 166 167lb1.grid(row=1, column=0 ,padx=5, pady=5) 168cb1.grid(row=1, column=1 ,padx=5, pady=5) 169lb2.grid(row=1, column=2 ,padx=5, pady=5) 170cb2.grid(row=1, column=3 ,padx=5, pady=5) 171lb3.grid(row=1, column=4 ,padx=5, pady=5) 172cb3.grid(row=1, column=5 ,padx=5, pady=5) 173lb4.grid(row=1, column=6 ,padx=5, pady=5) 174cb4.grid(row=1, column=7 ,padx=5, pady=5) 175bt1.grid(row=1, column=8 ,padx=5, pady=5) 176 177make_graph() 178 179root.mainloop()
試したこと
lid = cb4.get()とした。devices = ["1","2"]等の数字のみでは、この書き換えて出来たが
810C3B31、790A2D12の文字を含むとエラーが出る。