|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import easygui as g
import urllib.request
def main():
msg = "请填写喵的尺寸"
title = "下载一只喵"
fieldNames = ["宽:", "高:"]
fieldValues = []
size = width, height = 400, 600
fieldValues = g.multenterbox(msg, title, fieldNames, size)
while 1:
if fieldValues == None:
break
errmsg = ""
try:
width = int(fieldValues[0].strip())
except:
errmsg += "宽度必须为整数!"
try:
height = int(fieldValues[1].strip())
except:
errmsg += "高度必须为整数!"
if errmsg == "":
break
fieldValues = g.multenterbox(errmsg, title, fieldNames, fieldValues)
url = "http://placekitten.com/g/%d/%d" % (width, height)
response = urllib.request.urlopen(url)
cat_img = response.read()
filepath = g.diropenbox("请选择存放喵的文件夹")
if filepath:
filename = '%s/cat_%d_%d.jpg' % (filepath, width, height)
else:
filename = 'cat_%d_%d.jpg' % (width, height)
with open(filename, 'wb') as f:
f.write(cat_img)
if __name__ == "__main__":
main()
请问在选择喵的尺寸页面,点击cancle以后,如何阻止 保存页面弹出呢?因为选择喵的尺寸后,点击OK,出现保存页面才是顺理成章的事情。
可以加一行代码:
- response = urllib.request.urlopen(url)
- cat_img = response.read()
-
- [b]if fieldValues:[/b]
-
- filepath = g.diropenbox("请选择存放喵的文件夹")
- if filepath:
- filename = '%s/cat_%d_%d.jpg' % (filepath, width, height)
- else:
- filename = 'cat_%d_%d.jpg' % (width, height)
- with open(filename, 'wb') as f:
- f.write(cat_img)
复制代码
加了那一行加粗的代码 (if fieldValues:),就是说如果easygui的界面关闭了就不运行之后的代码
|
|