iT邦幫忙

0

Day19 參加職訓(機器學習與資料分析工程師培訓班),Python程式設計

上午:Python程式設計

延續上次的tkinter,製作Menu有下拉式選單的功能

import tkinter as tk

def Hello():
    print('hello')

base = tk.Tk()
Menu01 = tk.Menu(base)

Submenu01 = tk.Menu(Menu01, tearoff = 0)
Submenu01.add_command(label = 'New File', command = Hello)
Submenu01.add_command(label = 'Open', command = Hello)
Submenu01.add_command(label = 'Save', command = Hello)
Submenu01.add_separator()
Submenu01.add_command(label = 'Exit', command = base.quit)
Menu01.add_cascade(label = 'File',menu = Submenu01)

Submenu02 = tk.Menu(Submenu01, tearoff = 0)
Submenu02.add_command(label = 'Cut', command = Hello)
Submenu02.add_command(label = 'Copy', command = Hello)
Submenu02.add_command(label = 'Paste', command = Hello)
Submenu02.add_command(label = 'Select All', command = Hello)
Submenu02.add_separator()
Submenu02.add_command(label = 'Find...', command = Hello)
Submenu02.add_command(label = 'Find again', command = Hello)
Menu01.add_cascade(label = 'Edit',menu = Submenu02)

Submenu03 = tk.Menu(Menu01, tearoff = 0)
Submenu03.add_command(label = 'TK Help', command = Hello)
Submenu03.add_command(label = 'Demo', command = Hello)
Submenu03.add_separator()
Submenu03.add_command(label = 'About TK', command = Hello)
Menu01.add_cascade(label = 'Help',menu = Submenu03)

base.config(menu = Menu01)
base.mainloop()

呈現的結果
https://ithelp.ithome.com.tw/upload/images/20210726/20139039svGWjc77Rk.png

  • 查詢即時PM 2.5
import tkinter as tk
import ssl
import pandas as pd
ssl._create_default_https_context = ssl._create_unverified_context

def checkPM():
    pm25 = pd.read_csv('https://data.epa.gov.tw/api/v1/aqx_p_432?format=csv&limit=100&api_key=9be7b239-557b-4c10-9775-78cadfc555e9')
    chosen_site = pm25[pm25['SiteName']==str(MyString01.get())]

    for index, row in chosen_site.iterrows():
        String01 = row['PublishTime']
        String02 = row['County']
        String03 = row['PM2.5_AVG']
        String04 = row['SiteName']
        ans.set(f'{String01}在{String02}的{String04}觀測站觀測到PM2.5的濃度為{String03}')
    
base = tk.Tk()
base.title('查詢PM 2.5')
base.geometry('550x200')
MyString01 = tk.StringVar() 
ans = tk.StringVar()
ans.set('等待結果')


Frame01 = tk.Frame(base).pack()
Label01 = tk.Label(base, text = '請輸入觀測站名稱').pack()
Entry01 = tk.Entry(base, bd=5, textvariable = MyString01).pack()


Label02 = tk.Label(Frame01, textvariable=ans).pack()

Button01 = tk.Button(base, text = '搜尋', command = checkPM).pack()

tk.mainloop()

呈現的結果
https://ithelp.ithome.com.tw/upload/images/20210726/20139039qLrnO7njOc.png

下午: 實作Django


尚未有邦友留言

立即登入留言