The labels are currently side by side which is what I want. However each label's width is different. Is there a way to make each label have the same width?
Also is there a method that allows the labels to the have equal measurements and simultaneously fill all the way up to 250?
Here is my current code:
from tkinter import *
root = Tk()
root.geometry("250x50")
w = Label(root, text="Label UNO", bg="red", fg="white")
w.pack(side=LEFT)
w = Label(root, text="Label2", bg="green", fg="black")
w.pack(side=LEFT)
w = Label(root, text="Label DREI", bg="blue", fg="white")
w.pack(side=LEFT)
root.mainloop()
you should try checking tkinter's documentation on pack:
check if this is what you are looking for:
root = Tk()
root.geometry("250x50")
w = Label(root, text="Label UNO", bg="red", fg="white")
w.pack(side=LEFT, fill=BOTH, expand=True)
w = Label(root, text="Label2", bg="green", fg="black")
w.pack(side=LEFT, fill=BOTH, expand=True)
w = Label(root, text="Label DREI", bg="blue", fg="white")
w.pack(side=LEFT, fill=BOTH, expand=True)
root.mainloop()
Fill argument basically fills the parent container.
Expand argument will use to additional remaining space.
Also be sure to take a look at tkinter weight, here is an example.
You can use key word arguments width
, and height
with a tk.Label
import tkinter as tk
if __name__ == '__main__':
root = tk.Tk()
root.geometry("250x50")
label_frame = tk.Frame(root)
w = tk.Label(label_frame, text="Label UNO", bg="red", fg="white", width=8, height=2)
w.pack(side=tk.LEFT)
w = tk.Label(label_frame, text="Label2", bg="green", fg="black", width=8, height=2)
w.pack(side=tk.LEFT)
w = tk.Label(label_frame, text="Label DREI", bg="blue", fg="white", width=8, height=2)
w.pack(side=tk.LEFT)
label_frame.pack(expand=True)
root.mainloop()
tkinter is imported as tk
in order to keep the namespace tidy.
The tk.Labels
are included in a tk.Frame
, so once this is built, you do not have to worry about it until you pack the frame in your GUI.
The units for width and height are in "text units" when the label contains text.
To make the labels width the same , just add the width
option to the label
eg width=10
this will specifies how much space, in character widths.
if you want to apply the same width to multiable labels, store the width value in a variable, lets say label_w = 10
so your code might look like this:
from tkinter import *
root = Tk()
root.geometry("250x50")
label_w = 10
w = Label(root, text="Label UNO", bg="red", fg="white", width=label_w)
w.pack(side=LEFT)
w = Label(root, text="Label2", bg="green", fg="black", width=label_w)
w.pack(side=LEFT)
w = Label(root, text="Label DREI", bg="blue", fg="white", width=label_w)
w.pack(side=LEFT)
root.mainloop()
simultaneously fill all the way up to 250
this depend on how many labels you'll create, lets say window_width = 250
and labels_all = 3
then the width
option will be width = window_width/labels_all
also, if you don't write a geometry
values, the window size will automatically fit to labels.
if your window is simple enough , you might consider using the Packer Option expand
as in expand=1
, also include the fill
option if your label has color, fill Legal values: 'x', 'y', 'both', 'none'.
for more information, check label-options , packer-options
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.