• Problem resizing a window and button placement

    From Steve GS@3:633/280.2 to All on Sat Feb 24 12:14:00 2024
    Python, Tkinter: How do I
    determine if a window has been
    resized? I want to locate
    buttons vertically along the
    right border and need to know
    the new width. The buttons are
    to move with the change of
    location of the right-side
    border.







    SGA




    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From MRAB@3:633/280.2 to All on Sat Feb 24 13:26:43 2024
    On 2024-02-24 01:14, Steve GS via Python-list wrote:
    Python, Tkinter: How do I
    determine if a window has been
    resized? I want to locate
    buttons vertically along the
    right border and need to know
    the new width. The buttons are
    to move with the change of
    location of the right-side
    border.

    Bind an event handler for '<Configure>':

    ----8<----

    import tkinter as tk

    def on_configure(*args):
    print(args)

    root = tk.Tk()
    root.bind('<Configure>', on_configure)
    root.mainloop()

    ----8<----

    Are you placing the buttons yourself? I always use layouts and they
    handle such things automatically.


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Steve GS@3:633/280.2 to All on Sat Feb 24 15:33:05 2024
    How do I extract the values
    from args?

    SGA

    -----Original Message-----
    From: Python-list
    <python-list-bounces+gronicus=
    sga.ninja@python.org> On
    Behalf Of MRAB via Python-list
    Sent: Friday, February 23,
    2024 9:27 PM
    To: python-list@python.org
    Subject: Re: Problem resizing
    a window and button placement

    On 2024-02-24 01:14, Steve GS
    via Python-list wrote:
    Python, Tkinter: How do I
    determine if a window has
    been
    resized? I want to locate
    buttons vertically along the
    right border and need to
    know
    the new width. The buttons
    are
    to move with the change of
    location of the right-side
    border.

    Bind an event handler for
    '<Configure>':

    ----8<----

    import tkinter as tk

    def on_configure(*args):
    print(args)

    root = tk.Tk()
    root.bind('<Configure>',
    on_configure)
    root.mainloop()

    ----8<----

    Are you placing the buttons
    yourself? I always use layouts
    and they handle such things
    automatically.

    --
    https://mail.python.org/mailma
    n/listinfo/python-list


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Barry@3:633/280.2 to All on Sat Feb 24 19:04:18 2024


    On 24 Feb 2024, at 04:36, Steve GS via Python-list <python-list@python.org= wrote:
    =20
    How do I extract the values
    from args?

    You can look up the args in documentation.
    You can run the example code MRAB provided and see what is printed to learn w= hat is in the args.

    Barry



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Steve GS@3:633/280.2 to All on Sat Feb 24 19:20:47 2024
    Yes, I ran that elegantly
    simple code. The print
    statement reports the X, Y,
    Height and Width values.
    However, I do not see how to
    capture the width value.

    I experimented with the code
    Vwidth = rootV.winfo_width()
    and it also reports the width
    as I resize the window.

    However, I cannot seem to use
    the variable Vwidth outside
    the sub routine. It is acting
    as if Vwidth is not global but
    I added that. It is reported
    that Vwidth is not defined
    when I try to use it in my
    code.

    So close......
    SGA

    -----Original Message-----
    From: Barry
    <barry@barrys-emacs.org>
    Sent: Saturday, February 24,
    2024 3:04 AM
    To: Steve GS
    <Gronicus@sga.ninja>
    Cc: MRAB
    <python@mrabarnett.plus.com>;
    python-list@python.org
    Subject: Re: Problem resizing
    a window and button placement



    On 24 Feb 2024, at 04:36,
    Steve GS via Python-list
    <python-list@python.org>
    wrote:

    How do I extract the values
    from args?

    You can look up the args in
    documentation.
    You can run the example code
    MRAB provided and see what is
    printed to learn what is in
    the args.

    Barry



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Sat Feb 24 20:56:11 2024
    "Steve GS" <Gronicus@SGA.Ninja> writes:
    width

    Using a geometry manager, such as "pack", you usually do not need
    to know when a window has been resized or to know that width.

    You need to read a good tkinter book and learn about:

    tkinter.Tk
    pack
    tkinter.Frame
    side=tkinter.LEFT
    side=tkinter.RIGHT
    expand=tkinter.YES
    fill=tkinter.X
    fill=tkinter.Y
    fill=tkinter.BOTH

    . I'd recommand the GUI part of "Programming Python" by Mark Lutz!

    The geometry manages "pack" and "grid" should be able
    to handle 99 percent of all GUI geometry requirements,
    so you would need a good reason not to use them.

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Thomas Passin@3:633/280.2 to All on Sun Feb 25 00:39:55 2024
    On 2/24/2024 3:20 AM, Steve GS via Python-list wrote:
    Yes, I ran that elegantly
    simple code. The print
    statement reports the X, Y,
    Height and Width values.
    However, I do not see how to
    capture the width value.

    I experimented with the code
    Vwidth = rootV.winfo_width()
    and it also reports the width
    as I resize the window.

    However, I cannot seem to use
    the variable Vwidth outside
    the sub routine. It is acting
    as if Vwidth is not global but
    I added that. It is reported
    that Vwidth is not defined
    when I try to use it in my
    code.

    Well, yes, in Python a variable created inside a function or method is
    local to that function unless you declare it global. That characteristic
    is called its "scope". But if you think you need it to be a global
    variable you should rethink your design. For one thing, before the next
    time you use your global variable the window size may have changed again.

    Instead, it would be better to have the function that responds to the
    resize event perform the action that you want, or call another function
    that does, passing the new width to it.

    Note that in most programming languages, variables have a scope. The
    rules about those scopes vary between languages.


    So close......
    SGA

    -----Original Message-----
    From: Barry
    <barry@barrys-emacs.org>
    Sent: Saturday, February 24,
    2024 3:04 AM
    To: Steve GS
    <Gronicus@sga.ninja>
    Cc: MRAB
    <python@mrabarnett.plus.com>;
    python-list@python.org
    Subject: Re: Problem resizing
    a window and button placement



    On 24 Feb 2024, at 04:36,
    Steve GS via Python-list
    <python-list@python.org>
    wrote:

    How do I extract the values
    from args?

    You can look up the args in
    documentation.
    You can run the example code
    MRAB provided and see what is
    printed to learn what is in
    the args.

    Barry




    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Sun Feb 25 01:37:49 2024
    Thomas Passin <list1@tompassin.net> writes:
    Well, yes, in Python a variable created inside a function or method is
    local to that function unless you declare it global.

    Or equivalent,

    main.py

    def f():
    globals()[ 'x' ]= 2 # not a declaration!

    f()

    print( x )

    _sys.stderr

    2

    But if you think you need it to be a global variable you
    should rethink your design.

    "Global" variables in Python are actually just "module variable".
    Module variables are not so bad. If you think of the module as a
    singleton object it's kinda like a class.

    But the usual recommendation is to use a class. I.e., transform

    main.py

    def write_global():
    globals()[ 'x' ]= 2

    def read_global():
    print( globals()[ 'x' ])

    write_global()
    read_global()

    _sys.stderr

    2

    into

    class Object_:
    def write_field( self ):
    self.x = 2
    def read_field( self ):
    print( self.x )

    object_ = Object_()
    object_.write_field()
    object_.read_field()

    _sys.stderr

    2

    For one thing, before the next time you use your global
    variable the window size may have changed again.

    The OP might be sleep walking into writing his own ad hoc,
    informally-specified, bug-ridden, slow implementation of
    half of a geometry manager like "pack" or "grid".

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Grant Edwards@3:633/280.2 to All on Sat Feb 24 14:28:27 2024
    On 2024-02-24, MRAB via Python-list <python-list@python.org> wrote:
    On 2024-02-24 01:14, Steve GS via Python-list wrote:

    Python, Tkinter: How do I determine if a window has been resized? I
    want to locate buttons vertically along the right border and need
    to know the new width. The buttons are to move with the change of
    location of the right-side border.

    Bind an event handler for '<Configure>':

    ----8<----
    [...]
    ----8<----

    Are you placing the buttons yourself? I always use layouts and they
    handle such things automatically.

    Yes, definitely what he said: use a layout manager.

    I hope this doesn't sound rude, but if you're calculating button
    positions based on window size, you're doing it wrong and will end up
    wasting a lot of time to produce something that won't work right.

    Use a layout manager:

    https://tkinterpython.top/layout/#:~:text=Tkinter%20has%20three%20built%2Din,%2C%20grid%20%2C%20and%20place%20managers.

    https://www.pythonguis.com/tutorials/create-ui-with-tkinter-pack-layout-manager/

    https://www.pythonguis.com/tutorials/use-tkinter-to-design-gui-layout/

    You'll have to spend a little time learning how they work, but in the
    end you'll get done sooner and have better results.

    --
    Grant


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Steve GS@3:633/280.2 to All on Sun Feb 25 11:33:52 2024
    "Well, yes, in Python a
    variable created inside a
    function or method is local to
    that function unless you
    declare it global."

    Yes, I knew that. I tried to
    global it both before the
    function call and within it.
    Same for when I created the
    variable. If I try to use it
    in the rest of the code, it
    keeps coming up as not
    declared. In other functions,
    I can 'return' the variable
    but that apparently would not
    work for this function.

    Is this type of function any
    different that that which I
    have been using?

    SGA

    -----Original Message-----
    From: Python-list
    <python-list-bounces+gronicus=
    sga.ninja@python.org> On
    Behalf Of Thomas Passin via
    Python-list
    Sent: Saturday, February 24,
    2024 8:40 AM
    To: python-list@python.org
    Subject: Re: Problem resizing
    a window and button placement

    On 2/24/2024 3:20 AM, Steve GS
    via Python-list wrote:
    Yes, I ran that elegantly
    simple code. The print
    statement reports the X, Y,
    Height and Width values.
    However, I do not see how to
    capture the width value.

    I experimented with the
    code
    Vwidth = rootV.winfo_width()
    and it also reports the
    width
    as I resize the window.

    However, I cannot seem to
    use
    the variable Vwidth outside
    the sub routine. It is
    acting
    as if Vwidth is not global
    but
    I added that. It is
    reported
    that Vwidth is not defined
    when I try to use it in my
    code.

    Well, yes, in Python a
    variable created inside a
    function or method is local to
    that function unless you
    declare it global. That
    characteristic is called its
    "scope". But if you think you
    need it to be a global
    variable you should rethink
    your design. For one thing,
    before the next time you use
    your global variable the
    window size may have changed
    again.

    Instead, it would be better to
    have the function that
    responds to the resize event
    perform the action that you
    want, or call another function
    that does, passing the new
    width to it.

    Note that in most programming
    languages, variables have a
    scope. The rules about those
    scopes vary between languages.


    So close......
    SGA

    -----Original Message-----
    From: Barry
    <barry@barrys-emacs.org>
    Sent: Saturday, February 24,
    2024 3:04 AM
    To: Steve GS
    <Gronicus@sga.ninja>
    Cc: MRAB

    <python@mrabarnett.plus.com>;
    python-list@python.org
    Subject: Re: Problem
    resizing
    a window and button
    placement



    On 24 Feb 2024, at 04:36,
    Steve GS via Python-list
    <python-list@python.org>
    wrote:

    How do I extract the values
    from args?

    You can look up the args in
    documentation.
    You can run the example code
    MRAB provided and see what
    is
    printed to learn what is in
    the args.

    Barry



    --
    https://mail.python.org/mailma
    n/listinfo/python-list


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From MRAB@3:633/280.2 to All on Sun Feb 25 11:49:16 2024
    On 2024-02-25 00:33, Steve GS via Python-list wrote:
    "Well, yes, in Python a
    variable created inside a
    function or method is local to
    that function unless you
    declare it global."

    Yes, I knew that. I tried to
    global it both before the
    function call and within it.
    Same for when I created the
    variable. If I try to use it
    in the rest of the code, it
    keeps coming up as not
    declared. In other functions,
    I can 'return' the variable
    but that apparently would not
    work for this function.

    Is this type of function any
    different that that which I
    have been using?

    Please post a short example that shows the problem.


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Steve GS@3:633/280.2 to All on Sun Feb 25 13:51:54 2024
    import tkinter as tk

    #global Ww Neither global
    helps
    def on_configure(*args):
    # print(args)
    #global Ww Neither
    global helps
    Ww = root.winfo_width()
    print("WwInside = <" +
    str(Ww) + ">")

    root = tk.Tk()
    root.bind('<Configure>',
    on_configure)
    print("WwOutside = <" +
    str(Ww) + ">")
    #NameError: name 'Ww' is not
    defined
    root.mainloop()

    SGA

    -----Original Message-----
    From: Python-list
    <python-list-bounces+gronicus=
    sga.ninja@python.org> On
    Behalf Of MRAB via Python-list
    Sent: Saturday, February 24,
    2024 7:49 PM
    To: python-list@python.org
    Subject: Re: Problem resizing
    a window and button placement

    On 2024-02-25 00:33, Steve GS
    via Python-list wrote:
    "Well, yes, in Python a
    variable created inside a
    function or method is local
    to
    that function unless you
    declare it global."

    Yes, I knew that. I tried to
    global it both before the
    function call and within it.
    Same for when I created the
    variable. If I try to use it
    in the rest of the code, it
    keeps coming up as not
    declared. In other
    functions,
    I can 'return' the variable
    but that apparently would
    not
    work for this function.

    Is this type of function any
    different that that which I
    have been using?

    Please post a short example
    that shows the problem.

    --
    https://mail.python.org/mailma
    n/listinfo/python-list


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Thomas Passin@3:633/280.2 to All on Sun Feb 25 14:38:36 2024
    On 2/24/2024 9:51 PM, Steve GS via Python-list wrote:
    First of all, please make sure that the formatting is readable and
    especially the indentation. This is Python, after all.

    Do not use tabs; use 3 or 4 spaces instead of each tab.
    import tkinter as tk

    #global Ww Neither global
    helps
    def on_configure(*args):
    # print(args)
    #global Ww Neither
    global helps
    Ww = root.winfo_width()
    print("WwInside = <" +
    str(Ww) + ">")

    root = tk.Tk()
    root.bind('<Configure>',
    on_configure)
    print("WwOutside = <" +
    str(Ww) + ">")
    #NameError: name 'Ww' is not
    defined

    The function that declares Ww hasn't run yet. As I wrote earlier, the
    function bound to the callback should do all the work for the callback,
    or it should call other functions that do. That's if you don't let a
    layout do it all for you, as others have written.

    root.mainloop()

    SGA

    -----Original Message-----
    From: Python-list
    <python-list-bounces+gronicus=
    sga.ninja@python.org> On
    Behalf Of MRAB via Python-list
    Sent: Saturday, February 24,
    2024 7:49 PM
    To: python-list@python.org
    Subject: Re: Problem resizing
    a window and button placement

    On 2024-02-25 00:33, Steve GS
    via Python-list wrote:
    "Well, yes, in Python a
    variable created inside a
    function or method is local
    to
    that function unless you
    declare it global."

    Yes, I knew that. I tried to
    global it both before the
    function call and within it.
    Same for when I created the
    variable. If I try to use it
    in the rest of the code, it
    keeps coming up as not
    declared. In other
    functions,
    I can 'return' the variable
    but that apparently would
    not
    work for this function.

    Is this type of function any
    different that that which I
    have been using?

    Please post a short example
    that shows the problem.



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From MRAB@3:633/280.2 to All on Sun Feb 25 14:36:04 2024
    On 2024-02-25 02:51, Steve GS wrote:
    import tkinter as tk

    #global Ww Neither global helps
    def on_configure(*args):
    # print(args)
    #global Ww Neither global helps
    Ww = root.winfo_width()
    print("WwInside = <" + str(Ww) + ">")

    root = tk.Tk()
    root.bind('<Configure>', on_configure)
    print("WwOutside = <" + str(Ww) + ">")
    #NameError: name 'Ww' is not defined
    root.mainloop()
    'Ww' won't exist until 'on_configure' assigns to it, and that won't
    happen until `mainloop` starts.

    Also, 'global' works only within a function.

    ----8<----

    import tkinter as tk

    def on_configure(event):
    ÿÿÿ print(f'{event.width=}, {event.height=}')

    root = tk.Tk()
    root.bind('<Configure>',on_configure)
    root.mainloop()

    ----8<----


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Steve GS@3:633/280.2 to All on Sun Feb 25 14:53:40 2024
    The print statement in the
    function prints.
    Does that not mean that the
    function is being called?



    SGA

    -----Original Message-----
    From: Python-list
    <python-list-bounces+gronicus=
    sga.ninja@python.org> On
    Behalf Of Thomas Passin via
    Python-list
    Sent: Saturday, February 24,
    2024 10:39 PM
    To: python-list@python.org
    Subject: Re: Problem resizing
    a window and button placement

    On 2/24/2024 9:51 PM, Steve GS
    via Python-list wrote:
    First of all, please make sure
    that the formatting is
    readable and especially the
    indentation. This is Python,
    after all.

    Do not use tabs; use 3 or 4
    spaces instead of each tab.
    import tkinter as tk

    #global Ww Neither global
    helps
    def on_configure(*args):
    # print(args)
    #global Ww Neither
    global helps
    Ww =
    root.winfo_width()
    print("WwInside = <" +
    str(Ww) + ">")

    root = tk.Tk()
    root.bind('<Configure>',
    on_configure)
    print("WwOutside = <" +
    str(Ww) + ">")
    #NameError: name 'Ww' is not
    defined

    The function that declares Ww
    hasn't run yet. As I wrote
    earlier, the function bound to
    the callback should do all the
    work for the callback, or it
    should call other functions
    that do. That's if you don't
    let a layout do it all for
    you, as others have written.

    root.mainloop()

    SGA

    -----Original Message-----
    From: Python-list

    <python-list-bounces+gronicus=
    sga.ninja@python.org> On
    Behalf Of MRAB via
    Python-list
    Sent: Saturday, February 24,
    2024 7:49 PM
    To: python-list@python.org
    Subject: Re: Problem
    resizing
    a window and button
    placement

    On 2024-02-25 00:33, Steve
    GS
    via Python-list wrote:
    "Well, yes, in Python a
    variable created inside a
    function or method is local
    to
    that function unless you
    declare it global."

    Yes, I knew that. I tried
    to
    global it both before the
    function call and within
    it.
    Same for when I created the
    variable. If I try to use
    it
    in the rest of the code, it
    keeps coming up as not
    declared. In other
    functions,
    I can 'return' the variable
    but that apparently would
    not
    work for this function.

    Is this type of function
    any
    different that that which I
    have been using?

    Please post a short example
    that shows the problem.


    --
    https://mail.python.org/mailma
    n/listinfo/python-list


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Steve GS@3:633/280.2 to All on Sun Feb 25 14:58:00 2024
    So, how do I use the width value in my code?

    SGA

    -----Original Message-----
    From: Python-list <python-list-bounces+gronicus=3Dsga.ninja@python.org> =
    On Behalf Of MRAB via Python-list
    Sent: Saturday, February 24, 2024 10:36 PM
    To: python-list@python.org
    Subject: Re: Problem resizing a window and button placement

    On 2024-02-25 02:51, Steve GS wrote:
    import tkinter as tk

    #global Ww Neither global helps
    def on_configure(*args):
    # print(args)
    #global Ww Neither global helps
    Ww =3D root.winfo_width()
    print("WwInside =3D <" + str(Ww) + ">")

    root =3D tk.Tk()
    root.bind('<Configure>', on_configure) print("WwOutside =3D <" + =
    str(Ww)=20
    + ">")
    #NameError: name 'Ww' is not defined
    root.mainloop()
    'Ww' won't exist until 'on_configure' assigns to it, and that won't =
    happen until `mainloop` starts.

    Also, 'global' works only within a function.

    ----8<----

    import tkinter as tk

    def on_configure(event):
    print(f'{event.width=3D}, {event.height=3D}')

    root =3D tk.Tk()
    root.bind('<Configure>',on_configure)
    root.mainloop()

    ----8<----

    --
    https://mail.python.org/mailman/listinfo/python-list


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Alan Gauld@3:633/280.2 to All on Mon Feb 26 04:44:05 2024
    On 25/02/2024 03:58, Steve GS via Python-list wrote:
    import tkinter as tk

    Ww = None

    def on_configure(*args):
    global Ww
    Ww = root.winfo_width()
    print("Ww Inside = <" + str(Ww) + ">")

    root = tk.Tk()
    root.bind('<Configure>', on_configure)
    root.mainloop()

    print("Ww Outside = <" + str(Ww) > + ">")

    Produces:
    Ww Inside = <200>
    Ww Inside = <200>
    Ww Inside = <205>
    Ww Inside = <205>
    Ww Inside = <206>
    Ww Inside = <206>
    Ww Outside = <206>

    HTH

    --
    Alan G
    Author of the Learn to Program web site
    http://www.alan-g.me.uk/
    http://www.amazon.com/author/alan_gauld
    Follow my photo-blog on Flickr at:
    http://www.flickr.com/photos/alangauldphotos



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Steve GS@3:633/280.2 to All on Mon Feb 26 08:19:57 2024
    SOLUTION FOUND!

    The fix was to write the code that uses the width value and to place it =
    into the function itself. =20
    Kluge? Maybe but it works.=20

    Mischief Managed.

    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
    As for the most recent suggestion, it fails for me:

    Traceback (most recent call last):
    File "F:/___zInsulin Code A 08-02-23/WinPic/IOWw.pyw", line 14, in =
    <module>
    print("Ww Outside =3D <" + str(Ww) > + ">")
    TypeError: bad operand type for unary +: 'str'

    With the need to close the window, it adds an extra step and =
    intervention to the program to use. I am not sure how this help[s.

    As a curio, it would be interesting to see how to use the value of a = variable, created in the function used here, and make it available to =
    the code outside the function.



    SGA

    -----Original Message-----
    From: Alan Gauld <learn2program@gmail.com>=20
    Sent: Sunday, February 25, 2024 12:44 PM
    To: Steve GS <Gronicus@SGA.Ninja>; python-list@python.org
    Subject: Re: RE: Problem resizing a window and button placement

    On 25/02/2024 03:58, Steve GS via Python-list wrote:
    import tkinter as tk

    Ww =3D None

    def on_configure(*args):
    global Ww
    Ww =3D root.winfo_width()
    print("Ww Inside =3D <" + str(Ww) + ">")

    root =3D tk.Tk()
    root.bind('<Configure>', on_configure)
    root.mainloop()

    print("Ww Outside =3D <" + str(Ww) > + ">")

    Produces:
    Ww Inside =3D <200>
    Ww Inside =3D <200>
    Ww Inside =3D <205>
    Ww Inside =3D <205>
    Ww Inside =3D <206>
    Ww Inside =3D <206>
    Ww Outside =3D <206>

    HTH

    --=20
    Alan G
    Author of the Learn to Program web site
    http://www.alan-g.me.uk/
    http://www.amazon.com/author/alan_gauld
    Follow my photo-blog on Flickr at:
    http://www.flickr.com/photos/alangauldphotos



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Thomas Passin@3:633/280.2 to All on Mon Feb 26 09:55:12 2024
    On 2/25/2024 4:19 PM, Steve GS via Python-list wrote:
    SOLUTION FOUND!

    The fix was to write the code that uses the width value and to place it into the function itself.
    Kluge? Maybe but it works.

    Right, just what I wrote earlier:

    "have the function that responds to the resize event perform the action
    that you want"

    Mischief Managed.

    ========================
    As for the most recent suggestion, it fails for me:

    Traceback (most recent call last):
    File "F:/___zInsulin Code A 08-02-23/WinPic/IOWw.pyw", line 14, in <module>
    print("Ww Outside = <" + str(Ww) > + ">")
    TypeError: bad operand type for unary +: 'str'

    With the need to close the window, it adds an extra step and intervention to the program to use. I am not sure how this help[s.

    As a curio, it would be interesting to see how to use the value of a variable, created in the function used here, and make it available to the code outside the function.



    SGA

    -----Original Message-----
    From: Alan Gauld <learn2program@gmail.com>
    Sent: Sunday, February 25, 2024 12:44 PM
    To: Steve GS <Gronicus@SGA.Ninja>; python-list@python.org
    Subject: Re: RE: Problem resizing a window and button placement

    On 25/02/2024 03:58, Steve GS via Python-list wrote:
    import tkinter as tk

    Ww = None

    def on_configure(*args):
    global Ww
    Ww = root.winfo_width()
    print("Ww Inside = <" + str(Ww) + ">")

    root = tk.Tk()
    root.bind('<Configure>', on_configure)
    root.mainloop()

    print("Ww Outside = <" + str(Ww) > + ">")

    Produces:
    Ww Inside = <200>
    Ww Inside = <200>
    Ww Inside = <205>
    Ww Inside = <205>
    Ww Inside = <206>
    Ww Inside = <206>
    Ww Outside = <206>

    HTH



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From MRAB@3:633/280.2 to All on Mon Feb 26 10:40:20 2024
    On 2024-02-25 21:19, Steve GS via Python-list wrote:
    SOLUTION FOUND!

    The fix was to write the code that uses the width value and to place it into the function itself.
    Kluge? Maybe but it works.

    Mischief Managed.

    ========================
    As for the most recent suggestion, it fails for me:

    Traceback (most recent call last):
    File "F:/___zInsulin Code A 08-02-23/WinPic/IOWw.pyw", line 14, in <module>
    print("Ww Outside = <" + str(Ww) > + ">")
    TypeError: bad operand type for unary +: 'str'

    It fails because there's a mistake. It should be:

    print("Ww Outside = <" + str(Ww) + ">")

    With the need to close the window, it adds an extra step and intervention to the program to use. I am not sure how this help[s.

    As a curio, it would be interesting to see how to use the value of a variable, created in the function used here, and make it available to the code outside the function.

    [snip]



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Steve GS@3:633/280.2 to All on Mon Feb 26 18:56:35 2024
    Ww Inside = <250>
    Ww Inside = <249>
    Ww Inside = <250>
    Ww Outside =
    <1770662408256on_configure>

    Here is my result...

    SGA

    -----Original Message-----
    From: Python-list
    <python-list-bounces+gronicus=
    sga.ninja@python.org> On
    Behalf Of MRAB via Python-list
    Sent: Sunday, February 25,
    2024 6:40 PM
    To: python-list@python.org
    Subject: Re: Problem resizing
    a window and button placement

    On 2024-02-25 21:19, Steve GS
    via Python-list wrote:
    SOLUTION FOUND!

    The fix was to write the
    code that uses the width value
    and to place it into the
    function itself.
    Kluge? Maybe but it works.

    Mischief Managed.

    ========================
    As for the most recent
    suggestion, it fails for me:

    Traceback (most recent call
    last):
    File "F:/___zInsulin Code
    A 08-02-23/WinPic/IOWw.pyw",
    line 14, in <module>
    print("Ww Outside = <"
    + str(Ww) > + ">")
    TypeError: bad operand type
    for unary +: 'str'

    It fails because there's a
    mistake. It should be:

    print("Ww Outside = <" +
    str(Ww) + ">")

    With the need to close the
    window, it adds an extra step
    and intervention to the
    program to use. I am not sure
    how this help[s.

    As a curio, it would be
    interesting to see how to use
    the value of a variable,
    created in the function used
    here, and make it available to
    the code outside the function.

    [snip]


    --
    https://mail.python.org/mailma
    n/listinfo/python-list


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Steve GS@3:633/280.2 to All on Mon Feb 26 18:56:35 2024

    Musta misstit....
    I had thought of that before I
    started the discussion but
    figured it would take more
    code than it finally needed.
    I guess I was also
    variable-dependent thinking
    that I would need the result
    elsewhere in the code. So
    far, I see that I don't need
    the value.

    Then there is that discovery
    element: Why is my original
    idea not working? I still
    cannot pass the value back
    from the function. What is
    different about this function
    that others would have given
    me the value?


    SGA

    -----Original Message-----
    From: Python-list
    <python-list-bounces+gronicus=
    sga.ninja@python.org> On
    Behalf Of Thomas Passin via
    Python-list
    Sent: Sunday, February 25,
    2024 5:55 PM
    To: python-list@python.org
    Subject: Re: Problem resizing
    a window and button placement

    On 2/25/2024 4:19 PM, Steve GS
    via Python-list wrote:
    SOLUTION FOUND!

    The fix was to write the
    code that uses the width value
    and to place it into the
    function itself.
    Kluge? Maybe but it works.

    Right, just what I wrote
    earlier:

    "have the function that
    responds to the resize event
    perform the action that you
    want"

    Mischief Managed.

    ========================
    As for the most recent
    suggestion, it fails for me:

    Traceback (most recent call
    last):
    File "F:/___zInsulin Code
    A 08-02-23/WinPic/IOWw.pyw",
    line 14, in <module>
    print("Ww Outside = <"
    + str(Ww) > + ">")
    TypeError: bad operand type
    for unary +: 'str'

    With the need to close the
    window, it adds an extra step
    and intervention to the
    program to use. I am not sure
    how this help[s.

    As a curio, it would be
    interesting to see how to use
    the value of a variable,
    created in the function used
    here, and make it available to
    the code outside the function.



    SGA

    -----Original Message-----
    From: Alan Gauld
    <learn2program@gmail.com>
    Sent: Sunday, February 25,
    2024 12:44 PM
    To: Steve GS
    <Gronicus@SGA.Ninja>;
    python-list@python.org
    Subject: Re: RE: Problem
    resizing a window and button
    placement

    On 25/02/2024 03:58, Steve
    GS via Python-list wrote:
    import tkinter as tk

    Ww = None

    def on_configure(*args):
    global Ww
    Ww =
    root.winfo_width()
    print("Ww Inside =
    <" + str(Ww) + ">")

    root = tk.Tk()
    root.bind('<Configure>',
    on_configure)
    root.mainloop()

    print("Ww Outside = <" +
    str(Ww) > + ">")

    Produces:
    Ww Inside = <200>
    Ww Inside = <200>
    Ww Inside = <205>
    Ww Inside = <205>
    Ww Inside = <206>
    Ww Inside = <206>
    Ww Outside = <206>

    HTH


    --
    https://mail.python.org/mailma
    n/listinfo/python-list


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Alan Gauld@3:633/280.2 to All on Mon Feb 26 20:03:57 2024
    On 26/02/2024 07:56, Steve GS via Python-list wrote:

    Then there is that discovery
    element: Why is my original
    idea not working? I still
    cannot pass the value back
    from the function. What is
    different about this function
    that others would have given
    me the value?

    There is nothing different, see the code below.
    print() is a function like any other.
    In this case it is called after you close the
    window, ie after mainloop() exits.
    But any other function called inside
    mainloop - eg any other event handler can
    also access it.

    For example, if you added a button:

    def printW(): print("Button Ww = ", Ww)

    bw = tk.Button(root, text="Print Width", command=printW)
    bw.pack()

    You would be able to print the value on demand.

    import tkinter as tk

    Ww = None

    def on_configure(*args):
    global Ww
    Ww = root.winfo_width()
    print("Ww Inside =<"+str(Ww)+">")

    root = tk.Tk()
    root.bind('<Configure>',on_configure)
    root.mainloop()

    print("Ww Outside = <"+str(Ww)+">")

    --
    Alan G
    Author of the Learn to Program web site
    http://www.alan-g.me.uk/
    http://www.amazon.com/author/alan_gauld
    Follow my photo-blog on Flickr at:
    http://www.flickr.com/photos/alangauldphotos



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Steve GS@3:633/280.2 to All on Mon Feb 26 22:02:59 2024
    Although your code produces the value of Ww outside the function, I do =
    not see how I can use the value of Ww unless I close the program.

    import tkinter as tk

    Ww =3D None # What does this do? Why not Integer?
    WwZ =3D None

    def on_configure(*args):
    global Ww
    global WwZ
    Ww =3D root.winfo_width()
    print("9 Ww Inside =3D<"+str(Ww)+">") # works
    WwZ =3D Ww * 2
    print("11 WwZ Inside =3D<"+str(WwZ)+">") # works
    return(Ww) #Can I use this?
    =20
    root =3D tk.Tk()
    root.bind('<Configure>',on_configure)
    print("15 Ww Inside1 =3D <"+str(Ww)+">")
    #Ww2 =3D int(Ww) * 2 # fails
    print("17 WwZ Inside2 =3D <"+str(WwZ)+">")

    root.mainloop()

    Ww2 =3D int(Ww) * 2 #Works but only after the program stops
    print("21 Ww Outside2 =3D <"+str(WwZ)+">")
    # Can I have concentric loops?


    SGA

    -----Original Message-----
    From: Alan Gauld <learn2program@gmail.com>=20
    Sent: Monday, February 26, 2024 4:04 AM
    To: Steve GS <Gronicus@SGA.Ninja>; python-list@python.org
    Subject: Re: RE: Problem resizing a window and button placement

    On 26/02/2024 07:56, Steve GS via Python-list wrote:

    Then there is that discovery
    element: Why is my original
    idea not working? I still
    cannot pass the value back
    from the function. What is
    different about this function
    that others would have given
    me the value?

    There is nothing different, see the code below.
    print() is a function like any other.
    In this case it is called after you close the window, ie after =
    mainloop() exits.
    But any other function called inside
    mainloop - eg any other event handler can also access it.

    For example, if you added a button:

    def printW(): print("Button Ww =3D ", Ww)

    bw =3D tk.Button(root, text=3D"Print Width", command=3DprintW)
    bw.pack()

    You would be able to print the value on demand.

    import tkinter as tk

    Ww =3D None

    def on_configure(*args):
    global Ww
    Ww =3D root.winfo_width()
    print("Ww Inside =3D<"+str(Ww)+">")

    root =3D tk.Tk()
    root.bind('<Configure>',on_configure)
    root.mainloop()

    print("Ww Outside =3D <"+str(Ww)+">")

    --
    Alan G
    Author of the Learn to Program web site
    http://www.alan-g.me.uk/
    http://www.amazon.com/author/alan_gauld
    Follow my photo-blog on Flickr at:
    http://www.flickr.com/photos/alangauldphotos



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Alan Gauld@3:633/280.2 to All on Mon Feb 26 23:04:26 2024
    On 26/02/2024 11:02, Steve GS via Python-list wrote:
    Although your code produces the value of Ww outside the function,
    I do not see how I can use the value of Ww unless I close the program.

    You have to use a function that operates inside the mainloop.
    Thats the nature of event driven programs, all activity happens
    inside the mainloop except initialisation and cleanup.

    So you need to create an event handler as I described below.

    Here is the complete program including the button:

    ###########################
    import tkinter as tk

    Ww=None

    def on_configure(*args):
    global Ww
    Ww = root.winfo_width()
    print("Ww Inside = <" + str(Ww) + ">")

    def printW(): print("Button Ww = ", Ww)


    root = tk.Tk()
    bw = tk.Button(root, text="Print Width", command=printW)
    bw.pack()
    root.bind('<Configure>', on_configure)
    root.mainloop()

    print("Ww Outside = <" + str(Ww) + ">")
    ############################

    Notice that the button callback picks up the latest value of Ww.

    --
    Alan G
    Author of the Learn to Program web site
    http://www.alan-g.me.uk/
    http://www.amazon.com/author/alan_gauld
    Follow my photo-blog on Flickr at:
    http://www.flickr.com/photos/alangauldphotos



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Thomas Passin@3:633/280.2 to All on Tue Feb 27 00:34:19 2024
    On 2/26/2024 6:02 AM, Steve GS via Python-list wrote:
    Although your code produces the value of Ww outside the function, I do not see how I can use the value of Ww unless I close the program.

    The configuration event hasn't fired at the time you include the print statement in the handler's def block, and therefore the print function
    inside your handler hasn't invoked. It won't be invoked until you
    resize the window.

    There is no point to saving the width and height outside your
    on_configure() function, because outside that function you can't know if
    they have been changed. There could even have been a race condition
    where you use one but the other changes before you get around to using
    it. It's better just to ask tk for the values whenever you need them,
    as you do inside your handler.

    import tkinter as tk

    Ww = None # What does this do? Why not Integer?
    WwZ = None
    # These could be integers, like 0, but that would not be the correct
    # window sizes at that point. The window is either not constructed or it
    # has some definite size that is not zero.

    def on_configure(*args):
    global Ww
    global WwZ
    Ww = root.winfo_width()
    print("9 Ww Inside =<"+str(Ww)+">") # works
    WwZ = Ww * 2
    print("11 WwZ Inside =<"+str(WwZ)+">") # works
    return(Ww) #Can I use this?

    root = tk.Tk()
    root.bind('<Configure>',on_configure)
    print("15 Ww Inside1 = <"+str(Ww)+">")
    #Ww2 = int(Ww) * 2 # fails
    print("17 WwZ Inside2 = <"+str(WwZ)+">")

    root.mainloop()

    Ww2 = int(Ww) * 2 #Works but only after the program stops
    print("21 Ww Outside2 = <"+str(WwZ)+">")
    # Can I have concentric loops?


    SGA

    -----Original Message-----
    From: Alan Gauld <learn2program@gmail.com>
    Sent: Monday, February 26, 2024 4:04 AM
    To: Steve GS <Gronicus@SGA.Ninja>; python-list@python.org
    Subject: Re: RE: Problem resizing a window and button placement

    On 26/02/2024 07:56, Steve GS via Python-list wrote:

    Then there is that discovery
    element: Why is my original
    idea not working? I still
    cannot pass the value back
    from the function. What is
    different about this function
    that others would have given
    me the value?

    There is nothing different, see the code below.
    print() is a function like any other.
    In this case it is called after you close the window, ie after mainloop() exits.
    But any other function called inside
    mainloop - eg any other event handler can also access it.

    For example, if you added a button:

    def printW(): print("Button Ww = ", Ww)

    bw = tk.Button(root, text="Print Width", command=printW)
    bw.pack()

    You would be able to print the value on demand.

    import tkinter as tk

    Ww = None

    def on_configure(*args):
    global Ww
    Ww = root.winfo_width()
    print("Ww Inside =<"+str(Ww)+">")

    root = tk.Tk()
    root.bind('<Configure>',on_configure)
    root.mainloop()

    print("Ww Outside = <"+str(Ww)+">")

    --
    Alan G
    Author of the Learn to Program web site
    http://www.alan-g.me.uk/
    http://www.amazon.com/author/alan_gauld
    Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos




    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Steve GS@3:633/280.2 to All on Tue Feb 27 18:13:19 2024
    The configuration event
    hasn't fired at the time you
    include the print statement in
    the handler's def block, and
    therefore the print function
    inside your handler hasn't
    invoked. It won't be invoked
    until you resize the window.

    Exactly....

    There is no point (really?)
    to saving the width and height
    outside your
    on_configure() function,
    because outside that function
    you can't know if they have
    been changed. There could
    even have been a race
    condition where you use one
    but the other changes before
    you get around to using it.

    Aside from using it to resized
    the window, is there no way to
    know the last value of the
    change for use in the program?
    I could write the value to a
    label and read it back later
    in the process but that sounds
    to be klugy.

    It's better just to ask tk
    for the values whenever you
    need them, as you do inside
    your handler.

    How would that be done?



    SGA

    -----Original Message-----
    From: Python-list
    <python-list-bounces+gronicus=
    sga.ninja@python.org> On
    Behalf Of Thomas Passin via
    Python-list
    Sent: Monday, February 26,
    2024 8:34 AM
    To: python-list@python.org
    Subject: Re: Problem resizing
    a window and button placement

    On 2/26/2024 6:02 AM, Steve GS
    via Python-list wrote:
    Although your code produces
    the value of Ww outside the
    function, I do not see how I
    can use the value of Ww unless
    I close the program.

    The configuration event hasn't
    fired at the time you include
    the print statement in the
    handler's def block, and
    therefore the print function
    inside your handler hasn't
    invoked. It won't be invoked
    until you resize the window.

    There is no point to saving
    the width and height outside
    your
    on_configure() function,
    because outside that function
    you can't know if they have
    been changed. There could
    even have been a race
    condition where you use one
    but the other changes before
    you get around to using it.
    It's better just to ask tk for
    the values whenever you need
    them, as you do inside your
    handler.

    import tkinter as tk

    Ww = None # What does this
    do? Why not Integer?
    WwZ = None
    # These could be integers,
    like 0, but that would not be
    the correct # window sizes at
    that point. The window is
    either not constructed or it #
    has some definite size that is
    not zero.

    def on_configure(*args):
    global Ww
    global WwZ
    Ww =
    root.winfo_width()
    print("9 Ww Inside
    =<"+str(Ww)+">") # works
    WwZ = Ww * 2
    print("11 WwZ
    Inside =<"+str(WwZ)+">") #
    works
    return(Ww) #Can I
    use this?

    root = tk.Tk()

    root.bind('<Configure>',on_con
    figure)
    print("15 Ww Inside1 =
    <"+str(Ww)+">")
    #Ww2 = int(Ww) * 2 # fails
    print("17 WwZ Inside2 =
    <"+str(WwZ)+">")

    root.mainloop()

    Ww2 = int(Ww) * 2 #Works
    but only after the program
    stops
    print("21 Ww Outside2 =
    <"+str(WwZ)+">") # Can I have
    concentric
    loops?


    SGA

    -----Original Message-----
    From: Alan Gauld
    <learn2program@gmail.com>
    Sent: Monday, February 26,
    2024 4:04 AM
    To: Steve GS
    <Gronicus@SGA.Ninja>;
    python-list@python.org
    Subject: Re: RE: Problem
    resizing a window and button
    placement

    On 26/02/2024 07:56, Steve
    GS via Python-list wrote:

    Then there is that
    discovery
    element: Why is my original
    idea not working? I still
    cannot pass the value back
    from the function. What is
    different about this
    function
    that others would have
    given
    me the value?

    There is nothing different,
    see the code below.
    print() is a function like
    any other.
    In this case it is called
    after you close the window, ie
    after mainloop() exits.
    But any other function
    called inside
    mainloop - eg any other
    event handler can also access
    it.

    For example, if you added a
    button:

    def printW(): print("Button
    Ww = ", Ww)

    bw = tk.Button(root,
    text="Print Width",
    command=printW)
    bw.pack()

    You would be able to print
    the value on demand.

    import tkinter as tk

    Ww = None

    def on_configure(*args):
    global Ww
    Ww =
    root.winfo_width()
    print("Ww Inside
    =<"+str(Ww)+">")

    root = tk.Tk()

    root.bind('<Configure>',on_con
    figure)
    root.mainloop()

    print("Ww Outside =
    <"+str(Ww)+">")

    --
    Alan G
    Author of the Learn to
    Program web site
    http://www.alan-g.me.uk/

    http://www.amazon.com/author/a
    lan_gauld
    Follow my photo-blog on
    Flickr at:

    http://www.flickr.com/photos/a
    langauldphotos



    --
    https://mail.python.org/mailma
    n/listinfo/python-list


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Alan Gauld@3:633/280.2 to All on Tue Feb 27 23:15:14 2024
    On 27/02/2024 07:13, Steve GS via Python-list wrote:

    Aside from using it to resized
    the window, is there no way to
    know the last value of the
    change for use in the program?

    The last value would be the current width.
    And you know how to get that as shown in
    your configure function:

    Ww = root.winfo_width()

    I could write the value to a
    label and read it back later

    That's no different to writing it to
    global Ww and accessing that as demonstrated
    in my last code post (with button).

    It's better just to ask tk
    for the values whenever you
    need them, as you do inside
    your handler.

    How would that be done?

    Ww = root.winfo_width()

    Provided you can access the root widget
    (which is (nearly?) always true) you
    can get the width of the main window.

    But can I ask if you have a particular use-case
    in mind for this? We started out talking about
    relocating some widgets when the window was
    resized. We established that the best place
    to do that was inside the configure event
    handler, with no need to store the width.
    (assuming you aren't using a dynamic layout
    manager(grid/pack/form) which would be better
    still!)

    We've now moved on to the more general issue
    of communicating values between event handlers
    (although still using the width as our exemplar).
    Is this just academic interest or do you have
    a specific need for this? If we know the need
    we might be able to suggest a specific (and
    possibly better?)solution.

    --
    Alan G
    Author of the Learn to Program web site
    http://www.alan-g.me.uk/
    http://www.amazon.com/author/alan_gauld
    Follow my photo-blog on Flickr at:
    http://www.flickr.com/photos/alangauldphotos



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)