image doesn't show up on canvas output - python

I am trying to simply get some image on canvas, but even though I don't get any errors, my canvas stays white, no picture shows up. I know it will be some stupid misteak, but I can't find it.
def obrazek():
mesic = tkinter.PhotoImage(file="moon-0.gif")
canvas.create_image(700, 500, image = mesic)
rodic = tkinter.Tk()
rodic.title(u"Slunce a Měsíc")
rodic.geometry("+1000+300")
canvas = tkinter.Canvas(rodic, width=1400, height=800,)
canvas.pack()
obrazek()
tkinter.mainloop()
Output is whitescreen.

Keep a reference to the image by returning from the function
Then call pack() after the function obrazek

Related

tkinter create_image don't print

for a personnal project I've been playing with pyvirtualcam,
and wanted a nice feedback with interactions, so I started implementing some tkinter in it. But as I tried to display the image on the canvas, I faced a wall...
My problem is that when printing that "image" which I forced to be a black square, it do not display at all (I am convinced about this because my canvas background is red). Any idea where this can come from?
Here is the code to reproduce the problem I have:
self.screen = tk.Tk()
self.canvas = tk.Canvas(self.screen, bg = "red")
self.canvas.pack(fill=tk.BOTH, expand=tk.YES, side=tk.TOP)
size = 20, 20, 3
output = np.zeros(size, dtype=np.uint8)
imageRGB = cv2.cvtColor(output, cv2.COLOR_BGRA2RGBA)
imageTk = ImageTk.PhotoImage(image = Image.fromarray(imageRGB))
self.canvas.create_image(100, 100, image = imageTk, anchor=tk.NW)
questions you might ask:
console is empty as if everything was fine (no error)
when printing the "output" or "imageRGB" variables it prints the matrix fine,
when printing the "imageTk" variable is prints a < TkImage > object,
For more info, be free to ask me,
Thanks for your help!

Canvas not getting captured while saving tkinter python

[SOLVED]
Please don't judge the code. I just need to fix one bug with capturing canvas into file.
As you can see I've tried multiple solutions and still while saving file is all-white...
Do you have any soulutions to that?
This app is to generate randomly dots on canvas with option to save it.
it works when number of dots is above 40000 and rectangle is black.
But otherwise is white.
HEIGTH = 207
WIDTH = 207
def snapsaveCanvas():
fname = filename.get()
my_window.update_idletasks()
my_window.update()
canvas.update()
canvas.update_idletasks()
ps = canvas.postscript(colormode='color')
img = Image.open(io.BytesIO(ps.encode('utf-8')))
img.save(fname + ".bmp", 'bmp')
print("done")
canvas = tk.Canvas(frame2, width=HEIGTH, height=WIDTH, background='white')
canvas.grid(row=0, column=0)
canvas.create_rectangle(4, 4, 206, 206)
Ok, thanks for links.
I've tested several methods and one of them works.
taken from How can I convert canvas content to an image?
import win32gui
def snapsaveCanvas():
fileName = filename.get()
canvas.update()
canvas.update_idletasks()
HWND = canvas.winfo_id() # get the handle of the canvas
rect = win32gui.GetWindowRect(HWND) # get the coordinate of the canvas
im = ImageGrab.grab(rect).save(fileName + ".bmp")
print("done")

Tkinter: how to draw an image on a canvas with background

