|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
小甲鱼54讲第一道动手题
我是用tkinter写的
- import tkinter as tk
- import urllib.request
- # check if the input is number
- def test(content):
- return content.isdigit()
- # down the pic
- def downloadPic(file_name, w, h):
- print(0)
- url = "http://placekitten.com/g/" + w + "/" + h
- print(1)
- req = urllib.request.Request(url)
- print(2)
- response = urllib.request.urlopen(req)
- print(3)
-
- cat_img = response.read()
- print(4)
- with open(file_name, "wb") as f:
- print(5)
- f.write(cat_img)
- print(6)
- # get the file name
- def saveFile(w, h):
- file_name=tk.filedialog.asksaveasfilename(defaultextension=".jpg", \
- title="Save Pussy", filetypes=[("JPG", ".jpg")])
- # if user cancle saving file, then don't download pic
- if file_name=="":
- return
- downloadPic(file_name, w, h)
- def main():
- root = tk.Tk()
- root.title("Get A Pussy")
- root.minsize(240, 120)
- root.resizable(False, False)
- testCMD=root.register(test)
- width=tk.StringVar()
- height=tk.StringVar()
-
- # title
- tk.Label(root, text="-- Input the Size of the Pussy --")\
- .grid(row=0, columnspan=6)
- # width label and height label
- tk.Label(root, text="Width:")\
- .grid(row=1, column=0, padx=10)
- tk.Label(root, text="Height:")\
- .grid(row=2, column=0)
- # width entry and height entry
- ew=tk.Entry(root, width=26, textvariable=width, validate='key', \
- validatecommand=(testCMD, '%P'))
- eh=tk.Entry(root, width=26, textvariable=height, validate='key', \
- validatecommand=(testCMD, '%P'))
- # set the default size
- ew.delete(0, tk.END)
- eh.delete(0, tk.END)
- ew.insert(0, "40")
- eh.insert(0, "60")
- ew.grid(row=1, column=2, columnspan=4, padx = 10, pady=5)
- eh.grid(row=2, column=2, columnspan=4)
- tk.Button(root, text = "OK", width=15, command=lambda: saveFile(width.get(), height.get()))\
- .grid(row=3, column=0, columnspan=3, padx=10, pady=10)
- tk.Button(root, text="Cancel", width=15, command=root.quit)\
- .grid(row=3, column=3, columnspan=3, padx=10)
- tk.mainloop()
- if __name__ == "__main__":
- main()
复制代码
然后运行正常与否完全是随机发生
在linux下不能运行
在Windows下双击文件打开,OK键根本不能用
只有在IDLE下运行,才有概率能够成功
有没有大佬能够帮我看看那里出了问题 |
|