Tkinter: Why aren't my widgets expanding when I resize the window?


J

John Salerno

I can't seem to wrap my head around all the necessary arguments for making a widget expand when a window is resized. I've been following along with a tutorial and I feel like I'm doing everything it said, but I must be missing something. Here's what I have. What I expect is that when I resize the main window, I should be able to see the AppFrame's border stretch out, but it remains in the original position.

Is there something wrong with the sticky argument, maybe? The tutorial I'm reading says they can be strings, but it also uses what appears to be a tuple of constants like this: sticky=(N, S, E, W) -- but that didn't work either.


import tkinter as tk
import tkinter.ttk as ttk


class AppFrame(ttk.Frame):

def __init__(self, parent, **kwargs):
super().__init__(parent, **kwargs)
self.grid(row=0, column=0, sticky='nsew')
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)

self.create_widgets()

def create_widgets(self):
entry = ttk.Entry(self)
entry.grid(row=0, column=1, sticky='nsew')
#entry.columnconfigure(0, weight=1)
#entry.rowconfigure(0, weight=1)
label = ttk.Label(self, text='Name:')
label.grid(row=0, column=0, sticky='nsew')


root = tk.Tk()
root.title('Test Application')
frame = AppFrame(root, borderwidth=15, relief='sunken')
root.mainloop()
 
Ad

Advertisements

R

Rick Johnson

I can't seem to wrap my head around all the necessary arguments
for making a widget expand when a window is resized.

You will need to configure the root columns and rows also because the
configurations DO NOT propagate up the widget hierarchy! Actually, for
this example, I would recommend using the "pack" geometry manager on
the frame. Only use grid when you need to use grid. Never use any
functionality superfluously! Also, you should explicitly pack the
frame from OUTSIDE frame.__init__()!

## START CODE ##
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import BOTH, YES

class AppFrame(ttk.Frame):
def __init__(self, parent, **kwargs):
super().__init__(parent, **kwargs)
self._create_widgets()

def _create_widgets(self):
entry = ttk.Entry(self)
entry.grid(row=0, column=1, sticky='nsew')
label = ttk.Label(self, text='Name:')
label.grid(row=0, column=0, sticky='nsew')

root = tk.Tk()
root.title('Test Application')
frame = AppFrame(root, borderwidth=15, relief='sunken')
frame.pack(fill=BOTH, expand=YES)
root.mainloop()
## END CODE ##
Is there something wrong with the sticky argument, maybe? The
tutorial I'm reading says they can be strings, but it also uses what
appears to be a tuple of constants like this: sticky=(N, S, E, W) --
but that didn't work either.

I always use either:

sticky='nswe'
sticky=N+S+W+E
 
D

Dennis Lee Bieber

Is there something wrong with the sticky argument, maybe? The tutorial I'm reading says they can be strings, but it also uses what appears to be a tuple of constants like this: sticky=(N, S, E, W) -- but that didn't work either.
Those are probably "constants" defined in either tkinter or
tkinter.ttk; since you didn't use the 'from module import *' style you
likely would need to qualify the tuple: (tk.N, tk.S, etc.)

{Note: I've only done ONE tkinter application and that was back on
Python 2.3; and no resizing allowed <G>}
 
J

John Salerno

You will need to configure the root columns and rows also because the
configurations DO NOT propagate up the widget hierarchy! Actually, for
this example, I would recommend using the "pack" geometry manager on
the frame. Only use grid when you need to use grid. Never use any
functionality superfluously! Also, you should explicitly pack the
frame from OUTSIDE frame.__init__()!

Ok, so use pack when putting the frame into the root, since that's all that goes into the root directly. But just out of curiosity, what did I do wrong with using grid? How would it work with grid?

from tkinter.constants import BOTH, YES
I always use either:

sticky='nswe'
sticky=N+S+W+E

This is something I'm not too thrilled with. I don't like importing things piecemeal. I suppose I could do:

import tkinter.constants as tkc (or something like that)

and qualify each constant. Seems like more work, but it just seems better than having to manage each constant that I need in the import list.

Also, N+S+E+W and (N, S, E, W) don't seem to work unless qualified, so that's four more constants I'd have to explicitly import. And (tk.N, tk.S, tk.E, tk.W) is just horrible to look at.
 
R

Rick Johnson

Ok, so use pack when putting the frame into the root, since that's
all that goes into the root directly. But just out of curiosity,
what did I do wrong with using grid? How would it work with grid?

