我在customtkinter中有一些按钮,当我改变位置时它们没有移动,我将如何实现我的最终结果?

问题描述 投票:0回答: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 = ["Alvaro jesus mota reto","hernado abada treiton","Jualian gabriel rendon maraya"]    # 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,font=("Roboto",30))
    #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="")

def reset_callback():
    chosen_names.clear()
    label.configure(text="")

app = customtkinter.CTk()

image = PIL.Image.open("imagen.png")
background_image = customtkinter.CTkImage(image, size=(1280, 800))

app.title("app")
app.iconbitmap('isologo.ico')
app.geometry("1280x800")


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.grid(row=0, column=0, padx=20, pady=20)

button = customtkinter.CTkButton(app, text=" ", command=button_callback, hover_color='#8DBCBB',fg_color = '#F8E5BE')
button.grid(row=1,column=0,ipadx=40, ipady=30, padx=20, pady=20)

reset_button = customtkinter.CTkButton(app, text="Reiniciar", command=reset_callback,hover_color='#8DBCBB',fg_color = '#17223B')
reset_button.configure(width=5)
reset_button.grid(row=0, column=9,padx=0,pady=0)

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

我得到一个看起来像这样的窗口,但按钮和标签不应该在那里

我希望界面看起来像这样,但我一直无法实现

最上面的文字是随机生成的名字; reiciar 按钮是深蓝色的按钮,没有文本的按钮是黄色按钮。如果有人知道如何做到这一点,因为我无法做到这一点,我的按钮也会根据顶部显示的文本大小而跳跃。

python button customtkinter
1个回答
0
投票

这里是代码的更新版本(我已将

sticky=
参数添加到小部件和
grid_rowconfigure
/
grid_columnconfigure
以在应用程序调整大小时更新尺寸:

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 = [
        "Alvaro jesus mota reto",
        "hernado abada treiton",
        "Jualian gabriel rendon maraya",
    ]  # 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, font=("Roboto", 30))
    # 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="")


def reset_callback():
    chosen_names.clear()
    label.configure(text="")


app = customtkinter.CTk()

image = PIL.Image.open("python.png")

app.title("app")
# app.iconbitmap("isologo.ico")
app.geometry("1280x800")


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="")
bg_lbl.place(x=0, y=0)

# Create a label
label = customtkinter.CTkLabel(app, text="")
label.grid(row=0, column=0, padx=20, pady=20, sticky="n")

button = customtkinter.CTkButton(
    app, text=" ", command=button_callback, hover_color="#8DBCBB", fg_color="#F8E5BE"
)
button.grid(row=1, column=0, ipadx=40, ipady=30, padx=20, pady=20)

reset_button = customtkinter.CTkButton(
    app,
    text="Reiniciar",
    command=reset_callback,
    hover_color="#8DBCBB",
    fg_color="#17223B",
)
reset_button.configure(width=5)
reset_button.grid(row=0, column=1, pady=20, padx=20, sticky="ne")

app.grid_rowconfigure(0, weight=1)
app.grid_columnconfigure(0, weight=1)

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

创建此窗口:

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