How To Use Images as Backgrounds - Python Tkinter GUI Tutorial
ฝัง
- เผยแพร่เมื่อ 29 ม.ค. 2025
- In this video I'll show you two different ways to use Images as your app's Background with Tkinter and Python.
We'll look at using place to use images as backgrounds, and also how to use the Canvas to use images as backgrounds.
Both have positives and negatives to them, but depending on what you need to do, one or the other method should work for you.
▶️ Watch Entire Tkinter Playlist ✅ Subscribe To My TH-cam Channel:
bit.ly/2UFLKgj bit.ly/2IGzvOR
▶️ See More At: ✅ Join My Facebook Group:
Codemy.com bit.ly/2GFmOBz
▶️ Learn to Code at Codemy.com ✅ Buy a Codemy T-Shirt!
Take $30 off with coupon code: youtube1 bit.ly/2VC9WUN
▶️ Get The Code
bit.ly/3l8v3aE
please pin this comment, its not pinned
@@vaibhavsaran7124 yes it is
i'm a django developper but i came here just to push the like button because you are so fricking cool , You Tutorials are on point and super helpful
Awesome, thanks!! I'm planning more Django videos soon :-p
@@Codemycom God bless you
@@hankoiba9032 :-)
I wish you were my python teacher in my college... I'm struggling a lot to learn python. From now onwards I won't cause I found you SIR. Thank you so much for your explanation. It was really an amazing explanation I have ever heard from anyone.....
Yeah John has the best tkinter yt channel
Thanks!
Hope you make such videos for kivy too😍
@@Codemycom that's true bro I have learned a lot from this channel as compared to google
Have u checked *coding lifestyle 4u* for tkinter projects ?
Oh man! If I spent my life, thanking you, it will not be enough❤️❤️
You're very welcome!
Have u checked *coding lifestyle 4u* for tkinter projects ?
@@Codemycom how do i specify desktop in the direction?I think for that i need to specify the username but what if i dont know it?
@@lsantez Sorry, I have no idea what you're asking me
@@Codemycom how do i specify a username if i dont know wht it is for example in batch you can C:/Users/%USERNAME%/blah blah but how do i do it in python
Thanks man, this helped !
After watching few of his videos, I am convinced that buying his courses is gonna be worth it ! 🙌🏻
Glad it helped!
Thank you is not enough for you my friend. this was such a headache for me until i watched your video. thank you a million times over. you are truly one of a kind.😃😃😃❤❤💯💯
Glad you liked it!
Perfect timing, this what we were looking for our project. I was stuck on that problem last night. Your videos are the best. How would i add the voice to text within the canvas based on user's input. It works with a normal button that is not on a canvas. I think you hit on something in this video
Glad I could help! Just give your button a command that points to a function in the normal way you always do. Should work just fine...
I have learnt a lot from you. When I am not coding I keep murmuring tkinter and python, trying to mimic you 😃
Awesome
@@Codemycom you really helped me in doing my cs project I made a time table using buttons which on pressing gives message and a voice too a bigggggg thank you😃👍❤️
@@surbhitagare1889 Awesome!
@@Codemycom thank you very much love from india
This channel is the best .....your explanation is the best
Thank you so much 😀
am always tracking your videos thinking i"l one day be the first to watch...now am the 46th person just after 10min. awesome always!!!!!
Ha fun!
Excellent tutorial!!! Very easy to follow!
Glad you enjoyed it!
Man your videos are truly a lot of help! 🔥🔥
Glad you're enjoying them!
I wish if i could like this video 100 times 😀👍🏻
Thanks!
Well found today and gave a sub :) Thanks for providing such content
Welcome aboard!
Wow! I've learned a lot from this video, thanks Elder👌🏾
Awesome!!!!
ive been waiuting for this a long time!!!
Awesome
Have u checked *coding lifestyle 4u* for tkinter projects ?
If you get the error that the image can't be read just remove 'root' from this line
bgimage_label = Label(root, image=bg_image)
and if the image is jpg, convert it to png
does i have to show path of the file when the tkinter files are somewhere else? how do i know where i put my tkinter files?
Thanks man...Love your videos....
Keep making !
Thanks, will do!
This is great! but... tell me. is there an easier way to... ok, starting with the beginning. I used your second method to set the background image. That is all working find until you want to change the background image... Since we write text directly on the canvas, deleting the old image and setting up a new one loose all the text. I really wish to change the background at night time to reduce the light. Any trick for me?
Hey, I get a blank screen when I execute that. The photos are in the same directory as the code, any idea what I can do to fix it?
same problem here
You know how to Fix this problem now? Because i have the same problem can you help me?
Search this in Google as it's kinda hard to explain why it happens. Python garbage collector and tkinter referencing don't go hand in hand, so you need to create a separate reference of the image to make it work
Thanks John. That was great❤️
Glad you enjoyed it!
I know I'm like really late, but I've been wondering, how would I set the text background as transparent so I can see the background image through the text?
I have been searching for the same thing but couldnot find anything about that.Have you got any idea till now?
when you resize the program window the background doesnt fill and the text is no longer centered. other than that your tutorial works well but the code please i need to see the whole window going back and fourth and replying alot makes it harder to read.
for some reason root and bg arent defined in your code but its giving me an error does it have anything to do with your code editor or did you define those terms off screen?
edit nvm im just dumb
Keep going brother
Always
Great video as always, but what If I want the buttons to be invisible as well, so we just see the text inside them ?
Have u checked *coding lifestyle 4u* for tkinter projects ?
Hi thank you for the tutorial, is there a way to add animted background like a video or a gif? Thank you
yes but not easily
HOW are you SO helpful!
Glad you think so!
Could you please share your code Github Link makes it a lot easier to take the code for project
It's always in the pinned comment.
@@Codemycom but you haven't pinned the comment
@@vaibhavsaran7124 yes I have
I tried this (canvas method) and my image isn't being displayed properly. It hugs in the northwest corner but the image is too big. How do I make it so that the image fits perfectly inside of the window?
from PIL import Image, ImageTk
Img = Image.open(path)
Img = img.resize(width,height)
tk_img = ImageTk.PhotoImage(img)
can = tk.canvas(root,width=width, height=height)
can.pack(fill="both", expand=True)
can.create_image(0,0,image=tk_img,anchor="nw")
Can you please make videos on the canvas widget, more in dept information
I have a bunch of videos in the playlist on canvas
Hello, if i dont want use PTS to take background colour, can i insert .PNG image instead?
Hello and thank you for your explanation.
I have a problem with my code. I followed as you did in the video up to 19:54 minutes. I changed somethings like the image name and it is in the same directory as the code, also the size of the picture, but I don't think those changes are the problem.
What am I doing wrong?
Traceback (most recent call last):
File "d:\Documents\byu-idaho\2_semester\cse111\w12\personal_budget\teste_bg_image.py", line 17, in
my_canvas.create_image(0,0, Image=bg)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 2819, in create_image
return self._create('image', args, kw)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 2805, in _create
return self.tk.getint(self.tk.call(
_tkinter.TclError: unknown option "-Image"
Image should be lowercase on line 17. image
Thank you so much!
@@henryfox2001 sure thing
Thanks man you are the best
Welcome!
You just earned a sub! Is there any way you could make a video on using videos or image sequences that loop as a background in tkinter?
Sorry no, video in tkinter is not really a thing
How do you remove the white boarder around the canvas (background image)?
Never mind I found it in a later video.
th-cam.com/video/32v2rdQnXvQ/w-d-xo.html
-4:00
Was just about to say that :-p
highlightthickness=0 when u define the canvas
Thank you for this video!
Welcome!
hello sir! i dont get my background image and texts one upon the other. I get it image first and the the rest of them bottom of it. Can you please help me?
Sir, is this video related to gradient background?
Of course. Watch it and see lol it relates to any image background. Just use a gradient image as your background image.
Can you make a video to dynamically resize the background and thank you for the videos i really learned a lot because of you
Hm, interesting idea...I'll think on it.
@@Codemycom Thank you so much ❤️
any idea how to make a matplotlib chart background transparent in a tkinter window!?
Why are we passing root in every methods like
Button(root,) Label(root,)
I have used tkinter without passing window name(root here) and it works just fine.
what is the difference between them(using root and not using root).
Can u plz tell how I can add button images with another image as a background
I've tried this method bit I get a grey square background in my button
I don't know how to get rid of that , I can't set a background colour cuz there are variety of colours in the bg image
Can u plz help
Managed to style root window with JPEG image as background using pillow and place attribute. While trying to do the same for toplevel window it says photoImage has no attribute place. (Already made variable a global variable in the function definition)
Hello sir,
Sir when I run the code, it comes out good, but, after expanding the window to its maximum size, the background image does not fill the rest of the portion left on the window. Please help....
Thank you
Love from India
thanks, that was very interesting
Glad you enjoyed it
Sos mi héroe
Sir I am getting tclerror no such directory. What should I do . Please help me sir
Check your code for typos. Did you create that directory?
@@Codemycom yes sir
@@sudharsanvanamali1514 in the correct place?
when doing in mac os, buttons still having background color
Hello sir
How to do the same with checkbutton, the background of the checkbutton is white,how do we change the background of the checkbutton to merge with background image please help me sir
Video on moving widgets other than canvas with keys in tkinter
how i can create a tk.text or tk.entry with fg= transparent?
Thanks man 👍
You bet
Showing error
_tkinter.TclError: couldn't recognize data in image file "D:\Petrol Pump Management System\Petro Station PRO\LoginFrame.png"
>>>
try different image format
PNG is fine, your image is invalid use another image
For some weird reason my PIL doesnt get imported... Im using this code:
from PIL import ImageTk, Image
Any help?
what have you done differently from the video? Used different tools? Failed to pip install Pillow?
Thank you teacher... very good...
Glad you liked it!
Can we use background video in Tkinter using canvas?
Not really
Hi,
any idea why do i get the below error msg?
_tkinter.TclError: couldn't recognize data in image file "C:\projects\socix\vld-dev-env\IDELauncher\images\Background-1.gif"
same error with png
_tkinter.TclError: couldn't recognize data in image file "images\hulk.png"
hi john elder did you know you can press Ctrl+b in sublime text to run your code?
Of course, but it's not a good way to do it and doesn't always work and I don't want to promote doing that to students...especially newbies.
@@Codemycom ok but can pls tell what is bad about running it with sublime text
@@komodojack4346 It's not a proper terminal, it doesn't always work and can give errors. We want to use professional tools and do things the right way. Why promote bad habits?
@@Codemycom ok
Sir I'm getting error when I am using the "file" attribute and its shows blank white space if not used the fill attribute
couldn't recognize data in image file "E:\Matheran\Deep.jpeg" what does this error means..
Hey
I'm getting the following error when using the PIL
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\PIL\Image.py", line 2882, in open
fp.seek(0)
AttributeError: 'str' object has no attribute 'seek'
Try googling: "AttributeError: 'str' object has no attribute 'seek'"
How can we use video files as background
i get a permission denied error when tkinter tries to open up an image .Any clue on how to fix?
No, you'll have to google the exact error
Can this be implement in raspberry pi ?
sure
I think the version of python im on doesn't have create image that sucks i was gonna do this for my menu for my A level project....
Of course it does. Whatever problem you are having has nothing to do with the version of python you're using.
Hey bro is there anyway to remove this white border?
Yeah keep watching the playlist, I go over that
Why do I have "couldn't recognize data in image file"?
because it can't recognize the data in the image file :-p
@@Codemycom So how do can I solve the problem?
@@noname4011 Use a valid image file
@@Codemycom Yess, thank you! 👍
So thanks👍🏻💗
You’re welcome
any way to do the same for checkbuttons?
try it and see
@@Codemycom I tried its not working :(
Hello, I want to change the background. But it didn't work out. Can you help me?
Just watch and follow the video. If it didn't work, you typed something wrong. Try again.
@@Codemycom Thank you!
Cara você é o melhor mim ajudou muito thanks
Can i do this with gifs too?
I doubt it
Hello Dear Sir
really interesting your lecture
I am new in Python and I want to be a Programmer so how to start from where need your opnion thanks
I would start learning all you can about Python. I have a free python playlist on my channel, I would start there.
@@Codemycom Thanks but which program also mandatory with Python Programming?
@@zabijahesh4097 There is nothing mandatory
can we replace images with videos as background
No, not really
can you make something like "CSV File editor with Python" ??
I have a whole course on that at Codemy.com
is there any way to set .gif file as background in tkinter ?
this method works with gif
John thanks for such an amazing playlist. Quick question, can the Transparent widget hack used for the 1st method to get rid of the widget bg?
i keep getting an error no such file or directory idk how to find it.
you have to use a .png or .gif file. I got the same thing when I was trying to use a .jpg.
Can I create a entry this way?
how do you mean?
how to use diffrent images on diffrent pages in tkinter ( ex: one back in login page and another one in next page)
In the same way. Make the image variable global
It returns the error image "pyimage1" doesn't exist, while I am completely sure that I have specified the directory of the image, Here is my code.
from tkinter import * # As usual
appico = PhotoImage(file="renpy.jpg")
img_proc = Label(about_page, image=appico)
img_proc.grid(column=0, row=2)
the image is stored in the same directory this py file is in, and also it is named as renpy.jpg.
How can i make a curve line
Also I tried your code it does not work
lol of course it works. You can literally watch the video and see that it does. You have a typo.
Dosen't work at all, displays the error TclError: image "pyimage5" doesn't exist.
what did you do differently from the video?
Sir how to link registration buttton to register page
How to put multiple changing background images
In the same way...just use a timer
@@Codemycom how do I provide different image address in real time or I am saying wrong ???
@@shivanshukamboj7940 depends on what you mean by real time...
@@Codemycom please elaborate this or it will be fantastic if you make a video on this
@@shivanshukamboj7940 I can't, I'm not sure what you're talking about
can you set a gif file as the background?
Not really
Can you show me how to make a live wallpaper using tkinter 🥺🥺
No
you may change it using time.sleep(2)... or draw it in canvas, putting stars randomly with time sleep...
Object has not attribute '.create_image '
:(
Thanksss a lot ❤️
Glad you enjoyed it
Thanks
the last part of the vidoe is hard to follow because your only showing a part the code.
Thanks you so much
Most welcome
@@Codemycom do you know if i can u Canvas for gif too ? I create my and i want to use it like the galaxy image
@@axel9717 Yes but not in the way you think, it's fairly complicated
@@Codemycom do you have a video or a link to explain ? I didn’t find on the web
@@axel9717 No, sorry
Tkinter is never able to read image data. and its frustrating af.
I am not sure, but I think it is only works if your image pixels are also 800x500
huh?
Sorry! now I see you have mentioned it right at the start.
@@peterpacsay8358 yep
You say check the pin comment where is it ??
It's the first comment listed in the comment section...the one with all the info in it.
how to use .grid instead of .pack
I have dozens of videos on the playlist using grid.
@@Codemycom ok two more question
_how to call a fonction that is inside another to a function
_how can create a data base to get and store all my files and applications location
another* fonction
@@Russia_-dy7ys try functionone(functiontwo()) and I have lots of videos on databases with tkinter
How to add image on it
What do you mean?