tkInter and Pillow don't display any images in GUIs - program gives errors instead - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: tkInter and Pillow don't display any images in GUIs - program gives errors instead (/thread-22029.html) |
tkInter and Pillow don't display any images in GUIs - program gives errors instead - SomeRandomGuy - Oct-25-2019 I'm searching the internet for like 2 hours now and can't find any solutions. I tried multiple images, converted them to .png, .gif and others, but nothing seems to work. I even tried to use Pillow at some point, but it resulted in "_tkinter.TclError: couldn't open "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=24x24 at 0x73C970>": no such file or directory" error all the time. I even thought the size might be the issue, so I tried to rescale the images multiple times, but nothing works. import tkinter as tk w, h = (864, 576) root = tk.Tk() def firstGUI(): canvas = tk.Canvas(root, height=h, width=w) canvas.pack() bg = tk.PhotoImage(file="characterwindow01.jpg") bg_l = tk.Label(root, image=bg) bg_l.place() firstGUI() root.mainloop()With this script, I get the error "_tkinter.TclError: couldn't recognize data in image file "characterwindow01.jpg"" and I don't know what to search or do anymore. I am quite new to Python (or more like old but I keep "abandoning" Python every now and then because I program strictly for myself and only when I need something), so try to explain as much as possible, if you don't mind. I use "def firstGUI():" for "faster access" later on so I can just open GUIs whenever I need to. Additional info:characterwindow01.jpg image is "stolen" from a game called Titan Quest, but only temporarily till I learn how to manage GUIs. I just felt that it looks quite good as a learning image as it has plenty of space to toy with. I use Python 3.7, as I didn't really bother updating it for some reason. Newest version of PyCharm is on duty! Using version 2019.2.3. I am on Windows 7 If I forgot to include something, please tell me. I used the help (https://python-forum.io/misc.php?action=help) but I might've overlooked something. RE: tkInter and Pillow don't display any images in GUIs - program gives errors instead - Yoriz - Oct-25-2019 Try using pillow to open the image import tkinter as tk from PIL import Image, ImageTk w, h = (864, 576) root = tk.Tk() def firstGUI(): canvas = tk.Canvas(root, height=h, width=w) canvas.pack() bg = ImageTk.PhotoImage(Image.open("characterwindow01.jpg")) bg_l = tk.Label(root, image=bg) bg_l.place() firstGUI() root.mainloop() RE: tkInter and Pillow don't display any images in GUIs - program gives errors instead - SomeRandomGuy - Oct-25-2019 Doesn't work: https://imgur.com/a/u5Hw2I8 (image of result and proof I changed the script and how the image should look like under it - two pictures in one link). Script: import tkinter as tk from PIL import Image, ImageTk w, h = (864, 576) root = tk.Tk() def firstGUI(): canvas = tk.Canvas(root, height=h, width=w) canvas.pack() bg = ImageTk.PhotoImage(Image.open("characterwindow01.jpg")) bg_l = tk.Label(root, image=bg) bg_l.place() firstGUI() root.mainloop() RE: tkInter and Pillow don't display any images in GUIs - program gives errors instead - joe_momma - Oct-26-2019 its getting garbage collected and it will not show import tkinter as tk from PIL import Image, ImageTk w, h = (864, 576) root = tk.Tk() canvas = tk.Canvas(root, height=h, width=w) canvas.pack() bg = ImageTk.PhotoImage(Image.open("characterwindow01.jpg")) bg_l = tk.Label(root, image=bg) bg_l.place() root.mainloop() RE: tkInter and Pillow don't display any images in GUIs - program gives errors instead - SomeRandomGuy - Oct-28-2019 (Oct-26-2019, 04:05 PM)joe_momma Wrote: its getting garbage collected and it will not show So that would basically mean I can not use functions at all, even if I need them. I'm planning on doing something bigger than just a notepad with an image. Basically my imagination tells me about at least 3 additional GUIs - probably even more than that depending on what else I'll want to do, but that's for the future, first I want to learn how it's going with it. I've even tried to convert it into a .gif (via Gimp or GNU - however you want to call it) but it didn't change anything. All in all, your "garbage collecting" idea doesn't seem to work either: https://i.imgur.com/rYexQTn.png RE: tkInter and Pillow don't display any images in GUIs - program gives errors instead - joe_momma - Oct-28-2019 Quote:So that would basically mean I can not use functions at all,Nope just in your example, if you want success use guizero. tkinter is best implemented in a class. bg_l.place()where are you placing the image? Quote:place = place_configure(self, cnf={}, **kw) RE: tkInter and Pillow don't display any images in GUIs - program gives errors instead - SomeRandomGuy - Oct-29-2019 (Oct-28-2019, 07:21 PM)joe_momma Wrote:Quote:So that would basically mean I can not use functions at all,Nope just in your example, if you want success use guizero. tkinter is best implemented in a class. I actually looked into Guizero (haven't done anything in there yet), and I have to admit it looks way better. What I like the most there is that you don't have to type three lines of code to apply an image, but just one app = App(title="guizero grid span example", width=460, height=210, layout="grid") picture1 = Picture(app, image="std1.gif", grid=[0,0])where "app" is the main window. A link to the website that seems to have a nice explanation: https://pythonprogramming.altervista.org/gui-zero/ RE: tkInter and Pillow don't display any images in GUIs - program gives errors instead - Evil_Patrick - Oct-29-2019 Try this from tkinter import * root = Tk() canvas = Canvas(root, height = 864, width = 576) canvas.pack() img = PhotoImage(file = r'[file path]') bg = Label(canvas, image = img) bg.pack() root.mainloop() RE: tkInter and Pillow don't display any images in GUIs - program gives errors instead - SomeRandomGuy - Oct-29-2019 (Oct-29-2019, 12:37 PM)Evil_Patrick Wrote: Try this This did work: https://i.imgur.com/0fBt68B.png RE: tkInter and Pillow don't display any images in GUIs - program gives errors instead - SomeRandomGuy - Oct-29-2019 Is it even possible to set GUI element on a GUI element? I chose to use Guizero because it doesn't require me to convert all .jpg files to .gif, and the only thing I can do is either placing them on top of the image right in the middle or to the sides of the image. https://imgur.com/a/iqKOfNg from guizero import * app = App(title="Gui", height=576, width=864, layout="grid") pic = Picture(app, image="characterwindow01.jpg", height=576, width=864, grid=[1, 1]) text = Text(app, text="Text text", color="#FFAADD", bg="#000000", grid=[1, 1]) text2 = TextBox(app, text="Text textbox", grid=[1, 1]) app.display()I'm starting to think I should use another language for my GUIs... |