Python Tkinter - Photo as background - python

This is for a school project, so i kind off want to understand what I am doing.
I want to add a photo, for example the photo with the name 'teletubbies.jpg' as the background. I have no clue how to do this and how this works, been searching for hours now and dying to find an answer :$
This is the bit of code that i have now:
from tkinter import *
from tkinter.messagebox import showinfo
def clicked():
bericht = 'Test for stackflow!',
showinfo(title='popup', message=bericht)
def clicked1():
bericht = 'Test for stackflow'
showinfo(title='popup', message=bericht)
root = Tk()
label = Label(master=root,
text='This is a test for stackflow',
background='black',
foreground='white',
height = 2
)
label.pack()
button2 = Button(master=root, text='Klik hier voor het beheerportaal', command=clicked, fg='red')
button2.pack(side=BOTTOM,pady=10)
button1 = Button(master=root, text='Klik hier voor het klantportaal', command=clicked1)
button1.pack(side=TOP,pady=10)
entry = Entry(master=root)
entry.pack(padx=10, pady = 10)
root.configure(background='black')
root.mainloop()

If you have a .gif or .pgm/ppm file you could use the Tkinter PhotoImage class to load your image and put it as a background to your label:
backgroundImage = PhotoImage(file = <yourFilePath>)
label = Label(master=root,
image = backgroundImage,
text='This is a test for stackflow',
height = 2
)
label.place(x=0, y=0, relwidth=1, relheight=1)
This will put your image as background in your label.
For the other image formats you can use the Python Image Library.

Related

image dont show on canvas without any error custom tkinter [duplicate]

This question already has answers here:
Tkinter vanishing PhotoImage issue [duplicate]
(1 answer)
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 29 days ago.
hello i try to build a simpel watermark app
i user custom tkinter for user interface ( its same to tkinter )
when i upload a picture with button my function everything work expact picture in canvas i see the canvas but i dont see a picture i test the code with a text on canvas and its show when i add a label with same photo ( i comment that line in this cod) even i dont grid it on window my picture show in canvas this is my cod :
import customtkinter
from PIL import Image, ImageTk
my_font1 = ('times', 18, 'bold')
customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("dark-blue")
window = customtkinter.CTk()
window.title("Water mark Photo")
window.config(padx=50, pady=50)
window.geometry("600x600")
input_pic_label = customtkinter.CTkLabel(master=window,
text="Choose Your Photo",
font=my_font1)
input_pic_label.grid(row=0, column=0, padx=(180, 0), pady=(200, 0))
choose_pic_button = customtkinter.CTkButton(master=window,
text="Upload Photo",
command=lambda: upload_file())
choose_pic_button.grid(row=1, column=0, padx=(180, 0))
def upload_file():
choose_pic_button.destroy()
input_pic_label.destroy()
f_types = [('Jpg Files', '*.jpg'), ('Png Files', '*.png')]
filename = customtkinter.filedialog.askopenfilename(filetypes=f_types)
image = Image.open(filename)
original_image_size = image.size
print(original_image_size)
resized_image = image.resize((500, 500))
resized_image.save("temp.jpg")
new_image = ImageTk.PhotoImage(resized_image)
canvas = customtkinter.CTkCanvas(master=window, width=500, height=500, highlightthickness=0, bg="black")
canvas.grid(row=2, column=1, columnspan=2)
canvas.create_image(250, 250, image=new_image)
window.update()
# label = customtkinter.CTkLabel(master=window, text="", image=new_image)
window.mainloop()
i expect to see a picture but its show nothing i also try to di it in tkinter with the blow cod but output its same expect i get nothing as outpout here without any error:
import tkinter
from PIL import Image, ImageTk
from tkinter import filedialog
my_font1 = ('times', 18, 'bold')
window = tkinter.Tk()
window.title("Water mark Photo")
window.config(padx=50, pady=50)
input_pic_label = tkinter.Label(window,
text="Choose Your Photo",
font=my_font1)
input_pic_label.grid(row=0, column=0, padx=(180, 0))
choose_pic_button = tkinter.Button(window,
text="Upload Photo",
command=lambda: upload_file())
choose_pic_button.grid(row=1, column=0, padx=(180, 0))
canvas = tkinter.Canvas(window, width=500, height=500, highlightthickness=0)
text = canvas.create_text(250, 250, text="Your image")
canvas.grid(row=2, column=1, columnspan=2)
def upload_file():
#choose_pic_button.destroy()
#input_pic_label.destroy()
#canvas.itemconfig(text, text="")
f_types = [('Jpg Files', '*.jpg'), ('Png Files', '*.png')]
filename = tkinter.filedialog.askopenfilename(filetypes=f_types)
image = Image.open(filename)
img = ImageTk.PhotoImage(Image.open(filename))
original_image_size = image.size
print(original_image_size)
resized_image = image.resize((500, 500))
resized_image.save("temp.jpg")
resized_image = ImageTk.PhotoImage(resized_image)
canvas.create_image(250, 250, image=resized_image)
label = tkinter.Label(window, text="helloooooo", image=resized_image)
label.grid(row=2, column=4)
window.mainloop()

How can I manage to put a button image into an image background Tkinter