If you read my post carefully, i said: "You need to use
columnconfigure and rowconfigure on ROOT!". Here is the solution;
although, not the correct code you should use because pack is perfect
for this:

## START CODE ##
import tkinter as tk
import tkinter.ttk as ttk

class AppFrame(ttk.Frame):
def __init__(self, parent, **kwargs):
super().__init__(parent, **kwargs)
self.create_widgets()

def create_widgets(self):
entry = ttk.Entry(self)
entry.grid(row=0, column=1, sticky='nsew')
label = ttk.Label(self, text='Name:')
label.grid(row=0, column=0, sticky='nsew')

root = tk.Tk()
root.title('Test Application')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
frame = AppFrame(root, borderwidth=15, relief='sunken')
frame.grid(row=0, column=0, sticky='nsew')
root.mainloop()
## END CODE ##
I don't like importing things piecemeal. I suppose I could do:

So you prefer to pollute? How bout we just auto import the whole
Python stdlib so you can save a few keystrokes?
import tkinter.constants as tkc (or something like that)

and qualify each constant. Seems like more work, but it just seems
better than having to manage each constant that I need in the import
list.

Then do "from tkinter.constants import *". I have no complaints
against import all the constants during testing/building, however, you
should be a professional and import only the constants you are going
to use.
Also, N+S+E+W and (N, S, E, W) don't seem to work unless qualified,

Of course not, you never imported them! How could that code possibly
work?
so that's four more constants I'd have to explicitly import. And
(tk.N, tk.S, tk.E, tk.W) is just horrible to look at.

Wah!

Stop whining and act like a professional! You complain about
qualifying constants but you happily type "self" until your fingers
bleed without even a whimper???

Look, either import ONLY the constants you need, or qualify each
constant with a module name/variable; that is the choices available to
a professional. Or, just be lazy and pollute your namespace.

FYI: Lazy coders get what they deserve in the end.
 
J

John Salerno

I don't like importing things piecemeal. I suppose I could do:
So you prefer to pollute? How bout we just auto import the whole
Python stdlib so you can save a few keystrokes?

Wah!

Stop whining and act like a professional! You complain about
qualifying constants but you happily type "self" until your fingers
bleed without even a whimper???

Look, either import ONLY the constants you need, or qualify each
constant with a module name/variable; that is the choices available to
a professional. Or, just be lazy and pollute your namespace.

FYI: Lazy coders get what they deserve in the end.

How exactly am I being lazy and polluting the namespace? I never said I wanted to use the "*" import method, if that's what you are (wrongly) assuming.. I said it seems cleaner and, to me, LESS lazy to import the whole module as "import tkinter.constants as tkc" and qualify everything. It's certainlymore explicit than importing constants on an as-needed basis, having an ever-increasing list of constants, and referring to them unqualified in the code.

Yes, I complain that tk.N, tk.S, etc. is ugly to look at, but I'm saying itseems like a better way than using them unqualified, unless maybe there isyet another, better way.

As far as using "self" all the time, how do you know I never "whimpered" about it? The first time I learned about it I DIDN'T like it, because it seemed like a lot of unnecessary typing, but eventually I came to accept it because 1) you HAVE to do it, unlike the various options for referring to these constants, and 2) I began to like the explicitness of qualifying everything with "self."

You are very helpful, but you sure like to throw around the term "lazy" a little too unabashedly. I never said I wanted to import with "*", which you seem to think I want to do. I LIKE qualifying things, which is the reason Ididn't care so much for your method of importing the constants by name.
 
Ad

Advertisements

S

Steven D'Aprano

John, it is polite to leave attributions in place when you quote
somebody. I don't know who you are quoting above, but given the obnoxious
tone and the fact that this is about Tkinter, I'm guessing it is
RantingRick, a.k.a. Rick Johnson.

Rick is a notorious troll, and you'll probably save yourself a lot of
grief if you pay no attention to him. It's not so much that his advice is
*always* bad, but that you'll spend so many hours sifting through the
piles of bad advice, unprofessional language, and annoying self-
aggrandisement for the occasional nugget of gold that it just isn't worth
it.

Which is a pity, because I gather that Rick actually does know Tkinter
well. But dealing with him is like wrestling with a pig:

"I learned long ago, never to wrestle with a pig. You get dirty, and
besides, the pig likes it." -- George Bernard Shaw
 
J

John Salerno

John, it is polite to leave attributions in place when you quote
somebody. I don't know who you are quoting above, but given the obnoxious
tone and the fact that this is about Tkinter, I'm guessing it is
RantingRick, a.k.a. Rick Johnson.

