我想设置一个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")个 作为背景?背景可以是静态的,不需要改变大小,但如果react 灵敏一点,就会好得多.

推荐答案

您可以使用此示例来设置自定义BG图像+动态调整大小:

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 Backup":

enter image description here

Python相关问答推荐

为什么Pydantic在我申报邮箱时说邮箱丢失

try 使用tensorFlow.keras.models时optree Import错误

除了Python之外,可以替代bare?

Docker-compose:为不同项目创建相同的容器

具有2D功能的Python十六进制图

使用GEKKO在简单DTE系统中进行一致初始化

如何使用Google Gemini API为单个提示生成多个响应?

如何才能知道Python中2列表中的巧合.顺序很重要,但当1个失败时,其余的不应该失败或是0巧合

比较2 PD.数组的令人惊讶的结果

numba jitClass,记录类型为字符串

处理(潜在)不断增长的任务队列的并行/并行方法

对整个 pyramid 进行分组与对 pyramid 列子集进行分组

如果值不存在,列表理解返回列表

pandas滚动和窗口中有效观察的最大数量

从groupby执行计算后创建新的子框架

如何并行化/加速并行numba代码?

启动带有参数的Python NTFS会导致文件路径混乱

将scipy. sparse矩阵直接保存为常规txt文件

Matplotlib中的字体权重

OpenCV轮廓.很难找到给定图像的所需轮廓