0
votes

I am working on a Tkinter application where I need a menu list.

I have a list of buttons created that have a Text label, and an Icon.

I can successfully justify and anchor the text to the left (West) so that the text is lined up nicely.

Unfortunately since the menu has different length words, the icon is just on the end and does not line up.

Is there a way in a Tkinter button to justify and anchor the text separately from the icon? So the text/label can be on the West, and the Icon can be on the right, so everything lines up in the menu list.

1
Maybe get the maximum of those length of words first.And fill those which is not longest with space? - jizhihaoSAMA

1 Answers

2
votes

An easy way is get the maximum of those length of words first.And fill those which is not the longest with space.Don't know whether it is the best way.like:

import tkinter as tk

root = tk.Tk()
img = tk.PhotoImage(file="xxxx")

textList = ["open", "setting", "close"]
maxLength = max(map(len, textList)) # get the maximum of length

for i in textList:
    btn = tk.Button(root, text=i.ljust(maxLength), font=("Consolas",10),image=img, compound=tk.RIGHT)
    btn.pack()
root.mainloop()

PS: You need to use monospaced font,and make sure your icon has the same size.If not, consider use PIL.Image.resize().