首页 > 编程问答 >Customtkinter 网格按钮未居中

Customtkinter 网格按钮未居中

时间:2024-07-23 05:58:11浏览次数:7  
标签:python tkinter customtkinter

我正在尝试使用 customtkinter 制作一个控制面板 GUI,它本质上由具有不同按钮的不同页面组成,但是当我转到不同页面时,按钮不居中。

如您所见,第一页居中: Home page

但是,当我导航到 音乐播放器 时,按钮由于某种原因向左对齐: Music page

当按下 音乐控制 按钮时,我删除所有内容使用

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

相关文章

  • Python:支持索引的内存对象数据库?
    我正在做一些数据整理,如果我可以将一堆字典放入内存数据库中,然后对其运行简单的查询,这会简单得多。例如,类似:people=db([{"name":"Joe","age":16},{"name":"Jane","favourite_color":"red"},])over_16=db.filter(age__g......
  • 如何构建一维数组的二维数组的特定 Python 结构?
    如何构建一维数组(即行向量)的二维数组的特定结构以满足特定我正在维护的遗留程序的结构?我可以在此结构中生成正确的内容all_measurements[:12]array([[0.,0.,0.,2.],[0.02,0.334,0.04,2.24],[0.04,0.668,0.08,2.48],...........
  • 如何使用 Python Flask 将新的咖啡馆(元素)添加到数据库(SQLite)?
    这是我的代码:@app.route("/add",methods=["POST"])defpost_new_cafe():new_cafe=Cafe(name=request.form.get("name"),map_url=request.form.get("map_url"),img_url=request.form.get("img......
  • 使用 tkinter 为 python 创建 GUI 时如何解决语法错误?
    我是一名Python初学者,决定使用tkinter制作一个小型GUI,该GUI接受用户(潜在餐馆)的3个输入,然后单击按钮后随机输出其中一家餐馆。我不断收到语法错误,并认为它与我的buttonfunc或调用它的命令有关。此代码尚未包含在GUI上输出餐厅的任何位置。任何帮助将不胜感激#Pyth......
  • 在 python 中打开 gnome 终端立即显示为僵尸
    作为背景,我正在编写一个脚本来训练多个pytorch模型。我有一个训练脚本,我希望能够在gnome终端中作为子进程运行。这样做的主要原因是我可以随时关注训练进度。如果我可能有多个GPU,我想在单独的窗口中多次运行我的训练脚本。为了实现这一点,我一直在使用popen。以下代码用于打......
  • python threading.Condition 的意外行为
    我正在尝试同步多个线程。我期望使用threading.Condition和threading.Barrier时的脚本输出大致相同,但事实并非如此。请解释一下为什么会发生这种情况。一般来说,我需要线程在一个无限循环中执行工作(一些IO操作),但是每个循环都是以主线程的权限开始的,而权限是仅在......
  • Python - 逆透视数据框
    我有一个按日期时间索引的表,每个日期时间都有多个层(中心和交货间隔):日期时间中心交货间隔结算点价格2024-01-0101:00:00休斯顿中心1......
  • 试图理解这个错误:致命的Python错误:PyEval_RestoreThread:该函数必须在持有GIL的情况下
    我有一个小型tkinter应用程序,我一直在其中实现最小的“拖放”,主要作为学习实验。我真正关心的是删除文件的文件路径。一切实际上都工作正常,直到我尝试在拖放后打包标签小部件。下面的最小工作示例。有问题的行会用注释指出。我通常不会在调试方面遇到太多麻烦,但我只是不知......
  • 如何使代码格式再次适用于 Python(Mac 上的 Visual Studio Code)?
    在Mac上,Option+Shift+F现在会显示“没有安装用于‘python’文件的格式化程序”。消息框:我尝试安装这个插件,但没有看到这种情况的变化:我已经为Python安装了这两个插件:但是正如@starball提到的,它可能已经减少了支持现在。......
  • 无法在 python 中安装 pip install expliot - bluepy 的 Building Wheel (pyproject.t
    在此处输入图像描述当我尝试在Windows计算机中通过cmd安装pipinstallexpliot包时,我收到2个错误名称×Buildingwheelforbluepy(pyproject.toml)didnotrunsuccessfully.│exitcode:1**AND**opt=self.warn_dash_deprecation......