How do I remove the white frame around the photo in Tkinter? - python

When I upload an Image in tkinter there is a white frame around the picture. I searched a lot but I didn't foud any answer.
Picture of the white frame:
Here is my code:
from tkinter import *
from PIL import ImageTk,Image
root = Tk()
root.geometry("1200x583")
root.resizable(0,0)
place_one=Image.open("p1.jpg")
place_one=place_one.resize((1200,583))
place_one_2=ImageTk.PhotoImage(place_one)
place=Label(root,image=place_one_2)
place.place(x=-2,y=0)
character=Image.open("m2.gif")
character=ImageTk.PhotoImage(character)
L1=Label(root,image=character)
L1.place(x=0,y=355)
root.mainloop()

You need to set image border width (borderwidth) ,
L1=Label(root,image=character, borderwidth=0)
Edit:
Do not use import *(why it is bad?) try to,
import tkinter as tk
from PIL import ImageTk,Image
root = tk.Tk()
root.geometry("1200x583")
root.resizable(0,0)
place_one=Image.open("p1.jpg")
place_one=place_one.resize((1200,583))
place_one_2=ImageTk.PhotoImage(place_one)
place=tk.Label(root,image=place_one_2)
0place.place(x=-2,y=0)
character=Image.open("m2.gif")
character=ImageTk.PhotoImage(character)
L1=tk.Label(root,image=character, borderwidth=0)
L1.place(x=0,y=355)
root.mainloop()

Related

How do I use an image for a project using PIL in Python code?

I am trying to make a blackjack game in Python using Tkinter and PIL packages. I saved all the necessary cards in a folder on my desktop but cannot seem to get the card images to appear.
Below is the code I have tried :
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("Blackjack")
canvas = Canvas(root, bg="green", height=1200, width=800)
canvas.pack(fill=BOTH, expand=1)
img = ImageTk.PhotoImage(Image.open(
"C:\Desktop\GUI\Images\Cards\2_of_clubs.png"))
root.mainloop()
How to make images appear when using tkinter ?

Images in python using tkinter

Can't make this image to display. I get an error saying TclError: image "pyimage21" doesn't exist
Here is what I tried
import tkinter as tk
root = tk.Tk()
root.geometry("500x500")
root.title("Poker ranges")
photo = tk.PhotoImage(file = "C:\\Users\\chaeh\\OneDrive\\Desktop\\poker\\hand_range_grid.png")
text = tk.Label(root,image = photo)
root.mainloop()
You need to pack, grid or place your label...
I suggest to use pathlib to find correct path, because tkinter need relative references to use images...
import pathlib
import tkinter as tk
DIR = pathlib.Path('.')
root = tk.Tk()
root.geometry("500x500")
root.title("Poker ranges")
photo = tk.PhotoImage(file=DIR / "abc.png")
text = tk.Label(root, image=photo)
text.pack()
root.mainloop()

Why can't I display an Image in a tkinter Toplevel() window?

I'm trying to create a python program tkinter that, upon the pressing of a button, opens a new full screen tkinter window containing an image and plays an audio file - here's my code:
from tkinter import *
from PIL import Image, ImageTk
from playsound import playsound
def play():
window = Toplevel()
window.attributes('-fullscreen', True)
img = ImageTk.PhotoImage(Image.open("pic.png"))
label = Label(window, image=img).pack()
playsound("song.mp3")
buttonWindow = Tk()
b = Button(buttonWindow, text="Press Button", command=play)
b.pack()
(my image and audio file are both on the desktop with my python file)
However, when I run my code, when I press the button, the audio plays but no second tkinter window opens.
I've tried to destroy() the buttonWindow and have tried many different ways of including an image on a tkinter window - if I remove the line of code using PhotoImage(), the window appears (obviously I then get a syntax error stating that 'img' is not defined).
How could I solve this?
Thanks,
Louis
I had similar problem.
But i resolve this trial and error method.
First of all i don't use pillow library and put image into Label
You should try define Photoimage object NOT IN FUNCTION
Example:
It will work:
import tkinter as tk
root = tk.Tk()
root.geometry("600x400")
def popUp():
popff = tk.Toplevel(root)
popff.geometry("400x200")
labelImgff = tk.Label(popff, image=imgff, bg="white")
labelImgff.grid(row=0, column=0)
imgff = tk.PhotoImage(file="/home/user/image.png")
btnff = tk.Button(text="startPOP", command=popUp)
btnff.grid(column=0,row=0)
root.mainloop()
effect of action
Your playsound() command is blocking execution. The playsound() command has an optional field 'block', which is True by default. Changing this to False will continue execution and allow mainloop() to continue.
Second, just call label.draw() to draw your image to the TopLevel window.
Here's the code:
from tkinter import *
from PIL import Image, ImageTk
from playsound import playsound
def play():
window = Toplevel()
window.attributes('-fullscreen', True)
img = ImageTk.PhotoImage(Image.open("pic.jpeg"))
label = Label(window, image=img).pack()
playsound("song.mp3",block=False)
label.draw()
buttonWindow = Tk()
b = Button(buttonWindow, text="Press Button", command=play)
b.pack()
buttonWindow.mainloop()
Cheers!

My code doen't work inside a function, python3/tkinter

I'm using python3 and tkinter, the first code works but the second after I put it into a function it no longer shows the image and I want to know why
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("test")
canv = Canvas(root,width=500, height=500)
canv.pack()
image = ImageTk.PhotoImage(Image.open("C:/Users/tomas/Desktop/carros/carro2.jpg"))
canv.create_image(0,0,anchor="nw",image=image)
root.mainloop()
second code:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("test")
def main():
canv = Canvas(root,width=500, height=500)
canv.pack()
image = ImageTk.PhotoImage(Image.open("C:/Users/tomas/Desktop/carros/carro2.jpg"))
canv.create_image(0,0,anchor="nw",image=image)
main()
root.mainloop()
In order to properly display ImageTk.PhotoImage images, you need to keep a persistent reference to each of them (see this answer and the documentation).
When your code leaves the main() function, your image object is garbage-collected. One easy way to fix this is to store the image reference in the canvas object, so that it will not be garbage-collected as long as your canvas exists:
...
image = ImageTk.PhotoImage(Image.open("C:/Users/tomas/Desktop/carros/carro2.jpg"))
canv.img = image
...

Unable to display PIL Image inside Canvas

I'm trying to display a PIL.Image inside a tkinter.Canvas:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.geometry('500x400')
canvas = Canvas(root, width=500, height=400)
canvas.pack()
pilImage = Image.open("pillow.png")
image = ImageTk.PhotoImage(pilImage)
imagesprite = canvas.create_image(400,400,image=image)
root.mainloop()
But when I run this code only a void window appears, without the image.
How can I solve this problem?
I'm using Python 3.4.1 on OS X 10.9

Categories

Resources