I am trying to use tkingter to make a card game.
set a table image as background of the canvas.
draw cards on top of the table background. This will be triggered by clicking a button (not shown here)
The problem is I can't create card image on top of table background. what's the right way to do it? do i need to create another canvas for card, in stead of directly drawing on table background? I feel there is something fundamental that i am missing here.
from tkinter import *
from PIL import Image, ImageTk
# globals
table_dir = "C:/Python36/resourse/table.png"
card_dir = "C:/Python36/resourse/card.jpg"
#CREATE APP
root = Tk()
#LOAD IMAGES
table_PIL = Image.open(table_dir)
table_image = ImageTk.PhotoImage(table_PIL)
card_PIL = Image.open(card_dir)
card_image = ImageTk.PhotoImage(card_PIL)
#make canvas and set background image with a label
canv = Canvas(root, width=table_PIL.width, height=table_PIL.height, background="white")
background_label = Label(canv, image = table_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
canv.grid(row=0, column=0)
#trying to create card image on top of background table, not working
canv.create_image(200, 100, image=card_image)
root.mainloop()
Don't use a label for the background. Use canv.create_image(...) to create the background, just like you do for creating cards.

how to change labels background as same picture

I started to learn Python one month ago. Sorry if my question is not good, this is my first question here.
I have made a small game using tkinter, but I have a problem.
I made a big label with a picture on it as background. Whenever I make more labels with text on them, the text will have gray background. However what I want is for every text to have the picture that I already placed as background.
Here's some code to explain it:
from tkinter import*
x=Tk()
x.geometry("1000x1000")
z=PhotoImage(file="D:\\Blue.gif")
v=Label(x,text="hi",font=100,fg="red",compound=CENTER,image=z,width=1000,height=1000)
v.place(x=0,y=0)
v1=Label(x,text="OO",font=100,fg="red")
v1.place(x=300,y=400)
x.mainloop()
The v label works very well as long as I use compound with it. It shows the picture with the text "hi" on it.
However I want the v1 label to have the same background as v, instead of gray background.
All widgets have background - they can't be transparent.
You can use tk.Canvas to put text without background on image or transparent image on text.
effbot.org: Canvas, PhotoImage
#!/usr/bin/env python3
import tkinter as tk
from PIL import Image, ImageTk
# --- constants ---
WIDTH = 800
HEIGHT = 600
# --- main ---
root = tk.Tk()
c = tk.Canvas(root, width=WIDTH, height=HEIGHT)
c.pack()
# only GIF and PGM/PPM
#photo = tk.PhotoImage(file='test.gif')
# other formats
image = Image.open('test_transparent.png')
photo = ImageTk.PhotoImage(image)
# use in functions - solution for "garbage collector" problem
c.image = photo
i = c.create_image((WIDTH//2, HEIGHT//2), image=photo)
t = c.create_text((WIDTH//2, HEIGHT//2), text='Hello World')
root.mainloop()
Change order and you get image on text
t = c.create_text((WIDTH//2, HEIGHT//2), text='Hello World')
i = c.create_image((WIDTH//2, HEIGHT//2), image=photo)
test_transparent.png (image with transparent background)

How to make a tkinter Label background transparent?

I have a window with a label as my frame. I did this because i wanted an image in the background. But now im having trouble with the other labels i have used. The other labels i have used to actually labeled things dont have a transparent background. Is there a way to make the background of these labels transparent?
import Tkinter as tk
root = tk.Tk()
root.title('background image')
image1 = Tk.PhotoImage(file='image_name.gif')
# get the image size
w = image1.width()
h = image1.height()
# make the root window the size of the image
root.geometry("%dx%d" % (w, h))
# root has no image argument, so use a label as a panel
panel1 = tk.Label(root, image=image1)
panel1.pack(side='top', fill='both', expand='yes')
# put a button/label on the image panel to test it
label1 = tk.Label(panel1, text='here i am')
label1.pack(side=Top)
button2 = tk.Button(panel1, text='button2')
button2.pack(side='top')
# start the event loop
root.mainloop()
i think it can help, all black will be transparent
root.wm_attributes('-transparentcolor','black')
It is not supported with transparent backgrounds in Tk.
If you are working with images and putting text onto them, the most convenient way is - I think - utilizing Canvas widget.
tkinter Canvas widget has methods as .create_image(x, y, image=image, options) and .create_text(x, y, text="Some text", options).
use this :
from tkinter import *
main=Tk()
photo=PhotoImage(file='test.png')
Label(main,image=photo,bg='grey').pack()
#your other label or button or ...
main.wm_attributes("-transparentcolor", 'grey')
main.mainloop()
this work if use bg='grey' you can change it in line 7
good luck :)

Categories

Resources