3

I have been working with python for a week and with Tkinter even less, so sorry if my question will be typical.


I want to write a simple GUI for an oscilloscope. And I have encountered a problem with how to fit button's size to size of other buttons. Here is what I have reached to (screenshot). enter image description here You might notice that size of trigger and horizontal buttons are less than all channel group. So how to fit their size exactly to channel group size. Here is a piece of my code.

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 ...

Is it possible if those buttons are in different frames? I would like leave it so.

9
  • You say that "trigger and horizontal buttons are less than all channel group", but the buttons look the same size of the group as a whole, and much bigger than the buttons in the group. Just to be clear, are you asking how to make the trigger and horizontal buttons the same size as the other buttons? Do you want the buttons to be centered vertically, or aligned at the top? Can you make a drawing to show what you want? Commented May 27, 2017 at 18:30
  • I want the trigger button take the same place as all channel buttons. Now it takes a little bit less. Commented May 27, 2017 at 18:38
  • I don't quite understand what you mean. By "it takes a little less", are you asking about what looks like a one pixel difference along the top of the buttons? Commented May 27, 2017 at 18:41
  • I cannot draw anything that`s why I will try to explain. There are two groups:Channel group and Trigger group. In these groups there are buttons: four and one respectively. If you take a look at the upper bound of the button CH1 Settings and the same of the Trigger settings button you will notice that they aren't on the same horizontal line. Commented May 27, 2017 at 18:47
  • Your code is syntactically incorrect. It has weird vertical bars. Commented May 27, 2017 at 18:56

2 Answers 2

5

If you want these widgets to be really perfectly aligned with one another, it's definitely better to align them on the same grid and use the sticky argument to make the buttons stretch to fill their cell:

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry. I did not notice that you did not use separate frames for groups. It is not good because, for example, labels do not belong groups though they should.
2

The root of the problem is that you aren't taking advantage of the options available to pack and grid. For example, when you do .pack(side='left'), you are leaving it up to tkinter to decide how to vertically align the widget in the space allotted.

By default tkinter will center the items vertically. Since the native height of the various sections (channel group, trigger group, horizontal group) are slightly different, it prevents them from having the tops and/or bottoms perfectly aligned.

A simple fix is to use the "fill" option to have the widgets fill the space allocated to them. If you don't want them to fill the space allotted you can use the "anchor" option to have the widgets anchored to the top.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.