Python Forum
Adding an image to a tkinter window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding an image to a tkinter window
#1
Hi I'm trying to add an image to a tkinter window using the following code
from tkinter import * # gives access to tkinter GUI library

def hello():
    name = textbox1.get() # gets name from textbox1
    print = "Hello " + name # creates Hello name string 
    textbox2["text"] = print # prints that string in textbox2

window = Tk() # opens tkinter window
window.title("Names") # sets title of the window as names
window.geometry("1000x1000") # dimentions
window.wm_iconbitmap("Icon.ico") # changes tkinter window icon 
window.configure(background  = "black") # sets window background to green

logo = PhotoImage(file = "logo.gif") # displays logo.gif in a label widget
logoimage = Label(image = logo) # this can't be changed while the program is running 
logoimage.place(x = 50, y = 20, width = 200, height = 120) # position and dimentions

textbox1 = Entry(text = "") # creates textbox1 where data is entered 
textbox1.place(x = 145, y = 200, width = 125, height = 25) # position and dimentions
textbox1["bg"] = "white" # white background 
textbox1["fg"] = "black" # black font

label1 = Label(text = "Enter a name") # creates label with this text
label1.place(x = 20, y = 200, width = 125, height = 25) # position and dimentions
label1["bg"] = "red" # red background 
label1["fg"] = "blue" # blue font

textbox2 = Message(text = print) # creates textbox1 displaying the variable print
textbox2.place(x = 145, y = 250, width = 125, height = 25) # position and dimentions
textbox2["bg"] = "white" # white background 
textbox2["fg"] = "black" # black font

button1 = Button(text = "Press me", command = hello) # creates button which runs the hello sub-program
button1.place(x = 20, y = 250, width = 125, height = 25) # position and dimentions
button1["bg"] = "blue" # blue background 
button1["fg"] = "red" # red font

window.mainloop() # keeps program running 
but I get the following error saying that there is no such file or directory, however the image is saved in the same place as the program. Can anyone help?

Error:
File "c:/Users/djwil/Documents/python/learning python/Chapter 17 - more Tkinter/Ch17-c1.py", line 14, in <module> logo = PhotoImage(file = "logo.gif") # displays logo.gif in a label widget File "C:\Users\djwil\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 4061, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Users\djwil\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 4006, in __init__ self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: couldn't open "logo.gif": no such file or directory
Reply
#2
Good job on the topic. Next time post GUI questions to the GUI part of this forum.

Also next time create a shorter example with the fewest number of external dependencies possible. People are more likely to offer help if you make it easy for them. This is all that's required to demonstrate your problem.
from tkinter import *
window = Tk()
logo = PhotoImage(file = "image.gif")
I took a .png file I had and converted to .gif and I get the same error you do. Then I changed the code to use the .png file and it works. Guess this is another incompatible file format problem. Interesting since effbot says .gif is a compatible format. Hmmmm.

If you want to work with images I think you should take a look at the Python Imaging Library (PIL). If you really want to do GUI programming I think you should look into something newer and better than Tk. Anything is newer and better than Tk.
Reply
#3
(Aug-05-2020, 08:48 PM)deanhystad Wrote: If you want to work with images I think you should take a look at the Python Imaging Library (PIL). If you really want to do GUI programming I think you should look into something newer and better than Tk. Anything is newer and better than Tk.

For example?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Scrollable big image in a window (TKinter) Prospekteur 3 202 Sep-14-2020, 03:06 AM
Last Post: Larz60+
  Run more than one window together with tkinter SmukasPlays 1 206 Aug-24-2020, 05:00 PM
Last Post: Larz60+
  [Tkinter] calling a new window from a tkinter window neuroprogrammer 2 283 Jul-28-2020, 10:59 PM
Last Post: deanhystad
  How to display results from terminal window onto tkinter. buttercup 0 194 Jul-21-2020, 04:41 AM
Last Post: buttercup
  Create image on a Toplevel using tkinter ViktorWong 3 1,518 Jun-13-2020, 03:21 PM
Last Post: deanhystad
  [Tkinter] Tkinter Logo Image Evil_Patrick 1 599 May-25-2020, 05:12 AM
Last Post: DT2000
  [Tkinter] Tkinter adding entry values scratchmyhead 1 340 May-04-2020, 05:21 AM
Last Post: Yoriz
  Tkinter: multitab window WizardMitMagi 3 483 Apr-29-2020, 02:40 PM
Last Post: deanhystad
  tkinter.TclError: bad window path name kenwatts275 3 1,362 Apr-26-2020, 08:16 PM
Last Post: kenwatts275
  tkinter and image paths menator01 2 421 Apr-14-2020, 08:46 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020