|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
054讲 课后题动动手0
- import easygui as g
- import urllib.request as u
- def b():
- width,high = g.multenterbox('请填写喵的尺寸','下载一只喵',['宽:','高:'],[400,600])
- path = g.diropenbox('请选择存放喵的文件夹')
- def c():
- response = u.urlopen('http://www.placekitten.com/g/' + width + '/' + high)
- cat_img = response.read()
- with open(path + '\\cat_' + width + '_' + high + '.jpg','wb') as j:
- j.write(cat_img)
- def a():
- try:
- b()
- except TypeError:
- g.msgbox('未选择存放文件夹,请重新选择!')
- a()
- else:
- c()
- a()
复制代码
NameError: name 'width' is not defined
为什么明明运行了b(),width、high、path的依然没有被赋值呢?
两个方法
1、声明全局变量,即在def方法体外面什么width 和high - import easygui as g
- import urllib.request as u
- width = 0
- high = 0
- def b():
- width,high = g.multenterbox('请填写喵的尺寸','下载一只喵',['宽:','高:'],[400,600])
- path = g.diropenbox('请选择存放喵的文件夹')
复制代码
2、运行b()是返回width和high,调用c()的时候传进去
- import easygui as g
- import urllib.request as u
- def b():
- width,high = g.multenterbox('请填写喵的尺寸','下载一只喵',['宽:','高:'],[400,600])
- path = g.diropenbox('请选择存放喵的文件夹')
- return width,high
- def c(witdh, high):
- response = u.urlopen('http://www.placekitten.com/g/' + width + '/' + high)
- cat_img = response.read()
- with open(path + '\\cat_' + width + '_' + high + '.jpg','wb') as j:
- j.write(cat_img)
- def a():
- try:
- witdh,high = b()
- c(witdh, high)
- except TypeError:
- g.msgbox('未选择存放文件夹,请重新选择!')
- a()
- a()
复制代码
|
|