如何在Tkinter中调整按钮的大小?

3

我已经使用python工作了一周,与Tkinter更短,所以如果我的问题是典型的,很抱歉。


我想为示波器编写一个简单的GUI。但是,我遇到了一个问题,那就是如何将按钮的大小适配到其他按钮的大小。这是我所达到的程度(截图)。 enter image description here 你可能会注意到triggerhorizontal按钮的大小小于所有channel组的大小。那么,如何将它们的大小恰好适配到channel组的大小。这是我的一段代码。

class StartPage(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    startLabel = tk.Label(self,
                          text="Start page")
    startLabel.pack(side="top")

    quitGroup = tk.Frame(self)
    quitGroup.pack(side="bottom")
    quitButton = tk.Button(quitGroup, text="Quit",
                           command=quit,
                           bg="pink")
    quitButton.grid(pady=10)

    channelGroup = tk.Frame(self)
    channelGroup.pack(side=tk.LEFT)
    chLabel = tk.Label(channelGroup,
                       text="Channel group")
    chLabel.grid(pady=10)

    ch1Button = tk.Button(channelGroup, text="CH1 Settings",
                          command=lambda: controller.show_frame("CH1"))
    ch1Button.grid(row=1, column=0)

    ch2Button = tk.Button(channelGroup, text="CH2 Settings",
                          command=lambda: controller.show_frame("CH2"))
    ch2Button.grid(row=2, column=0)

    ch3Button = tk.Button(channelGroup, text="CH3 Settings",
                          command=lambda: controller.show_frame("CH3"))

    ch3Button.grid(row=3, column=0)

    ch4Button = tk.Button(channelGroup, text="CH4 Settings",
                          command=lambda: controller.show_frame("CH4"))
    ch4Button.grid(row=4, column=0)

    triggerGroup = tk.Frame(self)
    triggerGroup.pack(side=tk.LEFT)
    trigLabel = tk.Label(triggerGroup,
                          text="Trigger group")
    trigLabel.grid(pady=10)
    trigButton = tk.Button(triggerGroup, text="Trigger Settings",
                           command=lambda: controller.show_frame("Trigger"))
    trigButton.grid(row=1, column=0)
    trigButton.grid(ipady=43)#43? What?

    horizGroup = tk.Frame(self)
    horizGroup.pack(side=tk.LEFT)
    horizLabel = tk.Label(horizGroup,
                          text="Horizontal group")
    horizLabel.grid(pady=10)
    horizButton = tk.Button(horizGroup,
                            text="Horizontal settings",
                            command=lambda: controller.show_frame("Horizontal"))
    horizButton.grid(row=1, column=0)
    horizButton.grid(ipady=43)#you again ...

如果这些按钮在不同的框架中,是否有可能?我想保持原样。


你说“触发器和水平按钮比所有通道组都小”,但是这些按钮看起来与整个组的大小相同,比组内的其他按钮要大得多。为了明确,您是在询问如何使触发器和水平按钮与其他按钮大小相同吗?您希望按钮在垂直方向上居中,还是顶部对齐?您能画一张图来说明您想要的吗? - Bryan Oakley
我希望“触发器”按钮与所有频道按钮占据相同的位置。现在它所占的位置略微有些不同。 - LRDPRDX
我不太明白你的意思。你说的“it takes a little less”是指按钮顶部看起来像是少了一个像素的差异吗? - Bryan Oakley
我无法绘制任何图形,因此我将尝试解释。这里有两个组:Channel groupTrigger group。 在这些组中有按钮:分别为四个和一个。如果您查看CH1设置按钮的上限以及触发器设置按钮的相同限制,则会注意到它们不在同一水平线上。 - LRDPRDX
你尝试过使用packfillanchorexpand选项进行实验吗? - Bryan Oakley
显示剩余4条评论
2个回答

5
如果你希望这些小部件真正地完美对齐,最好将它们对齐在同一网格上,并使用sticky参数使按钮拉伸以填充其单元格:

grid align

import tkinter as tk

root = tk.Tk()

chLabel = tk.Label(root, text="Channel group")
channelButtons = tk.Frame(root, bg='yellow')
ch1Button = tk.Button(channelButtons, text="CH1 Settings")
ch1Button.pack(fill='x')
ch2Button = tk.Button(channelButtons, text="CH2 Settings")
ch2Button.pack(fill='x')
ch3Button = tk.Button(channelButtons, text="CH3 Settings")
ch3Button.pack(fill='x')
ch4Button = tk.Button(channelButtons, text="CH4 Settings")
ch4Button.pack(fill='x')

trigLabel = tk.Label(root, text="Trigger group")
trigButton = tk.Button(root, text="Trigger Settings")

horizLabel = tk.Label(root, text="Horizontal group")
horizButton = tk.Button(root, text="Horizontal settings")

# Align the labels and buttons in a 2-by-3 grid
chLabel.grid(row=0, column=0, pady=10)
trigLabel.grid(row=0, column=1, pady=10)
horizLabel.grid(row=0, column=2, pady=10)
channelButtons.grid(row=1, column=0, sticky='news')
trigButton.grid(row=1, column=1, sticky='news')
horizButton.grid(row=1, column=2, sticky='news')

root.mainloop()

抱歉,我没有注意到您没有为组使用单独的框架。这不好,因为例如标签不属于组,但它们应该属于组。 - LRDPRDX

2
问题的根源在于你没有充分利用packgrid提供的选项。例如,当你使用.pack(side='left')时,你将垂直对齐小部件的决定权交给了tkinter。
默认情况下,tkinter会将项目垂直居中。由于各个部分(通道组、触发器组、水平组)的本地高度略有不同,这使它们无法完美对齐顶部和/或底部。
一个简单的解决方法是使用“fill”选项让小部件填充分配给它们的空间。如果你不想让它们填充分配的空间,你可以使用“anchor”选项将小部件锚定在顶部。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接