我正在尝试使用 customtkinter 制作一个控制面板 GUI,它本质上由具有不同按钮的不同页面组成,但是当我转到不同页面时,按钮不居中。
但是,当我导航到 音乐播放器 时,按钮由于某种原因向左对齐:
当按下 音乐控制 按钮时,我删除所有内容使用
for widget in self.winfo_children():
widget.destroy()
主屏幕小部件,然后使用
self.grid_columnconfigure((0, 2), weight=1)
配置列。但是,
音乐播放器
屏幕上的按钮似乎是向左对齐的,尽管
grid_columnconfigure
已设置。
此处是代码:
import customtkinter
from PIL import Image
from time import strftime
customtkinter.set_default_color_theme("dark-theme.json")
playerOpen = False
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.title("Cai's Bar Controls")
self.geometry("1024x600")
self.home_screen()
def home_screen(self):
for widget in self.winfo_children():
widget.destroy()
self.grid_columnconfigure((0, 3), weight=1)
global playerOpen
playerOpen = False
self.bullseye = customtkinter.CTkImage(light_image=Image.open("images/bullseye.png"),size=(100, 100))
self.lightbulb = customtkinter.CTkImage(light_image=Image.open("images/lightbulb.png"),size=(100, 100))
self.microphone = customtkinter.CTkImage(light_image=Image.open("images/cog.png"),size=(100, 100))
self.play = customtkinter.CTkImage(light_image=Image.open("images/play.png"),size=(100, 100))
self.nexttrack = customtkinter.CTkImage(light_image=Image.open("images/next.png"),size=(100, 100))
self.previoustrack = customtkinter.CTkImage(light_image=Image.open("images/previous.png"),size=(100, 100))
self.house = customtkinter.CTkImage(light_image=Image.open("images/home.png"),size=(100, 100))
self.power = customtkinter.CTkImage(light_image=Image.open("images/power.png"),size=(100, 100))
self.quit = customtkinter.CTkImage(light_image=Image.open("images/quit.png"),size=(100, 100))
self.playlist = customtkinter.CTkImage(light_image=Image.open("images/playlist.png"),size=(100, 100))
self.singalongindiehits = customtkinter.CTkImage(light_image=Image.open("images/singalongindiehits.jpg"),size=(200, 200))
self.the80shits = customtkinter.CTkImage(light_image=Image.open("images/80shits.jpg"),size=(200, 200))
self.massivedancehits = customtkinter.CTkImage(light_image=Image.open("images/massivedancehits.jpg"),size=(200, 200))
self.label = customtkinter.CTkLabel(self, text="Cai's Bar Controls", fg_color="transparent", font=("Roboto", 75, 'bold'))
self.label.grid(row=0, column=0, padx=20, pady=20, columnspan=4)
self.clock = customtkinter.CTkLabel(self, text="Time", fg_color="transparent", font=("Roboto", 150))
self.clock.grid(row=1, column=0, padx=20, pady=20, columnspan=4)
self.music_controls = customtkinter.CTkButton(self, text="Music Controls", command=self.open_music_controls, image=self.play, width=200, height=200, compound='top')
self.music_controls.grid(row=2, column=0, padx=20, pady=20)
self.dart_counter = customtkinter.CTkButton(self, text="Dart Counter", command=self.open_dart_counter, image=self.bullseye, width=200, height=200, compound='top')
self.dart_counter.grid(row=2, column=1, padx=20, pady=20)
self.karaoke = customtkinter.CTkButton(self, text="Settings", command=self.open_karaoke, image=self.microphone, width=200, height=200, compound='top')
self.karaoke.grid(row=2, column=2, padx=20, pady=20)
self.light_controls = customtkinter.CTkButton(self, text="Light Controls", command=self.open_light_controls, image=self.lightbulb, width=200, height=200, compound='top')
self.light_controls.grid(row=2, column=3, padx=20, pady=20)
self.time()
def time(self):
string = strftime('%#I:%M %p')
self.clock.configure(text=string)
self.clock.after(1000, self.time)
def open_music_controls(self):
for widget in self.winfo_children():
widget.destroy()
self.grid_columnconfigure((0, 2), weight=1)
self.label = customtkinter.CTkLabel(self, text="Music Controls", fg_color="transparent", font=("Arial", 75))
self.label.grid(row=0, column=0, padx=20, pady=20, columnspan=3)
self.previous = customtkinter.CTkButton(self, text="", command=self.previous_track, image=self.previoustrack, width=200, height=200, compound='top', font=("Arial", 25))
self.previous.grid(row=1, column=0, padx=20, pady=20)
self.pause = customtkinter.CTkButton(self, text="", command=self.play_pause, image=self.play, width=200, height=200, compound='top', font=("Arial", 25))
self.pause.grid(row=1, column=1, padx=20, pady=20)
self.skip = customtkinter.CTkButton(self, text="", command=self.next_track, image=self.nexttrack, width=200, height=200, compound='top', font=("Arial", 25))
self.skip.grid(row=1, column=2, padx=20, pady=20)
self.home = customtkinter.CTkButton(self, text="", command=self.home_screen, image=self.house, width=50, height=50, compound='top', font=("Arial", 25))
self.home.grid(row=3, column=1, padx=20, pady=20)
self.library = customtkinter.CTkButton(self, text="", command=self.open_library, image=self.playlist, width=50, height=50, compound='top', font=("Arial", 25))
self.library.grid(row=3, column=2, padx=20, pady=20)
self.nowplaying = customtkinter.CTkLabel(self, text="Loading track...", fg_color="transparent", font=("Arial", 40))
self.nowplaying.grid(row=2, column=0, padx=20, pady=20, columnspan=3)
global playerOpen
playerOpen = True
问题在于你在
open_music_controls
函数中设置
grid_columnconfigure
的方式。你只配置了 0 和 2 列的权重,而没有配置 1 列的权重。这会导致 1 列不扩展,从而使按钮向左对齐。
要解决此问题,请将以下代码行:
self.grid_columnconfigure((0, 2), weight=1)
替换为:
self.grid_columnconfigure((0, 1, 2), weight=1)
这将为所有三列(0、1 和 2)设置相同的权重,从而使它们均匀扩展并使按钮居中。
以下是修改后的
open_music_controls
函数:
def open_music_controls(self):
for widget in self.winfo_children():
widget.destroy()
# 为所有三列设置权重
self.grid_columnconfigure((0, 1, 2), weight=1)
self.label = customtkinter.CTkLabel(self, text="Music Controls", fg_color="transparent", font=("Arial", 75))
self.label.grid(row=0, column=0, padx=20, pady=20, columnspan=3)
self.previous = customtkinter.CTkButton(self, text="", command=self.previous_track, image=self.previoustrack, width=200, height=200, compound='top', font=("Arial", 25))
self.previous.grid(row=1, column=0, padx=20, pady=20)
self.pause = customtkinter.CTkButton(self, text="", command=self.play_pause, image=self.play, width=200, height=200, compound='top', font=("Arial", 25))
self.pause.grid(row=1, column=1, padx=20, pady=20)
self.skip = customtkinter.CTkButton(self, text="", command=self.next_track, image=self.nexttrack, width=200, height=200, compound='top', font=("Arial", 25))
self.skip.grid(row=1, column=2, padx=20, pady=20)
self.home = customtkinter.CTkButton(self, text="", command=self.home_screen, image=self.house, width=50, height=50, compound='top', font=("Arial", 25))
self.home.grid(row=3, column=1, padx=20, pady=20)
self.library = customtkinter.CTkButton(self, text="", command=self.open_library, image=self.playlist, width=50, height=50, compound='top', font=("Arial", 25))
self.library.grid(row=3, column=2, padx=20, pady=20)
self.nowplaying = customtkinter.CTkLabel(self, text="Loading track...", fg_color="transparent", font=("Arial", 40))
self.nowplaying.grid(row=2, column=0, padx=20, pady=20, columnspan=3)
global playerOpen
playerOpen = True
进行此更改后,你的音乐控制按钮应该会正确居中。
标签:python,tkinter,customtkinter From: 78780896