CustomTkinter按钮无法固定位置求助:如何实现目标布局
问题
开发CustomTkinter随机选择器时,遇到两个布局问题:
- 按钮和标签无法定位到预期位置
- 标签文本更新时,控件会随意偏移
期望实现的布局:
- 顶部居中显示随机生成的名称
- 右上角放置深蓝色重置按钮
- 底部居中放置黄色触发按钮
现有代码
import random import customtkinter import PIL from PIL import Image customtkinter.set_appearance_mode("light") # 记录已选中的名字 chosen_names = [] def button_callback(): names = ["Alvaro jesus mota reto","hernado abada treiton","Jualian gabriel rendon maraya"] name = random.choice(names) # 避免重复选中 while name in chosen_names: name = random.choice(names) chosen_names.append(name) label.configure(text=name, font=("Roboto",30)) # 所有名字选完后清空 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.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) # 背景标签 bg_lbl = customtkinter.CTkLabel(app, text="", image=background_image) bg_lbl.place(x=0, y=0) # 结果标签 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()
修复后的代码及说明
问题根源在于原代码使用grid布局时未配置行列权重,且控件未设置合理的sticky和columnspan属性,导致位置失控。以下是修复后的代码:
import random import customtkinter import PIL from PIL import Image customtkinter.set_appearance_mode("light") chosen_names = [] def button_callback(): names = ["Alvaro jesus mota reto","hernado abada treiton","Jualian gabriel rendon maraya"] name = random.choice(names) while name in chosen_names: name = random.choice(names) chosen_names.append(name) label.configure(text=name, font=("Roboto",30)) 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.iconbitmap('isologo.ico') app.geometry("1280x800") # 配置行列权重,让布局自适应窗口大小 for col in range(10): app.grid_columnconfigure(col, weight=1) app.grid_rowconfigure(0, weight=3) # 顶部显示区域占比更大 app.grid_rowconfigure(1, weight=1) # 底部按钮区域 def bg_resizer(e): if e.widget is app: i = customtkinter.CTkImage(image, size=(e.width, e.height)) bg_lbl.configure(text="", image=i) # 背景标签设置为铺满整个窗口 bg_lbl = customtkinter.CTkLabel(app, text="", image=background_image) bg_lbl.place(x=0, y=0, relwidth=1, relheight=1) # 结果标签:顶部居中,跨所有列 label = customtkinter.CTkLabel(app, text="", anchor="n") label.grid(row=0, column=0, columnspan=10, pady=(50, 0), sticky="n") # 触发按钮:底部居中,跨所有列 button = customtkinter.CTkButton(app, text=" ", command=button_callback, hover_color='#8DBCBB',fg_color = '#F8E5BE') button.grid(row=1, column=0, columnspan=10, ipadx=40, ipady=30, pady=(0, 50), sticky="s") # 重置按钮:右上角固定位置 reset_button = customtkinter.CTkButton(app, text="Reiniciar", command=reset_callback,hover_color='#8DBCBB',fg_color = '#17223B') reset_button.grid(row=0, column=9, padx=20, pady=20, sticky="ne") app.bind("<Configure>", bg_resizer) app.mainloop()
关键修改点:
- 给所有列设置
weight=1,让列宽自适应窗口;给行设置不同权重,区分顶部显示区和底部按钮区 - 结果标签和触发按钮添加
columnspan=10,使其横跨所有列,配合sticky实现居中 - 重置按钮用
sticky="ne"固定在右上角 - 背景标签添加
relwidth=1, relheight=1,确保始终铺满窗口 - 给标签和按钮设置合理的
pady,控制上下间距,避免文本变化时偏移
内容的提问来源于stack exchange,提问作者Pandas INC
相关产品推荐
相关产品推荐

