Python學習,給本身的代碼作個合集,定製本身的桌面軟件!

Python學習,給本身的代碼作個合集,定製本身的桌面軟件!

 

在學習Python的過程當中,常常會寫不少的練手的腳本,那麼有沒有想過,寫到一塊兒呢?固然了,方法有不少,好比寫到web網頁中,作各類跳轉、寫到微信中,各類回覆關鍵字調用,還有今天和你們分享的GUI圖形用戶界面!html

構建基本框架

Python中有標準庫tkinter,不須要安裝便可使用!能夠用來寫簡單的GUI程序,只須要短短几行代碼就能夠了,好比下面這個:python

Python學習,給本身的代碼作個合集,定製本身的桌面軟件!

 

具體教程你們能夠去自行搜索,這裏就不一一細說了,註釋也寫的很清楚!web

將本身的其餘腳本都寫到GUI程序中

其實能夠導入其餘腳本中的函數,來達到多個腳本整合的效果,可是那樣又不是很方便,就先放到一塊兒了,慢慢在完善!數據庫

首先是將以前的天氣預報寫入(這裏有個城市代碼的字典省略了,很長,你們能夠去我相關的文章中查找)編程

def weather():
	'''	天氣預報查詢	'''
	global city_code_list#城市列表
	city = entry.get()#獲取輸入的城市名
	if city in city_code_list:
		city_code = city_code_list[city]
		home_page = 'http://www.weather.com.cn'
		url = home_page + '/weather/' + city_code + '.shtml'
		header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0'}
		res = requests.get(url, headers=header)
		res.encoding = 'utf-8'
		html = etree.HTML(res.text)
		for i in range(1, 8):
			date = html.xpath('//ul[@class="t clearfix"]/li[{}]/h1/text()'.format(i))[0]
			weather = html.xpath('//ul[@class="t clearfix"]/li[{}]/p[1]/text()'.format(i))[0]
			tem1 = html.xpath('//ul[@class="t clearfix"]/li[{}]/p[@class="tem"]/span/text()'.format(i))
			tem2 = html.xpath('//ul[@class="t clearfix"]/li[{}]/p[@class="tem"]/i/text()'.format(i))
			tem = "".join(tem1) + '/' + "".join(tem2)
			win1 = html.xpath('//ul[@class="t clearfix"]/li[{}]/p[@class="win"]/i/text()'.format(i))
			win2 = html.xpath('//ul[@class="t clearfix"]/li[{}]/p[@class="win"]/em/span[1]/@title'.format(i))
			win = "".join(win1) + "".join(win2)
			text.insert(END,'%s天氣預報查詢'%city)#添加數據
			text.insert(END,'%s %s %s %s'%(date, weather, tem, win))#添加數據
			text.see(END)#文本框向下滾動
			text.update()#更新
	else:
		text.insert(END, '輸錯了吧?')  # 添加數據
		text.see(END)  # 文本框向下滾動
		text.update()  # 更新

而後是空氣質量排名微信

def ranking():
	city = entry.get()
	url = 'http://www.tianqihoubao.com/aqi/aqi_rank.html'
	html = requests.get(url)
	datas = etree.HTML(html.text).xpath('//table[@class="b"]')[0]
	i = 1
	for data in datas:
		trs = data.xpath('./td')
		info = []
		for tr in trs:
			a = tr.xpath('string(.)').split()[0]
			if i % 6 != 0:
				info.append(a)
			elif i == 6:
				text.insert(END, '%s' % (" | ".join(info))) # 第一行
			elif city in info:#判斷須要數據所在行
				text.insert(END, '%s' % ("   |   ".join(info)))  # 添加須要的數據
				text.see(END)  # 文本框向下滾動
				text.update()  # 更新
				info = []
			i += 1

最後是空氣指數查詢app

