When i use the grid method, the text is not well centered.
In the exemple below, i would like to get the labels "row 0 col 0" and "row 0 col 1" well centered.
Here is an example :
import tkinter as tk
class GUI(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.frame1 = tk.LabelFrame(self, text="frame #1")
self.frame1.grid(row=0, column=0)
self.l1f1 = tk.Label(self.frame1, text="vvvvvvvvvveeeeeeeerrrryyyyyy "
"llllllllooooonnnngggg tttttteeeeeexxxxxxtttttt")
self.l1f1.grid(row=0, column=0)
self.frame2 = tk.LabelFrame(self, text="frame #2")
self.frame2.grid(row=1, column=0, sticky="ew")
self.l1f2 = tk.Label(self.frame2, text=" row 0 col 0 ")
self.l1f2.grid(row=0, column=0)
self.l2f2 = tk.Label(self.frame2, text=" row 0 col 1 ")
self.l2f2.grid(row=0, column=1)
self.update()
mygui = GUI()
mygui.mainloop()
CodePudding user response:
I found the solution using this method
grid_columnconfigure(index, weight)
Setting the weight to "1", the column will use the whole available width.
import tkinter as tk
class GUI(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.frame1 = tk.LabelFrame(self, text="frame #1")
self.frame1.grid(row=0, column=0)
self.l1f1 = tk.Label(self.frame1, text="vvvvvvvvvveeeeeeeerrrryyyyyy "
"llllllllooooonnnngggg tttttteeeeeexxxxxxtttttt")
self.l1f1.grid(row=0, column=0)
self.frame2 = tk.LabelFrame(self, text="frame #2")
self.frame2.grid(row=1, column=0, sticky="ew")
self.frame2.grid_columnconfigure(0, weight=1)
self.frame2.grid_columnconfigure(1, weight=1)
self.l1f2 = tk.Label(self.frame2, text=" row 0 col 0 ")
self.l1f2.grid(row=0, column=0)
self.l2f2 = tk.Label(self.frame2, text=" row 0 col 1 ")
self.l2f2.grid(row=0, column=1)
self.update()
mygui = GUI()
mygui.mainloop()