| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
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() 
 
 
##为啥  try: 
            width = int(fieldValues[0].strip()) 
        except: 
            errmsg += "宽度必须为整数!"这里看不懂呢 已经用int变成整数了啊如果我输入456.47 那int后就变成456为啥还能执行except下面的语句呢 求指点
fieldValues = g.multenterbox(msg, title, fieldNames, size) 
这里的参数会把用户的输入转成字符串,所以 int(fieldValues[0].strip()) 才能用 strip 
int('5.5') 会报错的但是 int('5') 可以 
 
 
 |   
 
 
 
 |