qun542110741

Python学习,给自己的代码做个合集,定制自己的桌面软件!

Python学习,给自己的代码做个合集,定制自己的桌面软件!

 

在学习Python的过程中,经常会写很多的练手的脚本,那么有没有想过,写到一起呢?当然了,方法有很多,比如写到web网页中,做各种跳转、写到微信中,各种回复关键字调用,还有今天和大家分享的GUI图形用户界面!

构建基本框架

Python中有标准库tkinter,不需要安装即可使用!可以用来写简单的GUI程序,只需要短短几行代码就可以了,比如下面这个:

Python学习,给自己的代码做个合集,定制自己的桌面软件!

 

具体教程大家可以去自行搜索,这里就不一一细说了,注释也写的很清楚!

将自己的其他脚本都写到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

 

最后是空气指数查询

 1 def quality():
 2     url = \'http://www.tianqihoubao.com/aqi/\'
 3     html = requests.get(url)
 4     html.encoding = \'gbk\'
 5     citys = etree.HTML(html.text).xpath(\'//div[@class="citychk"]/dl/dd/a/text()\')
 6     city_urls = etree.HTML(html.text).xpath(\'//div[@class="citychk"]/dl/dd/a/@href\')
 7     dic = {}#构架城市列表
 8     for city, city_url in zip(citys, city_urls):
 9         city = city.replace(" ", "")
10         city_url = \'http://www.tianqihoubao.com/\' + city_url
11         dic[city] = city_url
12     city_n = entry.get()
13     if city_n in dic.keys():
14         html_n = requests.get(dic[city_n])
15         html_n.encoding = \'gbk\'
16         data = etree.HTML(html_n.text)
17         num = data.xpath(\'//div[@class="num"]/text()\')[0].strip()
18         status = data.xpath(\'//div[@class="status"]/text()\')[0].strip()
19         explain = data.xpath(\'//div[@class="hd"]/div[@id="content"]/div[@class="txt01"]/h4\')[0].xpath(\'string(.)\')
20         surveys = re.findall(r\'<td.*?>(.*?)</td>\', html_n.text, re.S)
21         sur = re.sub("</b>", "|", \'\'.join([x.strip().replace("<b>", \' \') for x in surveys[0:9]]))[:-1]
22         sur2 = [x.strip().replace(" ", \'\') + \'|\' for x in surveys[9:]]
23         surv = [sur2[i:i + 9] for i in range(0, len(sur2), 9)]
24         text.insert(END,\'%s空气质量查询\' % city_n)  # 添加数据
25         text.insert(END,"%s空气质量指数: %s" % (city_n, num))  # 添加数据
26         text.insert(END,"%s空气质量:    %s" %(city_n, status))  # 添加数据
27         text.insert(END,explain)  # 添加数据
28         text.insert(END,sur)  # 添加数据
29         for su in surv:
30             text.insert(END," ".join(su)[:-1])  # 添加数据
31         text.see(END)  # 文本框向下滚动
32         text.update()  # 更新
33     else:
34         text.insert(END, \'输错了吧?\')  # 添加数据
35         text.see(END)  # 文本框向下滚动
36         text.update()  # 更新

 

好了,现在是主函数

 1 if __name__ == \'__main__\':
 2     root = Tk()
 3     root.title("我的应用汇总")#窗口标题
 4     root.geometry(\'660x600+600+50\')#窗口大小位置 用x链接 +后面是位置
 5     label = Label(root,text="<<-----云飞学编程----Q群542110741----->>",font=(\'微软雅黑\'))#创建标签控件
 6     label.grid(row=0,columnspan=3)#网格式布局
 7     text = Listbox(root,font=(\'微软雅黑\',15),width=55,height=18)#列表框控件,设置组件默认宽高
 8     text.grid(row=2,columnspan=4)#columnspan为组件所跨越的列数
 9     button_tq = Button(root,text="天气预报查询",font=(\'微软雅黑\',8),command=weather)#点击按钮
10     button_tq.grid(row=1,column=1)
11     button_kq = Button(root,text="空气质量查询",font=(\'微软雅黑\',8),command=quality)#点击按钮
12     button_kq.grid(row=1,column=3)
13     button_pm = Button(root,text="空气质量排名",font=(\'微软雅黑\',8),command=ranking)#点击按钮
14     button_pm.grid(row=1,column=2)
15     entry = Entry(root,font=(\'微软雅黑\'))#创建输入框
16     entry.grid(row=1,column=0)#定位第1行3列
17     root.mainloop()

 

运行效果如下:

Python学习,给自己的代码做个合集,定制自己的桌面软件!

上面3个爬虫,都在以往的文章中发过,大家如果感兴趣可以去看看!这里就不详细注释了,只是复制过来稍微修改下就用了!

Python学习,给自己的代码做个合集,定制自己的桌面软件!

待改进:

1、内容添加,目前就3个爬虫的内容,慢慢添加更多的感兴趣的内容进去,最终形成自己的定制软件

2、界面的优化,比如滑动条、字体大小、按钮大小位置等等

3、连接数据库,目前的内容都是实时抓取网页内容,连接数据库会更加的快捷

目前就这么多,想到在继续吧!刚开始学习GUI,总有不足之处,如果有更好的建议大家可以评论区讨论哦!需要源码的话就私信我吧!

Python学习,给自己的代码做个合集,定制自己的桌面软件!

分类:

技术点:

相关文章:

  • 2021-08-08
  • 2022-12-23
  • 2021-06-04
  • 2021-06-13
  • 2021-12-04
  • 2022-12-23
  • 2022-01-07
  • 2021-08-09
猜你喜欢
  • 2021-06-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
  • 2021-04-01
相关资源
相似解决方案