How do i place a background image on tab window in tkinter - python

i wanted to know how to place a background image on Tkinter tab window i usually get an error.please help

Main Window:
from tkinter import *
gui = Tk()
gui.geometry('100x100')
C = Canvas(gui, bg="blue", height=250, width=300)
filename = PhotoImage(file='example.png')
filename = filename.zoom(7)
filename = filename.subsample(18)
background_label = Label(gui, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
gui.mainloop
Or
New Tab Window:
from tkinter import *
gui = Tk()
gui.geometry('100x100')
def Tab():
gui = Toplevel()
gui.geometry('100x100')
C = Canvas(gui, bg="blue", height=250, width=300)
filename = PhotoImage(file='example.png')
filename = filename.zoom(7)
filename = filename.subsample(18)
background_label = Label(gui, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
Tab()
gui.mainloop()
You Also Need To Have Your Image In The Defined Directory.
Hope It Helps & Welcome To StackOverflow!

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.TclError: couldn't recognize data in image file PLEASE Solve For Windows 10

I have tried using a formatted code that already existed where I imported 'PIL' from 'ImageTk' however, that has not worked. The image and script is in the same file directory yet the image data is not recognizable. I changed the string to 'raw string' by adding the 'r' in front of the file path and I even tried using a different image and it still outputs the same error. This line linebackground_image = tk.PhotoImage(file=r'C:\Users\kevan\OneDrive\Desktop\Programming for me\Python\Tkinker\bgfortest.png') of the code is the source of error.
Your help would be appreciated
import tkinter as tk
HEIGHT = 700
WIDTH = 800
root = tk.Tk()
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
background_image = tk.PhotoImage(file=r'C:\Users\kevan\OneDrive\Desktop\Programming for me\Python\Tkinker\bgfortest.png')
background_label = tk.Label(root, image=background_image)
background_label.place(x=0, y=0, relheight=1, relwidth=1)
frame = tk.Frame(root, bg='#80c1ff', bd=5)
frame.place(relx=0.5, rely=0.1, relheight=0.1, relwidth=0.75, anchor='n')
button = tk.Button(frame, text="Hello", font=40)
button.place(relx=0.7, relheight=1, relwidth=0.3)
entry = tk.Entry(frame, bg='white', font=40)
entry.place(relheight=1, relwidth=0.65)
lower_frame = tk.Frame(root, bg='#80c1ff', bd=10)
lower_frame.place(relx=0.5, rely=0.25, relheight=0.6, relwidth=0.75, anchor='n')
label = tk.Label(lower_frame, text="This is a python GUI.", bg='cadet blue', fg="Black")
label.place(relwidth=1, relheight=1)
root.mainloop()

Tkinter Image not showing up

I'm quite new to coding and I'm trying to have an image show up in a tkinter window.
However, when I run the code below there is the gap where the image should be. I am getting no error from this code as well.
window2 = Toplevel()
window2.geometry("1920x1200")
Namearea = Label(window2, text="Please Name the Prebuild:")
Namearea.pack()
e = Entry(window2, width=50, borderwidth=3, bg="Light Grey", fg="black")
e.pack()
#Here is the part that is not working.
img3 = PhotoImage(file=r"C:\\Tkinter\\ComputerImage.png")
picture1 = Label(window2, image=img3)
picture1.pack()
SaveAndContinue = Button(window2, text="Save and Return to Main Menu", padx=75, pady=20, bg="Light Grey")
SaveAndContinue.pack()
Try the answer from josav09 for this question:
How to add an image in Tkinter?
from tkinter import *
from PIL import ImageTk, Image
import os
root = Tk()
img = ImageTk.PhotoImage(Image.open("True1.gif"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()

Why does Tkinter make a new window for my button?

Im trying to add a button into my window, but it makes a new window for the button, how can I add it to the same window where I have a background on?
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import messagebox
from PIL import ImageTk
top = Tk()
C = Canvas(top, bg ="blue", height=1920, width=1080)
filename = ImageTk.PhotoImage(file = "C:/Users/plapl/Desktop/ching.jpg")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
root = Tk()
myButton = Button(root, text = "Add a task")
myButton.pack()
root.mainloop()
It's because you have created a new instance for Tk().
change it to this:
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import messagebox
from PIL import ImageTk
top = Tk()
C = Canvas(top, bg ="blue", height=1920, width=1080)
filename = ImageTk.PhotoImage(file = "C:/Users/plapl/Desktop/ching.jpg")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
myButton = Button(top, text = "Add a task")
myButton.pack()
root.mainloop()
This happens because you have 2 Tk() objects, which means that 2 windows are created. To solve this, change the line: background_label = Label(top, image=filename) to this: background_label = Label(root, image=filename).
Then, you can get rid of this line: top = Tk() because it isn't needed anymore.

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()

Categories