Reputation: 195
I am making a Python program, and I am looking for ways to make a nice centre-aligned title at the top of inside my program. However, I am having trouble doing this because with the current tools I have, I would have to experiment my label spacing length to match how many characters I have for my title. Is there a way to make a Python label be certain amount of pixels long regardless of length of the text? I am specifically looking for a way of specifically making a label be a specific number of pixels long so I don't have to experiment with all these different lengths.
I tried using the "padx" and "pady" parameters, but I have found that the lengths will always change depending on the length of the text of the label itself. I also tried the "width" parameter, but it only extends by a number of textlengths long.
from tkinter import *
root = Tk()
root.geometry("960x600")
label_toptitle = Label(root,
text="Program Name",
padx=40,
pady=40,
font=(None, 40),
#height=2,
#width=30
)
root.mainloop()
This is my current code right here, but if I change the name "Program Name" to another name, the padx and pady and/or width or height functions will become not the same amount of pixels long. I need a way to make the label a fixed amount of pixels long.
Upvotes: 1
Views: 1491
Reputation: 385900
Your question title mentions setting the length of a label, but it seems your real goal is to center a label without having to calculate margins.
The best solution for that is to not use padding or margins to center the widget. Just tell tkinter you want things centered when you use grid
or pack
, which is actually the default. You don't have to do much at all.
For example, the following will give a window with a centered title:
import tkinter as tk
root = tk.Tk()
root.geometry("960x600")
label_toptitle = tk.Label(root,
text="Program Name",
font=(None, 40),
)
label_toptitle.pack(side="top", fill="x")
root.mainloop()
Grid can center things too, though the exact syntax depends on what other rows and columns you have. Without knowing that it's hard to give a useful example.
If you really need to specify an exact width in pixels for a label, you can add a transparent one-pixel image to the label and hide it behind the text. When you add an image, the width
attribute represents a width in pixels rather than characters.
Example:
pixel = tk.PhotoImage(width=1, height=1)
label = tk.Label(root, image=pixel, text="Hello", compound="center", width=100)
Upvotes: 2
Reputation: 384
You could make a variable that declares the max size of a label and then use it to fix the width/height issues on all label declarations as follows:
maxSizeX = 100
maxSizeY = 100
labelTitleText = "Program Name"
labelTitleTextSize = len(labelTitleText)
labelTitle = Label(root,
text=labelTitleText,
width=(maxSizeX-labelTitleTextSize),
height=maxSizeY,
font=(None, 40)
)
Although I'm unsure as to how you would get character pixel width on the display if you could then you could change the above to:
maxSizeX = 100
maxSizeY = 100
labelTitleText = "Program Name"
labelTitleTextSize = len(labelTitleText) * pixelWidth
labelTitle = Label(root,
text=labelTitleText,
padx=(maxSizeX-labelTitleTextSize),
pady=maxSizeY,
font=(None, 40)
)
Upvotes: 0