Rick is a notorious troll, and you'll probably save yourself a lot of
grief if you pay no attention to him. It's not so much that his advice is
*always* bad, but that you'll spend so many hours sifting through the
piles of bad advice, unprofessional language, and annoying self-
aggrandisement for the occasional nugget of gold that it just isn't worth
it.

Which is a pity, because I gather that Rick actually does know Tkinter
well. But dealing with him is like wrestling with a pig:

"I learned long ago, never to wrestle with a pig. You get dirty, and
besides, the pig likes it." -- George Bernard Shaw

Thank you. Sorry about the attribution. That was my fault because I was trying to cite just the relevant bits, and I cut off the header too.
 
R

Rick Johnson

John, it is polite to leave attributions in place when you quote
somebody. I don't know who you are quoting above,
[...]
Which is a pity, because I gather that Rick actually does know Tkinter
well.

My, my. Between your ad hominem attacks, FUD, wild assumptions, and
veiled insults you did manage to get ONE small sentence of truthful
content to propagate up; congratulations! Excuse me whist i toss my
cookies!
But dealing with him is like wrestling with a pig:

And there it is again. We can't admit Rick is correct without
insulting him.

But Steven, when have you EVER dealt with me on a professional level?
When have you EVER offered a helping hand to one of my issues? For
example: I can remember asking you to explain the new .format method
of Py3000 and you said: "Sorry, too busy!". But you where not too busy
to troll-up the list! WHEN have you ever admitted (without insulting
me first) that i am in fact an asset to this community? Do you think
this community will ever be a homogeneous block? And if so, would that
be a good thing?

I have an idea: How about YOU stop clinging to some archaic ideal of
what YOU *think* this community *should* be, and instead, start
opening up your mind to diverse personalities and diverse ideas. Heck,
maybe you'll learn something new, "old dog". Remember how much you
ranted about diversity in evolution? And now you have the AUDACITY to
preach the opposite just to suit your own selfish argument. You are as
hollow as a politician Steven. Nothing that you say can be taken
seriously because it is just more propaganda and lies.

And one more thing, If ANYONE on this list deserves "thespian of the
year" award, it is you my friend! Between your comeo appearances as
"Devils Advocate", "Sammy the Strawman Stuffer", "Snarky Detractor",
and the ever-present "Troll Slayer" (saving damsels in distress from
the trolls of c.l.p) -- i'd say you have the nomination won by a
landslide!
 
M

Mark Lawrence

John, it is polite to leave attributions in place when you quote
somebody. I don't know who you are quoting above,
[...]
Which is a pity, because I gather that Rick actually does know Tkinter
well.

My, my. Between your ad hominem attacks, FUD, wild assumptions, and
veiled insults you did manage to get ONE small sentence of truthful
content to propagate up; congratulations! Excuse me whist i toss my
cookies!
But dealing with him is like wrestling with a pig:

And there it is again. We can't admit Rick is correct without
insulting him.

But Steven, when have you EVER dealt with me on a professional level?
When have you EVER offered a helping hand to one of my issues? For
example: I can remember asking you to explain the new .format method
of Py3000 and you said: "Sorry, too busy!". But you where not too busy
to troll-up the list! WHEN have you ever admitted (without insulting
me first) that i am in fact an asset to this community? Do you think
this community will ever be a homogeneous block? And if so, would that
be a good thing?

I have an idea: How about YOU stop clinging to some archaic ideal of
what YOU *think* this community *should* be, and instead, start
opening up your mind to diverse personalities and diverse ideas. Heck,
maybe you'll learn something new, "old dog". Remember how much you
ranted about diversity in evolution? And now you have the AUDACITY to
preach the opposite just to suit your own selfish argument. You are as
hollow as a politician Steven. Nothing that you say can be taken
seriously because it is just more propaganda and lies.

And one more thing, If ANYONE on this list deserves "thespian of the
year" award, it is you my friend! Between your comeo appearances as
"Devils Advocate", "Sammy the Strawman Stuffer", "Snarky Detractor",
and the ever-present "Troll Slayer" (saving damsels in distress from
the trolls of c.l.p) -- i'd say you have the nomination won by a
landslide!

There is no need for this. You (as I've stated in another reply) seem
to know what you're talking about WRT tkinter so please stick with the
Light rather than the Dark Side.
 
Ad

Advertisements

R

Rick Johnson

please stick with the
Light rather than the Dark Side.

You're correct. I need to ignore these negative distractions. Thanks
for re-focusing the conversation.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top