Good day everyone, can I ask how can I manage to put this button inside an image background
code:
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("Japanese Retro Hub")
root.geometry('2200x1000')
#Image
bg = Image.open("RetroHub_BG_Main.png")
resized = bg.resize((2200,1000), Image.ANTIALIAS)
bg = ImageTk.PhotoImage(resized)
bglabel = Label(root, image=bg)
bglabel.pack(pady=20)
#text for my_command and Start exit button
text = Label(root, text= "Hello world")
text.pack(pady=30)
def my_command():
text.config(text="You have clicked Me...")
def start_exit_button():
click_btn = PhotoImage(file='start_button.png')
img_label= Label(image=click_btn)
button = Button(root, image=click_btn,command=my_command,borderwidth=0)
button.pack(padx=50, pady=20)
root.mainloop()
start_exit_button()
root.mainloop()
This is the image example that I am trying to portray
https://drive.google.com/file/d/1VzbhNazEnfOCk0_QZOD_nWR5jsb-a8GB/view?usp=sharing

How do I make my Label Tkinter element display?

Currently, I am creating a text adventure game. Now to make it a tiny bit better than a lot of them which use the python console, I am using Tkinter to make a GUI. Only it displays my background image for the start screen, but not the text I put there! Please help!
# writes the title as text in the window
titleFont = tkFont.Font(family = "Comic Sans MS", size = 20)
titleText = tkinter.Label(app, text="Gods of This World", font=titleFont)
titleText.pack()
# sets the background image to the games 3 colors (Red, Green, Grey)
C = tkinter.Canvas(app, height=300, width=250)
filename = tkinter.PhotoImage(file = "C:/Users/" + getpass.getuser() + "/Desktop/Gods of This World/net/godsofthisworld/ui/images/backgroundimage.png")
background_label = tkinter.Label(app, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
You put image after text - and you use .place(x=0, y=0, relwidth=1, relheight=1) - so text is hidden behind image.
Put it in different order - first image, next text.
import tkinter as tk
app = tk.Tk()
# first image
photo = tk.PhotoImage(file="image.png")
background_label = tk.Label(app, image=photo)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
# next text
titleText = tk.Label(app, text="Gods of This World")
titleText.pack()
app.mainloop()
BTW: label with text will have gray background - and you can't remove it. If you want text without background then use Canvas and canvas.create_image(), canvas.create_text() - without pack() and place()
import tkinter as tk
WIDTH = 800
HEIGHT = 600
app = tk.Tk()
canvas = tk.Canvas(width=WIDTH, height=HEIGHT)
canvas.pack()
photo = tk.PhotoImage(file="image.png")
canvas.create_image((WIDTH/2, HEIGHT/2), image=photo) #, anchor='center')
canvas.create_text((WIDTH/2, HEIGHT/2), text="Gods of This World") #, anchor='center')
app.mainloop()

How can I set an image as a background in tkinter? [duplicate]

#import statements
from Tkinter import *
import tkMessageBox
import tkFont
from PIL import ImageTk,Image
Code to import image:
app = Tk()
app.title("Welcome")
image2 =Image.open('C:\\Users\\adminp\\Desktop\\titlepage\\front.gif')
image1 = ImageTk.PhotoImage(image2)
w = image1.width()
h = image1.height()
app.geometry('%dx%d+0+0' % (w,h))
#app.configure(background='C:\\Usfront.png')
#app.configure(background = image1)
labelText = StringVar()
labelText.set("Welcome !!!!")
#labelText.fontsize('10')
label1 = Label(app, image=image1, textvariable=labelText,
font=("Times New Roman", 24),
justify=CENTER, height=4, fg="blue")
label1.pack()
app.mainloop()
This code doesn't work. I want to import a background image.
One simple method is to use place to use an image as a background image. This is the type of thing that place is really good at doing.
For example:
background_image=tk.PhotoImage(...)
background_label = tk.Label(parent, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
You can then grid or pack other widgets in the parent as normal. Just make sure you create the background label first so it has a lower stacking order.
Note: if you are doing this inside a function, make sure you keep a reference to the image, otherwise the image will be destroyed by the garbage collector when the function returns. A common technique is to add a reference as an attribute of the label object:
background_label.image = background_image
A simple tkinter code for Python 3 for setting background image .
from tkinter import *
from tkinter import messagebox
top = Tk()
C = Canvas(top, bg="blue", height=250, width=300)
filename = PhotoImage(file = "C:\\Users\\location\\imageName.png")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
top.mainloop
You can use this:
root.configure(background='your colour')
Example:-
import tkinter
root=tkiner.Tk()
root.configure(background='pink')

Background image using Tkinter - Python 2.7

I am trying to display a button over an image.The button appears to be in front of the image.
How can I set the image to the background of the app?
The button also needs to change his location, so the image should be permenant.
My code:
from Tkinter import *
class App:
def __init__(self,master):
frame = Frame(master)
master.geometry("400x200")
frame.pack()
self.hello_b = Button(master,text="ME",command=sys.exit, height=1, width=3,fg= "blue",bg = "green")
self.hello_b.pack()
photo = PhotoImage(file="unnamed.gif")
w = Label (master, image=photo)
w.place(x=0, y=0, relwidth=1, relheight=1)
w.photo = photo
w.pack()
root = Tk()
app = App(root)
root.mainloop()
The answer, which I suspected, is here: "[the code] packs a Label widget in a frame widget, and then places a Button in the upper right corner of the frame. The button will overlap the label." I tested like so
from tkinter import *
class App:
def __init__(self,master):
master.geometry("400x200")
photo = PhotoImage(file="C:/programs/Python34/temimage.png")
self.w = w = Label (master, image=photo)
w.photo = photo
w.pack()
self.hello_b = Button(master,text="ME",command=sys.exit, height=1,
width=3,fg= "white",bg = "green")
self.hello_b.place(x=200, y=5)
root = Tk()
app = App(root)
root.mainloop()
The button is indeed on top of the image. The x,y positions seems to be the upper left corner of the button. Adding
def move():
app.hello_b.place(x=100, y=100)
root.after(1000, move)
caused the button to jump after a second, as expected.

Categories

Resources