Reputation: 433
I'm a very very new to Python and was trying to develop a GUI in Python to help automating some of my work. I've looked at a few older posts in this forum and others, but couldn't find my mistake.
I'm trying to make a label text centered using the .grid()
method, both in the window that pops but also keep it centered if the user resizes it. The main suggestion I've found online was to use the .grid_rowconfigure()
method and give a non 0 weight to the relevant row, but that doesn't seems to be working.
I've created an alternate test code that has the same problem to avoid posting a wall of code:
import tkinter as tk
class TestApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
label = tk.Label(self, text="TEST")
label.grid(row=0, sticky="nsew")
label.grid_rowconfigure(0, weight=1)
TestApp().mainloop()
Thanks in advance for your help!
Upvotes: 1
Views: 1967
Reputation: 9597
Calling grid_rowconfigure()
on the label would only affect widgets contained WITHIN the label. To affect the label itself, you have to call this method on its parent (which is self
in your test code).
Fixed code:
import tkinter as tk
class TestApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
label = tk.Label(self, text="TEST")
label.grid(row=0, sticky="nsew")
self.grid_rowconfigure(0, weight=1)
TestApp().mainloop()
Upvotes: 2
Reputation: 765
You don't need to give the relevant row a non-zero weight, you can give the other rows a non-zero weight:
label = tk.Label(self, text="TEST")
label.grid(row=1, column=1)
self.grid_rowconfigure(0, weight=1)
self.grid_rowconfigure(2, weight=1)
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(2, weight=1)
Upvotes: 1