Created
January 4, 2017 08:37
-
-
Save ctosib/da6024d44d6e80143768edf88f572c46 to your computer and use it in GitHub Desktop.
利用tkinter顯示網路上的圖片
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import Tkinter as tk | |
import io | |
from PIL import Image,ImageTk | |
class Mainapplication(tk.Frame): | |
def __init__(self,master=None): | |
tk.Frame.__init__(self,master) | |
self.pack() | |
self.createwidget() | |
def createwidget(self): | |
self.label = tk.Label(self,text="hello world") | |
self.label.pack() | |
res = requests.get("https://s1.yimg.com/rz/d/yahoo_frontpage_zh-Hant-TW_s_f_p_bestfit_frontpage_2x.png") | |
self.imagebyte = io.BytesIO(res.content) | |
self.imagepil = Image.open(self.imagebyte) | |
self.imagetk = ImageTk.PhotoImage(self.imagepil) | |
self.label.config(image=self.imagetk) | |
if __name__=='__main__': | |
root = tk.Tk() | |
app = Mainapplication(master=root) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment