Why does winfo_width() return a larger size of button widget than the size of what it exactly looks like?

PC_Wind

I want buttons/labels in the same column will share the same width with a specific/first button/label. The problem is winfo_width() seems not to return what I want. The return value of winfo_width() is multiple times the button.

I don't want to make width fixed by a number I select. Thus, I did not find a solution to my problem.

Here is part of my code:

button_1.update_idletasks()
print(button_1.winfo_width())
new_label = Label(frame_1, bg=  "#8432C7", width = 30, height = 5)
new_label.grid(row = 2, column = 0)

Since I don't have 10 reputation to post images, here is the link for the generated interface:

enter image description here

If you could see the above image, you should find the lower label (width = 30) is larger than the upper button (width = 157 ?).

But, according to my attempts, 157 seems not to be the width of button_1. I feel confused about what exactly winfo_width() returns here. Thus, I want to know what winfo_width() returns (why winfo_width() return 157 which should be a smaller number than 30) and how to get the exact width of the button.

I am stuck here for an hour since I just started to learn Tkinter recently. Thanks in advance for anyone who can give me suggestions.

Henry Yik

Fun fact: you don't have to bother with this at all.

Just pass sticky when you grid your widgets:

import tkinter as tk

root = tk.Tk()

button_1 = tk.Button(root,text="button_1")
button_1.grid(row = 1, column = 0, sticky="ew")
new_label = tk.Label(root, bg=  "#8432C7", height = 5)
new_label.grid(row = 2, column = 0, sticky="ew")

root.mainloop()

Then your columns will be auto-fit and scaled to the same size.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why the tuple has a larger size than expected?

Why is lineHeight of UIFont larger than font size?

Width 100% is larger than the screen size

Why is the size of a pointer to something on the heap larger than the size of a stack variable?

What exactly does array of size N mean?

In CNN, what does the size of a layer mean exactly?

Why is my Java socket receiving an array size larger than what was sent?

How does matplotlib decide what to display when the data size is larger than axis

Why does saving mat files with scipy result in larger file size than with Matlab?

Why does text with a larger font-size show up lower than other text and how to stop it?

Elasticsearch: What if size of index is larger than available RAM?

Why does the "GetDeviceCaps" Function always return exactly half of the size of my screen?

What exactly is Input size

Why are image sequences larger (in size) than the source videos?

Why is Java 6 compiled class size larger than Java 5?

Why is the resident set size (RSS) of my program larger than the executable?

is mobile viewport size larger than screen size?

BooleanArray with size larger than 2147483647

What would happen raft-log-gc-size-limit is larger than region-split-size in TiKV

Why a partitioned table size is larger than a normal table size with the same data

Why is exFAT's "Size on Disk" larger than that of NTFS when they both have the same allocation unit size?

How to find the largest size of a view that does not make window larger than screen size?

What does Map's size() method return?

What does "batch size" exactly mean in case of AWS Lambda?

@Scheduled & scheduler: What exactly does pool-size do?

What exactly does px represent in web font size

Why does numpy.sin return a different result if the argument size is greater than 8192?

why does the list size return 0?

HDD backups are larger than physical disk size