How to create a tkinter background image? - python

I need to draw a ball moving on a canvas with a background (floor map) image.
I succeded to load the image, and to draw the ball moving, but the canvas ball is not placed on top of the background.
import Tkinter as tk
import random
import time
from PIL import ImageTk, Image
root = tk.Tk()
root.resizable(width=False, height=False)
root.wm_attributes("-topmost", 1)
path = 'C:\xx\Pictures\xxx.jpg'
img = Image.open(path)
photo = ImageTk.PhotoImage(img)
class Ball:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 100)
def draw(self):
self.canvas.move(self.id, 1, 1)
self.canvas.after(50, self.draw)
canvas = tk.Canvas(root, bd=0, highlightthickness=0)
canvas.pack()
background_label = tk.Label(root, image = photo)
background_label.place(x=0, y=0, relwidth=1.0, relheight=1.0, anchor="center")
background_label.pack( )
ball = Ball(canvas, "red")
ball.draw() #Changed per Bryan Oakley's comment.
root.mainloop()

You load the image in a label that's placed below the canvas you draw the ball in. You have to load the image in the same canvas.
Replace this
background_label = tk.Label(root, image = photo)
background_label.place(x=0, y=0, relwidth=1.0, relheight=1.0, anchor="center")
background_label.pack( )
with this:
canvas.create_image(0, 0, image=photo)
Make sure to create the image before you create the ball to get the z-ordering right.

Related

How can I hide the image in Tkinter Python

I want to hide the image in Tkinter Python module. How can I do it?
For example:
from tkinter import *
root = Tk()
root.title("Example")
root.geometry("500x500")
root.resizable(0, 0)
def hide():
# How can I hide the image?
canvas = Canvas(root, width=500, height=500)
canvas.pack()
image = PhotoImage(file='example.png')
img = canvas.create_image(0, 0, anchor=NW, image=image)
hide = Button(root, text="Hide", command=hide)
hide = canvas.create_window(0, 30, anchor=NW, window=hide)
root.mainloop()
I try the [image].destory(), but it's not working.
If you want to remove a canvas object from a canvas, you can use the delete method of the canvas. It takes as a parameter the id of the object to be deleted:
canvas.delete(img)

TKinter background resizer + buttons/labels

