How would I set a background Image in customtkinter?

1k views Asked by At

I want to set a png as the background image in a custokinter UI I have this code

import customtkinter
import random
from PIL import Image
import PIL

customtkinter.set_appearance_mode("light")

# Create a list to track the names that have already been chosen
chosen_names = []

def button_callback():
    # Create a list of names
    names = ["Alice", "Bob", "Carol", "Dave", "Eve"]

    # Randomly select a name from the list
    name = random.choice(names)

    # Check if the name has already been chosen
    while name in chosen_names:
        name = random.choice(names)

    # Add the name to the list of chosen names
    chosen_names.append(name)

    # Get the label
    #label = app.winfo_children()[0]
    # Update the label text
    label.configure(text=name)
    label.grid_remove()

    # Check if all the values in the list have been selected
    if len(chosen_names) == len(names):
        chosen_names.clear()
        label.configure(text="")

app = customtkinter.CTk()
image = PIL.Image.open("Imagen.png")
background_image = customtkinter.CTkImage(image)

app.title("app")
app.iconbitmap('isologo.ico')
app.geometry("500x500")
# Create a label
label = customtkinter.CTkLabel(app)
label.pack(padx=0, pady=0)
label.configure(text="")

button = customtkinter.CTkButton(app, text="Selector Nombre", command=button_callback)
button.pack(ipadx=20, ipady=20,padx=20, pady=50)
app.mainloop()

how would i set image = PIL.Image.open("Imagen.png") as the background? The background can be static and doesn't have to change size, but if it is a bit responsive, it will be much better.

1

There are 1 answers

0
Andrej Kesely On BEST ANSWER

You can use this example how to set custom bg image + dynamic resizing:

import random

import customtkinter
import PIL
from PIL import Image

customtkinter.set_appearance_mode("light")

# Create a list to track the names that have already been chosen
chosen_names = []


def button_callback():
    # Create a list of names
    names = ["Alice", "Bob", "Carol", "Dave", "Eve"]

    # Randomly select a name from the list
    name = random.choice(names)

    # Check if the name has already been chosen
    while name in chosen_names:
        name = random.choice(names)

    # Add the name to the list of chosen names
    chosen_names.append(name)

    # Get the label
    # label = app.winfo_children()[0]
    # Update the label text
    label.configure(text=name)
    label.grid_remove()

    # Check if all the values in the list have been selected
    if len(chosen_names) == len(names):
        chosen_names.clear()
        label.configure(text="")


app = customtkinter.CTk()

image = PIL.Image.open("python.png")
background_image = customtkinter.CTkImage(image, size=(500, 500))

app.title("app")
app.geometry("500x500")


def bg_resizer(e):
    if e.widget is app:
        i = customtkinter.CTkImage(image, size=(e.width, e.height))
        bg_lbl.configure(text="", image=i)


# Create a bg label
bg_lbl = customtkinter.CTkLabel(app, text="", image=background_image)
bg_lbl.place(x=0, y=0)

# Create a label
label = customtkinter.CTkLabel(app, text="")
label.pack(padx=20, pady=20)

button = customtkinter.CTkButton(app, text="Selector Nombre", command=button_callback)
button.pack(ipadx=20, ipady=20, padx=20, pady=50)

app.bind("<Configure>", bg_resizer)
app.mainloop()

Creates this window with "Python background":

enter image description here