def quality():
	url = 'http://www.tianqihoubao.com/aqi/'
	html = requests.get(url)
	html.encoding = 'gbk'
	citys = etree.HTML(html.text).xpath('//div[@class="citychk"]/dl/dd/a/text()')
	city_urls = etree.HTML(html.text).xpath('//div[@class="citychk"]/dl/dd/a/@href')
	dic = {}#構架城市列表
	for city, city_url in zip(citys, city_urls):
		city = city.replace(" ", "")
		city_url = 'http://www.tianqihoubao.com/' + city_url
		dic[city] = city_url
	city_n = entry.get()
	if city_n in dic.keys():
		html_n = requests.get(dic[city_n])
		html_n.encoding = 'gbk'
		data = etree.HTML(html_n.text)
		num = data.xpath('//div[@class="num"]/text()')[0].strip()
		status = data.xpath('//div[@class="status"]/text()')[0].strip()
		explain = data.xpath('//div[@class="hd"]/div[@id="content"]/div[@class="txt01"]/h4')[0].xpath('string(.)')
		surveys = re.findall(r'<td.*?>(.*?)</td>', html_n.text, re.S)
		sur = re.sub("</b>", "|", ''.join([x.strip().replace("<b>", ' ') for x in surveys[0:9]]))[:-1]
		sur2 = [x.strip().replace(" ", '') + '|' for x in surveys[9:]]
		surv = [sur2[i:i + 9] for i in range(0, len(sur2), 9)]
		text.insert(END,'%s空氣質量查詢' % city_n)  # 添加數據
		text.insert(END,"%s空氣質量指數: %s" % (city_n, num))  # 添加數據
		text.insert(END,"%s空氣質量:    %s" %(city_n, status))  # 添加數據
		text.insert(END,explain)  # 添加數據
		text.insert(END,sur)  # 添加數據
		for su in surv:
			text.insert(END," ".join(su)[:-1])  # 添加數據
		text.see(END)  # 文本框向下滾動
		text.update()  # 更新
	else:
		text.insert(END, '輸錯了吧?')  # 添加數據
		text.see(END)  # 文本框向下滾動
		text.update()  # 更新

好了,如今是主函數框架

if __name__ == '__main__':
	root = Tk()
	root.title("個人應用匯總")#窗口標題
	root.geometry('660x600+600+50')#窗口大小位置 用x連接 +後面是位置
	label = Label(root,text="<<-----雲飛學編程----Q羣542110741----->>",font=('微軟雅黑'))#建立標籤控件
	label.grid(row=0,columnspan=3)#網格式佈局
	text = Listbox(root,font=('微軟雅黑',15),width=55,height=18)#列表框控件,設置組件默認寬高
	text.grid(row=2,columnspan=4)#columnspan爲組件所跨越的列數
	button_tq = Button(root,text="天氣預報查詢",font=('微軟雅黑',8),command=weather)#點擊按鈕
	button_tq.grid(row=1,column=1)
	button_kq = Button(root,text="空氣質量查詢",font=('微軟雅黑',8),command=quality)#點擊按鈕
	button_kq.grid(row=1,column=3)
	button_pm = Button(root,text="空氣質量排名",font=('微軟雅黑',8),command=ranking)#點擊按鈕
	button_pm.grid(row=1,column=2)
	entry = Entry(root,font=('微軟雅黑'))#建立輸入框
	entry.grid(row=1,column=0)#定位第1行3列
	root.mainloop()

運行效果以下:函數

Python學習,給本身的代碼作個合集,定製本身的桌面軟件!

上面3個爬蟲,都在以往的文章中發過,你們若是感興趣能夠去看看!這裏就不詳細註釋了,只是複製過來稍微修改下就用了!oop

Python學習,給本身的代碼作個合集,定製本身的桌面軟件!

待改進:

一、內容添加,目前就3個爬蟲的內容,慢慢添加更多的感興趣的內容進去,最終造成本身的定製軟件

二、界面的優化,好比滑動條、字體大小、按鈕大小位置等等

三、鏈接數據庫,目前的內容都是實時抓取網頁內容,鏈接數據庫會更加的快捷

目前就這麼多,想到在繼續吧!剛開始學習GUI,總有不足之處,若是有更好的建議你們能夠評論區討論哦!須要源碼的話就私信我吧!

Python學習,給本身的代碼作個合集,定製本身的桌面軟件!

相關文章
相關標籤/搜索