I'm trying to setup tkinter gui screen with wallpaper on the background when the bg image fit the resize of the screen, i have took code from here..but the problem is when i'm trying to put buttons on it or another labels, the wallpaper still stay on the top and i can't see them.
Here the code:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("Title")
root.geometry("800x600")
class Example(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.configure(background="black")
self.grid(sticky=N+S+E+W)
self.image = Image.open("bg.jpg")
self.img_copy= self.image.copy()
self.background_image = ImageTk.PhotoImage(self.image)
self.background = Label(self, image=self.background_image)
self.background.grid(row =0, column =0,sticky="nsew")
self.background.grid_rowconfigure(0, weight=1)
self.background.grid_columnconfigure(0, weight=1)
self.master = master
self.master.bind('<Configure>', self._resize_image)
# create a button
self.button= Button(root, text="EXIT", width=4).grid(row=3, column=1, sticky=N+S+E+W)
def _resize_image(self,event):
new_width = self.master.winfo_width()
new_height = self.master.winfo_height()
self.image = self.img_copy.resize((new_width, new_height))
self.background_image = ImageTk.PhotoImage(self.image)
self.background.configure(image = self.background_image)
e = Example(root)
e.grid(row =0, column =0,sticky="nsew")
e.grid_rowconfigure(0, weight=1)
e.grid_columnconfigure(0, weight=1)
root.mainloop()
You need to use place() to put the background image so that it does not interfere other widgets in the same parent.
Below is a simplified example based on your code:
from tkinter import *
from PIL import Image, ImageTk
class Example(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.configure(background="black")
self.image = Image.open("bg.jpg")
# label for the background image
self.background = Label(self)
self.background.place(x=0, y=0)
self.bind('<Configure>', self._resize_image)
# create a button
self.button = Button(self, text="EXIT", width=4)
self.button.grid(row=3, column=1, sticky=N+S+E+W, padx=50, pady=50)
def _resize_image(self,event):
if event.widget is self:
# resize background image to fit the frame size
image = self.image.resize((event.width, event.height))
self.background_image = ImageTk.PhotoImage(image)
self.background.configure(image=self.background_image)
root = Tk()
root.title("Title")
root.geometry("800x600")
e = Example(root)
e.pack(fill=BOTH, expand=1)
root.mainloop()

How to resize image using canvas frame in python

Here I am trying to display images using the canvas frame but it is not showing anything.
what is wrong in this.
when I replace the frame with root in Label widget it is working.
Thanks
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
import tkinter as tk
root = Tk()
root.title("Title")
root.geometry('600x600')
def resize_image(event):
new_width = event.width
new_height = event.height
image = copy_of_image.resize((new_width, new_height))
photo = ImageTk.PhotoImage(image)
label.config(image = photo)
label.image = photo
def on_configure(event):
canvas.configure(scrollregion=canvas.bbox('all'))
scrollbary = Scrollbar(root, orient = tk.VERTICAL)
scrollbarx = Scrollbar(root,orient = tk.HORIZONTAL)
canvas = tk.Canvas(root,yscrollcommand = scrollbary.set,
xscrollcommand = scrollbarx.set)
scrollbary.config(command=canvas.yview)
scrollbarx.config(command=canvas.xview)
scrollbary.pack(side=tk.RIGHT, fill='y',expand = tk.FALSE)
scrollbarx.pack(side=tk.BOTTOM,fill = 'x',expand = tk.FALSE)
canvas.pack(side=tk.LEFT, padx=5, pady=5,
fill=tk.BOTH, expand=tk.TRUE)
canvas.bind('<Configure>', on_configure)
frame = tk.Frame(canvas)
canvas.create_window((0,0), window=frame, anchor='nw')
image = Image.open('logo1.png')
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = ttk.Label(frame, image = photo)
label.bind('<Configure>', resize_image)
label.pack(fill=BOTH, expand = YES)
root.mainloop()

How do I make the background in a tkinter window transparent?

How do I make this so the window background is transparent, but the image remains visible?
Here is my code;
from tkinter import Tk, Label, Button, Canvas
from PIL import ImageTk, Image
class MyFirstGUI:
def __init__(self, master):
self.canvas = Canvas(master, width = 300, height = 300)
self.canvas.pack()
self.image = ImageTk.PhotoImage(Image.open("Sprites/ph1.png"))
self.canvas.create_image(150, 150, image=self.image)
self.master = master
master.title("A simple GUI")
self.close_button = Button(master, text="Close", command=master.quit)
self.close_button.pack()
def greet(self):
print("Greetings!")
root = Tk()
root.geometry("+1630+775")
root.overrideredirect(1)
my_gui = MyFirstGUI(root)
root.mainloop()

Draw line on image in tkinter

I'm trying to make a script that will draw lines on an image in a python GUI. I've been able to get the image on the GUI, but do not know how to draw the additional lines. The script should be able to loop so I can draw more lines.
What I have so far:
import tkinter as Tk
root = Tk.Tk()
background_image=Tk.PhotoImage(file="map.png")
background_label = Tk.Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
root.wm_geometry("794x370")
root.title('Map')
root.mainloop()
You can do that by first placing your image on a canvas:
import tkinter as Tk
root = Tk.Tk()
canvas = Tk.Canvas(root)
background_image=Tk.PhotoImage(file="map.png")
canvas.pack(fill=Tk.BOTH, expand=1) # Stretch canvas to root window size.
image = canvas.create_image(0, 0, anchor=Tk.NW, image=background_image)
line = canvas.create_line(10, 10, 100, 35, fill="red")
root.wm_geometry("794x370")
root.title('Map')
root.mainloop()

Categories