Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5946107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:46:33+00:00 2026-05-22T16:46:33+00:00

I want a function that, when a button is clicked, it will take an

  • 0

I want a function that, when a button is clicked, it will take an image from the web using URLLIB and display it in a GUI using TKINTER.

I’m new to both URLLIB and TKINTER, so I’m having an incredibly difficult time doing this.
Tried this, but it obviously doesn’t work because it uses a textbox and only will display text.

 def __init__(self, root):
    self.root = root
    self.root.title('Image Retrieval Program')
    self.init_widgets()


def init_widgets(self):
    self.btn = ttk.Button(self.root, command=self.get_url, text='Get Url', width=8)
    self.btn.grid(column=0, row=0, sticky='w')

    self.entry = ttk.Entry(self.root, width=60)
    self.entry.grid(column=0, row=0, sticky='e')

    self.txt = tkinter.Text(self.root, width=80, height=20)
    self.txt.grid(column=0, row=1, sticky='nwes')
    sb = ttk.Scrollbar(command=self.txt.yview, orient='vertical')
    sb.grid(column=1, row=1, sticky='ns')
    self.txt['yscrollcommand'] = sb.set

def get_url(self):
    s = urllib.request.urlretrieve("http://www.smellymonkey.com/monkeys/images/ill-monkey.gif", "dog.gif")
    tkimage = ImageTk.PhotoImage(im)
    self.txt.insert(tkinter.INSERT, s)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:46:34+00:00Added an answer on May 22, 2026 at 4:46 pm

    I don’t use python 3, but I can give an answer that works in python 2.5+. I assume the code will work nearly identically on python 3.

    Before we get started, we need to import Tkinter and create the root window:

    import Tkinter as tk
    root = tk.Tk()
    

    Next, to download the image using urllib:

    import urllib
    URL = "http://www.smellymonkey.com/monkeys/images/ill-monkey.gif"
    u = urllib.urlopen(URL)
    raw_data = u.read()
    u.close()
    

    You now have the binary data for the image in the variable raw_data. Tkinter accepts a data option, but unfortunately you can’t feed it this raw data. It expects the data to be encoded as base64. This is easy enough to do:

    import base64
    b64_data = base64.encodestring(raw_data)
    image = tk.PhotoImage(data=b64_data)
    

    Now that we have an image, time to put it on the screen:

    label = tk.Label(image=image)
    label.pack()
    

    You should now see the image on the screen.

    The above only works for .gif images, but other image formats are almost as easy to handle. The simplest method is to write the raw image data to disk (or use urllib to directly download the data to a file) and reference the file when creating the PhotoImage object. Of course, this only works for image formats directly supported by the PhotoImage class.

    Your other choice is to use PIL (Python Image Library) which supports many different image formats. The technique remains roughly the same, only you must first create a PIL image, then convert it to a format usable by Tkinter. For more information about PIL consult the Python Imaging Library Handbook and also the effbot documentation on The Tkinter PhotoImage Class

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing code that will, when a button is clicked, display some simple text
I just want a function that can take 2 parameters: the URL to POST
I want a spreadsheet function that will produce a sum of all values in
I want to have a function that will return the reverse of a list
Alright, so I have a button that when clicked, will change the class of
CSS3 using Webkit in Safari; I have a button that, when clicked, causes a
I know that using .submit will trigger an event when the submit button is
Say I want a function that takes two floats ( x and y ),
I'm trying to call a function that contains jQuery code. I want this function
I want a simple function that receives a string and returns an array of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.