-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.py
153 lines (121 loc) · 7.14 KB
/
main.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
import tkinter as tk
import dataSource
from tkinter import messagebox
import tkinter.ttk as ttk
from datetime import datetime,timedelta
class Window(tk.Tk):
def getInternetData(self):
try:
self.downloadTime = datetime.now()
citylist = dataSource.getAirData()
self.cities = {cityObject.county:cityObject for cityObject in citylist} #包存現在要顯示的資料,轉成dict,key=城市名,value=CityWeather的實體
except ValueError as e:
messagebox.showwarning("連線錯誤",e)
self.destroy()
currentTimeString = citylist[0].time # 取得json內顯示的時間字串
self.currentDateTime = datetime.strptime(currentTimeString,
"%Y-%m-%d %H:%M:%S.%f") # 將字串轉為datetime物件,保存目前顯示資料的datetime物件
def __init__(self):
super().__init__()
self.title("全省空氣品質指標")
#--------------取得資料start-------------------#
self.getInternetData()
# --------------取得資料end-------------------#
# --------------建立視窗start-------------------#
mainFrame = tk.Frame(self,width=500,height=600,borderwidth=1,relief=tk.GROOVE,padx=20,pady=20)
#建立上方的topFrame
topFrame = tk.Frame(mainFrame)
tk.Label(topFrame,text="台灣各地空氣品質指標",font=("arial",22,"bold"),fg="#555555").pack()
self.currentTimeLabel = tk.Label(topFrame,text="觀測時間:xxxxxxxxx",font=("arial",16),fg="#555555")
self.currentTimeLabel.pack(pady=(30,10))
self.nextTimeLabel = tk.Label(topFrame,text="下次更新時間:xxxxxxxxx",font=("arial",16),fg="#555555")
self.nextTimeLabel.pack()
self.leftTimeLabel = tk.Label(topFrame, text="20:15", font=("arial", 16), fg="#555555")
self.leftTimeLabel.pack()
topFrame.pack()
#建立中間的middleFrame
middleFrame = tk.Frame(mainFrame)
tk.Label(middleFrame,text="請選擇監測站:",font=("arial",20),fg="#555555").pack(side=tk.LEFT)
self.comboboxText = tk.StringVar()
self.comboBox = ttk.Combobox(middleFrame,textvariable=self.comboboxText,font=("arial",20))
self.comboBox.pack(side=tk.LEFT)
self.comboBox.bind('<<ComboboxSelected>>', self.combobox_selected)
middleFrame.pack(pady=20)
#建立下方的bottomFrame
bottomFrame = tk.Frame(mainFrame,bg='#666666')
bottomFrame.columnconfigure(0,weight=1)
bottomFrame.columnconfigure(1, weight=2)
tk.Label(bottomFrame,text="監測點:",font=("Arial",16),bg='#666666',fg='#ffffff').grid(row=0,column=0,sticky=tk.E,padx=10,pady=10)
self.siteNameLabel = tk.Label(bottomFrame,text="監測點",font=("Arial",16),bg='#666666',fg='#ffffff')
self.siteNameLabel.grid(row=0,column=1,sticky=tk.W,padx=10,pady=10)
tk.Label(bottomFrame, text="城市:", font=("Arial", 16),bg='#666666',fg='#ffffff').grid(row=1, column=0, sticky=tk.E, padx=10, pady=10)
self.cityNameLabel = tk.Label(bottomFrame, text="城市", font=("Arial", 16),bg='#666666',fg='#ffffff')
self.cityNameLabel.grid(row=1, column=1, sticky=tk.W,padx=10, pady=10)
tk.Label(bottomFrame, text="AQI:", font=("Arial", 16),bg='#666666',fg='#ffffff').grid(row=2, column=0, sticky=tk.E, padx=10, pady=10)
self.aqiLabel = tk.Label(bottomFrame, text="AQI", font=("Arial", 16),bg='#666666',fg='#ffffff')
self.aqiLabel.grid(row=2, column=1, sticky=tk.W,padx=10, pady=10)
tk.Label(bottomFrame, text="PM2.5:", font=("Arial", 16),bg='#666666',fg='#ffffff').grid(row=3, column=0, sticky=tk.E, padx=10, pady=10)
self.pm25Label = tk.Label(bottomFrame, text="PM2.5", font=("Arial", 16),bg='#666666',fg='#ffffff')
self.pm25Label.grid(row=3, column=1, sticky=tk.W,padx=10, pady=10)
tk.Label(bottomFrame, text="狀態:", font=("Arial", 16),bg='#666666',fg='#ffffff').grid(row=4, column=0, sticky=tk.E, padx=10, pady=10)
self.stateLabel = tk.Label(bottomFrame, text="狀態", font=("Arial", 16),bg='#666666',fg='#ffffff')
self.stateLabel.grid(row=4, column=1, sticky=tk.W,padx=10, pady=10)
bottomFrame.pack(pady=20,padx=70,fill=tk.X,expand=tk.YES,anchor=tk.CENTER)
mainFrame.pack_propagate(0)
mainFrame.pack(padx=50,pady=50)
# --------------建立視窗end-------------------#
#-------------更新標題start---------#
self.updateTopWindowContent(self.currentDateTime,self.downloadTime,list(self.cities.keys()))
# -------------更新標題end---------#
def updateTopWindowContent(self,displayCurrent,downloadTime,cityNames):
'''
更新畫面上方的的內容
:param displayCurrent: 觀測的時間(datetime物件)
:param downloadTime: 現在下載的時間(datetime物件)
:param cityNames:目前所有城市名程(list,包含城市名稱字串)
:return:
'''
self.currentTimeLabel.config(text=displayCurrent.strftime("觀測時間:%Y年%m月%d日--%H時%M分%S秒"))
self.updateTime = downloadTime + timedelta(minutes=10);
self.updatePersecond()
self.nextTimeLabel.config(text=self.updateTime.strftime("下次更新時間:%Y年%m月%d日--%H時%M分%S秒"))
self.comboBox.config(values=cityNames)
self.comboBox.current(0) #預設選擇第一位
self.updateBottomWindowContent(cityNames[0])#預設選擇第一個城市
def updatePersecond(self):
'''
每隔1秒執行1次,更新self.leftTimeLabel的text
時間到時,重新下載,更新畫面,取消self.updateTime
:return:
'''
remainTime = self.updateTime - datetime.now() # 下載時間 - 現在既時時間 #timeDelta實體
if remainTime.seconds <= 0:
self.getInternetData() #重新下載資料
self.updateTopWindowContent(self.currentDateTime,self.downloadTime,list(self.cities.keys())) #更新上方畫面
print('時間到了')
return #取消self.updateTime
remainSeconds = remainTime.seconds #取出秒數
remainMinutes = remainSeconds // 60 #轉換為分鐘
remainSeconds1 = remainSeconds % 60 #轉換為剩餘秒數
self.leftTimeLabel.config(text=f"剩餘:{remainMinutes}分,{remainSeconds1}秒")
self.after(1000,self.updatePersecond)
def updateBottomWindowContent(self,selectedCounty):
'''
更新視窗下半部內容
:param selectedCounty: 使用者選擇的城市string
:return:
'''
cityWeather = self.cities[selectedCounty]
self.siteNameLabel.config(text=cityWeather.siteName)
self.cityNameLabel.config(text=cityWeather.county)
self.aqiLabel.config(text=cityWeather.aqi)
self.pm25Label.config(text=cityWeather.pm25)
self.stateLabel.config(text=cityWeather.status)
def combobox_selected(self,event):
widget = event.widget
comboBoxIndex = widget.current()
selectedCounty = self.comboboxText.get()
self.updateBottomWindowContent(selectedCounty)
if __name__ == "__main__":
window = Window()
window.mainloop()