如何在customtkinter中设置背景图片?

问题描述 投票:0回答:1

我想在 custokinter UI 中设置一个 png 作为背景图像我有这个代码

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

我该如何设置

image = PIL.Image.open("Imagen.png")
作为背景?背景可以是静态的,不必改变大小,但如果它有点响应式,那就更好了。

python user-interface background customtkinter
1个回答
1
投票

您可以使用此示例如何设置自定义背景图像+动态调整大小:

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

使用“Python 背景”创建此窗口:

© www.soinside.com 2019 - 2024. All rights reserved.