1.目标:

初步认识tkinter or requests

2.准备:(https://pasteme.cn/28686)

2.1参考资料《用的时候再看也可以》

视频资料(全英、无字幕、慎选) 、略(英文版)、 tkinter详解中文版

廖雪峰requests详解

(再几个与爬虫原理相关的视频,毕竟每个人实操不一样,需要自己调整)

2.2需求:  注册Gmail 或者Msn.....

一些坑:

②注册gmail:通过qq邮箱那种方式(百度经验有),前提是需要注册谷歌账号(巨坑,输入电话验证的时候,要把前面空格去掉,对,它系统自己给你带了个空格)

3.详细过程:

3.1 根据视频前半截,练习 tkinter基础 与学会用  tkinter库资料

  • Tk:pass
  • mainloop:pass
  • Entry:It is used to input the single line text entry from the user.(用于输入单行文本) For multi-line text input, Text widget is used.(多行文本用Text)
  • Label: It refers to the display box where you can put any text or image which can be updated any time as per the code.(在显示框中放置文本或图像)标签控件;可以显示文本和位图
  • Button:To add a button in your application, this widget(小部件) is used.
  • canvas:画布控件;显示图形元素如线条或文本
  • bd: to set the border width(边宽) in pixels(像素).
  • bg: to set the normal background color.
  • cursor: to set the cursor(光标) used in the canvas.
  • highlightcolor: to set the color shown in the focus highlight.
  • width: to set the width of the widget.
  • height: to set the height of the widget.
  • command: to call a function.
  • font: to set the font on the button label.(控制字体)
  • image: to set the image on the button.
  • pack() :It organizes the widgets in blocks before placing in the parent widget.(包装)
  • grid() :It organizes the widgets in grid (table-like structure) before placing in the parent widget.(网格)
  • place():It organizes the widgets by placing them on specific positions directed by the programmer.;(位置)
# 1.建立窗口
import tkinter
m = tkinter.Tk()
'''
widgets are added here
'''
m.mainloop() 
# 2.
import tkinter as tk
root = tk.Tk()
root.title('Counting Seconds')   #窗口的名称
button = tk.Button(root, text='Stop', width=25, command=root.destroy)  #设置按钮
button.pack()
w =tk.Label(root, text='GeeksForGeeks.org!')
w.pack()
root.mainloop()
# 3.
from tkinter import *
master = Tk()
Label(master, text='First Name').grid(row=0)
Label(master, text='Last Name').grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
mainloop()

3.2 requests(这个方面不太清楚,能模糊理解几个关键词的意思,后面会慢慢补充,此篇博客只是简单运用而已)

requests:它是一个Python第三方库,处理URL资源特别方便

3.3跟着视频实操(一定要动手,适当暂停多次尝试)

3.3.1 引入模块(提前下载)

import tkinter as tk
import requests

3.3.2 建一个页面,并设置成自己喜欢的大小

import tkinter as tk
import requestsHEIGHT = 400
WIDTH = 500root = tk.Tk()canvas = tk.Canvas(root,height=HEIGHT,width=WIDTH)
canvas.pack()
root .title ('my first Python program')root.mainloop()

3.3.3设置按钮/标签/设置颜色等

import tkinter as tk
import requestsHEIGHT = 400
WIDTH = 500root = tk.Tk()canvas = tk.Canvas(root,height=HEIGHT,width=WIDTH)
canvas.pack()root .title ('my first Python program')frame = tk.Frame(root,bg='#80c1ff',bd=5)  #frame:框架,构架 bd:边宽
frame.place(relx=0.5,rely=0.1,relwidth=0.75,relheight=0.1,anchor='n')  #relx:指与左边距离,rely:与上边的距离button = tk.Button(frame,text="Get Weather",bg='gray',font=('courier',10),command='')
button.place(relx=0.7,relwidth=0.3,relheight=1) # font:字体及大小,cammand:为命令root.mainloop()

3.3.4 这里将frame与lower_frame对比,体会一下,frame的涵义

import tkinter as tk
import requestsHEIGHT = 400
WIDTH = 500root = tk.Tk()canvas = tk.Canvas(root,height=HEIGHT,width=WIDTH)
canvas.pack()root .title ('my first Python program')frame = tk.Frame(root,bg='#80c1ff',bd=5)  #frame:框架,构架 bd:边宽
frame.place(relx=0.5,rely=0.1,relwidth=0.75,relheight=0.1,anchor='n')  #relx:指与左边距离,rely:与上边的距离button = tk.Button(frame,text="Get Weather",bg='gray',font=('courier',10),command='')
button.place(relx=0.7,relwidth=0.3,relheight=1) # font:字体及大小,cammand:为命令lower_frame = tk.Frame(root,bg='#80c1ff',bd=10)
lower_frame.place(relx=0.5,rely=0.25,relheight=0.6,relwidth=0.75,anchor='n')lable = tk.Label(lower_frame,font=('courier',15))
lable.place(relwidth=1,relheight=1)root.mainloop()

3.3.5体会Entry

import tkinter as tk
import requestsHEIGHT = 400
WIDTH = 500root = tk.Tk()canvas = tk.Canvas(root,height=HEIGHT,width=WIDTH)
canvas.pack()root .title ('my first Python program')frame = tk.Frame(root,bg='#80c1ff',bd=5)  #frame:框架,构架 bd:边宽
frame.place(relx=0.5,rely=0.1,relwidth=0.75,relheight=0.1,anchor='n')  #relx:指与左边距离,rely:与上边的距离entry = tk.Entry(frame,font=40,fg='green')
entry.place(relwidth=0.65,relheight=1)button = tk.Button(frame,text="Get Weather",bg='gray',font=('courier',10),command='')
button.place(relx=0.7,relwidth=0.3,relheight=1) # font:字体及大小,cammand:为命令lower_frame = tk.Frame(root,bg='#80c1ff',bd=10)
lower_frame.place(relx=0.5,rely=0.25,relheight=0.6,relwidth=0.75,anchor='n')lable = tk.Label(lower_frame,font=('courier',15))
lable.place(relwidth=1,relheight=1)root.mainloop()

3.3.6注册openweathermap (前面已经将我遇到的坑交代了,剩下的靠自己了)

看视屏吧,我传不了截图。。。我太菜。不懂的留言就行

3.3.7

非常重要的事:从注册完之后,就不能抄视频里面的了,因为有变化,而且,要确保能懂点英文,懂点python基础,才能进行接下来的工作。

注意button 那一行的变化,command变化了

import tkinter as tk
import requestsHEIGHT = 400
WIDTH = 500'''
'''
def get_weather(city):weather_key = 'd918307fea274e3a6812dbe2296fb205'url = 'http://api.openweathermap.org/data/2.5/forecast'params = {'APPID':weather_key,'q':city}response = requests.get(url,params=params)print(response.json())# weather = (response.json())# lable['text'] = format_response(weather)root = tk.Tk()canvas = tk.Canvas(root,height=HEIGHT,width=WIDTH)
canvas.pack()root .title ('my first Python program')frame = tk.Frame(root,bg='#80c1ff',bd=5)  #frame:框架,构架 bd:边宽
frame.place(relx=0.5,rely=0.1,relwidth=0.75,relheight=0.1,anchor='n')  #relx:指与左边距离,rely:与上边的距离entry = tk.Entry(frame,font=40,fg='green')
entry.place(relwidth=0.65,relheight=1)button = tk.Button(frame,text="Get Weather",bg='gray',font=('courier',10),command=lambda: get_weather(entry.get()))
button.place(relx=0.7,relwidth=0.3,relheight=1) # font:字体及大小,cammand:为命令lower_frame = tk.Frame(root,bg='#80c1ff',bd=10)
lower_frame.place(relx=0.5,rely=0.25,relheight=0.6,relwidth=0.75,anchor='n')lable = tk.Label(lower_frame,font=('courier',15))
lable.place(relwidth=1,relheight=1)root.mainloop()

3.3.8 注意两个函数

import tkinter as tk
import requestsHEIGHT = 400
WIDTH = 500def format_response(weather):try:name = weather['city']['name']desc = weather['list'][0]['weather'][0]['description']temp = weather['list'][0]['main']['temp']return ('City:%s \n Conditions:%s \n Temperature(℉):%s \n' %(name,desc,temp))except:return 'There was a problem \n retrieving that information'def get_weather(city):weather_key = 'd918307fea274e3a6812dbe2296fb205'url = 'http://api.openweathermap.org/data/2.5/forecast'params = {'APPID':weather_key,'q':city}response = requests.get(url,params=params)# print(response.json())weather = (response.json())lable['text'] = format_response(weather)root = tk.Tk()canvas = tk.Canvas(root,height=HEIGHT,width=WIDTH)
canvas.pack()root .title ('my first Python program')frame = tk.Frame(root,bg='#80c1ff',bd=5)  #frame:框架,构架 bd:边宽
frame.place(relx=0.5,rely=0.1,relwidth=0.75,relheight=0.1,anchor='n')  #relx:指与左边距离,rely:与上边的距离entry = tk.Entry(frame,font=40,fg='green')
entry.place(relwidth=0.65,relheight=1)button = tk.Button(frame,text="Get Weather",bg='gray',font=('courier',10),command=lambda: get_weather(entry.get()))
button.place(relx=0.7,relwidth=0.3,relheight=1) # font:字体及大小,cammand:为命令lower_frame = tk.Frame(root,bg='#80c1ff',bd=10)
lower_frame.place(relx=0.5,rely=0.25,relheight=0.6,relwidth=0.75,anchor='n')lable = tk.Label(lower_frame,font=('courier',15))
lable.place(relwidth=1,relheight=1)root.mainloop()

4.第一次代码:

准备改进的地方:

(1)、添加背景图片,增加更多信息,添加天气等图标

(2) 、理解requests

​import tkinter as tk
import requestsHEIGHT = 400
WIDTH = 500def format_response(weather):try:name = weather['city']['name']desc = weather['list'][0]['weather'][0]['description']temp = weather['list'][0]['main']['temp']return ('City:%s \n Conditions:%s \n Temperature(℉):%s \n' %(name,desc,temp))except:return 'There was a problem \n retrieving that information'def get_weather(city):weather_key = 'd918307fea274e3a6812dbe2296fb205'url = 'http://api.openweathermap.org/data/2.5/forecast'params = {'APPID':weather_key,'q':city}response = requests.get(url,params=params)# print(response.json())weather = (response.json())lable['text'] = format_response(weather)root = tk.Tk()
canvas = tk.Canvas(root,height=HEIGHT,width=WIDTH)
canvas.pack()# d918307fea274e3a6812dbe2296fb205
# api.openweathermap.org/data/2.5/forecast?q={city name}&appid={your api key}'''
background_image = tk.PhotoImage(file=)
background_label = tk.Lable(root,image=background_image)
background_lable.place(relwidth=1,relheight=1)
'''frame = tk.Frame(root,bg='#80c1ff',bd=5)
frame.place(relx=0.5,rely=0.1,relwidth=0.75,relheight=0.1,anchor='n')entry = tk.Entry(frame,font=40,fg='green')
entry.place(relwidth=0.65,relheight=1)button = tk.Button(frame,text="Get Weather",bg='gray',font=('courier',10),command=lambda: get_weather(entry.get()))
button.place(relx=0.7,relwidth=0.3,relheight=1)lower_frame = tk.Frame(root,bg='#80c1ff',bd=10)
lower_frame.place(relx=0.5,rely=0.25,relheight=0.6,relwidth=0.75,anchor='n')lable = tk.Label(lower_frame,font=('courier',15))
lable.place(relwidth=1,relheight=1)root.mainloop()​

Python--weather.NO.1---了解tkinter和requests的基本用法相关推荐

  1. 【python学习总结1】Tkinter学习总结

    Tkinter的各种控件的介绍.语法格式.属性说明.常见方法: 可参考文章  Tkinter是什么 (biancheng.net) python tkinter可以使用的颜色 可参考文章 https: ...

  2. python requests 重定向_认识Python最最最常用语重要的库Requests

    Requests库是Python爬虫中最最最最最最重要与常见的库,一定要熟练掌握它.下面我们来认识这个库 Requests requests是Python最为常用的http请求库,也是极其简单的.使用 ...

  3. python界面设计-python图形化界面设计tkinter

    匿名用户 1级 2017-12-13 回答 python提供了多个图形开发界面的库,几个常用Python GUI库如下: Tkinter: Tkinter模块("Tk 接口")是P ...

  4. python中的gui界面编程_python应用系列教程——python的GUI界面编程Tkinter全解

    全栈工程师开发手册 (作者:栾鹏) python的GUI界面编程,常用的几个python库包含如下: Tkinter: Tkinter 模块(Tk 接口)是 Python 的标准 Tk GUI 工具包 ...

  5. python居中对齐代码end_Python tkinter.END属性代码示例

    本文整理汇总了Python中tkinter.END属性的典型用法代码示例.如果您正苦于以下问题:Python tkinter.END属性的具体用法?Python tkinter.END怎么用?Pyth ...

  6. Python 的 requests 库的用法

    Python爬虫利器一之Requests库的用法:http://cuiqingcai.com/2556.html Python利用Requests库写爬虫(一):http://www.jianshu. ...

  7. python爬虫 被重定向_爬虫篇 | 认识Python最最最常用语重要的库Requests

    最近整理一个爬虫系列方面的文章,不管大家的基础如何,我从头开始整一个爬虫系列方面的文章,让大家循序渐进的学习爬虫,小白也没有学习障碍. 爬虫篇 | Python爬虫学前普及 基础篇 | Python基 ...

  8. 【华为云技术分享】小白篇,认识Python最最最常用语重要的库Requests

    Requests库是Python爬虫中最最最最最最重要与常见的库,一定要熟练掌握它. 下面我们来认识这个库 Requests库是Python爬虫中最最最最最最重要与常见的库,一定要熟练掌握它. 下面我 ...

  9. Python使用鼠标滚轮调整tkinter应用程序窗口大小

    图书推荐: <Python程序设计基础与应用>(ISBN:9787111606178),董付国,机械工业出版社 图书详情: 用书教师可以联系董老师获取教学大纲.课件.源码.教案.考试系统等 ...

最新文章

  1. Spring-AOP的五种通知和切面的优先级、通知变量声明
  2. 开启Nginx的目录文件列表功能
  3. 什么是 SAP UI5 的 Component-preload.js, 什么是Minification和Ugification
  4. 基于IdentityServer的系统对接微信公众号
  5. 微型计算机生产制约因素,精品解析:广东省东莞市2019-2020学年高三下学期第一次统考(5月)模拟考试文科综合地理试题...
  6. (147)FPGA面试题-Verilog移位相加实现乘法(二)
  7. 使用Maven前夕(Maven项目架构管理工具、配置环境变量、阿里云镜像、本地仓库)
  8. 测试工程师值得被尊重!是否有此共鸣!
  9. doc命令操作数据库(下)
  10. 一起学libcef--libcef的基本类和方法介绍(如何产生一个你自己的浏览器)
  11. 题解 P1550 【[USACO08OCT]打井Watering Hole】
  12. H5游戏开发实例——数独游戏开发1
  13. Java 对接 阿里云 的短信服务完成短信的发送与查询
  14. C++搭建集群聊天室(十四):群聊功能
  15. vsftpd服务的黑名单和白名单
  16. UGUI-ContentSizeFitter之最简单实现maxSize限制
  17. 用python打造一颗爱心
  18. 医学图像领域,是时候用视觉Transformer替代CNN了吗?
  19. 用液晶显示器和纯平显示器浏览Blog
  20. 遵从自己的内心就永远不会迷失前进的方向

热门文章

  1. 比较传统数据与大数据
  2. 网页设计作业`京东网站设计——仿2016版京东首页(1页) HTML+CSS+JavaScript 大学生网页作品 电商网页设计作业模板 学生网页制作源代码下载
  3. BZOJ 2743: [HEOI2012]采花
  4. 任务态fMRI测量的重测可靠性:新的实证证据和元分析
  5. 用聚类算法计算股票的支撑位和阻力位
  6. nohup: 忽略输入 Error: Unable to access jarfile【已解决】
  7. 薄荷英语10月26(心理学百科)
  8. jvm系列之一:jvm结构
  9. Power BI 可视化:KPI 指标在卡片图中的突出显示
  10. PyTorch GPU计算