I'm making a very simple GUI app using Tkinter for the first time. The problem I've encountered is that when using text Labels and text Buttons side-by-side using grid(), the button's height exceeds that of the label when I want them to be the same.
I've tried modifying the height option when making the Labels and Buttons, but despite the font size being the same for both, and the height for both being determined by "text units", setting height = 1 for both results in two different heights. I've also tried modifying the pady option of the button but that didn't resolve the problem either.
Here is some code that reproduces the problem:
import tkinter as tk
from tkinter import font
root = tk.Tk()
# making font larger for easier viewing
default_font = tk.font.nametofont("TkDefaultFont")
default_font.configure(size = 44)
# make a text Label and text Button, both with height 1... results in 2 different heights
tk.Label(root, text="foo", bg = 'black', fg = 'white', relief = 'raised', height = 1).grid(row=0,column=0)
tk.Button(root, text="bar", bg = 'red', fg = 'black', relief = 'raised', height = 1).grid(row=0,column=1)
root.mainloop()
Any help with this would be much appreciated.
use sticky option in widget.grid( grid_options ) .
sticky
What to do if the cell is larger than widget. By default, with sticky='', widget is centered in its cell. sticky may be the string concatenation of zero or more of N, E, S, W, NE, NW, SE, and SW, compass directions indicating the sides and corners of the cell to which widget sticks.
tk.Label(root, text="foo", bg = 'black', fg = 'white', relief = 'raised', height = 1).grid(row=0,column=0, sticky= W+E+N+S)
output:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With