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.
Python, Tkinter: How do Ibeen
determine if a window has
resized? I want to locateknow
buttons vertically along the
right border and need to
the new width. The buttonsare
to move with the change of
location of the right-side
border.
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?
On 24 Feb 2024, at 04:36,Steve GS via Python-list
How do I extract the values
from args?
width
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
Well, yes, in Python a variable created inside a function or method is
local to that function unless you declare it global.
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.
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, I ran that elegantlycode
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
Vwidth = rootV.winfo_width()width
and it also reports the
as I resize the window.use
However, I cannot seem to
the variable Vwidth outsideacting
the sub routine. It is
as if Vwidth is not globalbut
I added that. It isreported
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-list@python.orgresizing
Subject: Re: Problem
a window and buttonplacement
isOn 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
printed to learn what is in
the args.
Barry
"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?
"Well, yes, in Python ato
variable created inside a
function or method is local
that function unless youfunctions,
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
I can 'return' the variablenot
but that apparently would
work for this function.
Is this type of function any
different that that which I
have been using?
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 ato
variable created inside a
function or method is local
that function unless youfunctions,
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
I can 'return' the variablenot
but that apparently would
work for this function.Please post a short example
Is this type of function any
different that that which I
have been using?
that shows the problem.
import tkinter as tk'Ww' won't exist until 'on_configure' assigns to it, and that won't
#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()
import tkinter as tkroot.winfo_width()
#global Ww Neither global
helps
def on_configure(*args):
# print(args)
#global Ww Neither
global helps
Ww =
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
sga.ninja@python.org> OnPython-list
Behalf Of MRAB via
Sent: Saturday, February 24,resizing
2024 7:49 PM
To: python-list@python.org
Subject: Re: Problem
a window and buttonplacement
On 2024-02-25 00:33, SteveGS
via Python-list wrote:to
"Well, yes, in Python ato
variable created inside a
function or method is local
that function unless you
declare it global."
Yes, I knew that. I tried
it.global it both before the
function call and within
itSame for when I created the
variable. If I try to use
anyin the rest of the code, itfunctions,
keeps coming up as not
declared. In other
I can 'return' the variablenot
but that apparently would
work for this function.
Is this type of function
different that that which IPlease post a short example
have been using?
that shows the problem.
import tkinter as tkstr(Ww)=20
#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 <" + =
+ ">")'Ww' won't exist until 'on_configure' assigns to it, and that won't =
#NameError: name 'Ww' is not defined
root.mainloop()
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'
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
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'
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.
SOLUTION FOUND!code that uses the width value
The fix was to write the
Kluge? Maybe but it works.suggestion, it fails for me:
Mischief Managed.
========================
As for the most recent
Traceback (most recent calllast):
File "F:/___zInsulin CodeA 08-02-23/WinPic/IOWw.pyw",
print("Ww Outside = <"+ str(Ww) > + ">")
TypeError: bad operand typefor unary +: 'str'
With the need to close thewindow, it adds an extra step
As a curio, it would beinteresting to see how to use
SOLUTION FOUND!code that uses the width value
The fix was to write the
Kluge? Maybe but it works.
Mischief Managed.suggestion, it fails for me:
========================
As for the most recent
Traceback (most recent calllast):
File "F:/___zInsulin CodeA 08-02-23/WinPic/IOWw.pyw",
print("Ww Outside = <"+ str(Ww) > + ">")
TypeError: bad operand typefor unary +: 'str'
With the need to close thewindow, it adds an extra step
As a curio, it would beinteresting to see how to use
SGA<learn2program@gmail.com>
-----Original Message-----
From: Alan Gauld
Sent: Sunday, February 25,2024 12:44 PM
To: Steve GS<Gronicus@SGA.Ninja>;
Subject: Re: RE: Problemresizing a window and button
On 25/02/2024 03:58, SteveGS via Python-list wrote:
import tkinter as tkroot.winfo_width()
Ww = None
def on_configure(*args):
global Ww
Ww =
print("Ww Inside =<" + str(Ww) + ">")
root = tk.Tk()on_configure)
root.bind('<Configure>',
root.mainloop()str(Ww) > + ">")
print("Ww Outside = <" +
Produces:
Ww Inside = <200>
Ww Inside = <200>
Ww Inside = <205>
Ww Inside = <205>
Ww Inside = <206>
Ww Inside = <206>
Ww Outside = <206>
HTH
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?
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)+">")
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?
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)+">")
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.
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# These could be integers, like 0, but that would not be the correct
Ww = None # What does this do? Why not Integer?
WwZ = None
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
hasn't fired at the time youThe configuration event
to saving the width and heightThere is no point (really?)
for the values whenever youIt's better just to ask tk
Although your code producesthe value of Ww outside the
import tkinter as tkdo? Why not Integer?
Ww = None # What does this
WwZ = None# These could be integers,
def on_configure(*args):root.winfo_width()
global Ww
global WwZ
Ww =
print("9 Ww Inside=<"+str(Ww)+">") # works
WwZ = Ww * 2Inside =<"+str(WwZ)+">") #
print("11 WwZ
return(Ww) #Can Iuse this?
root = tk.Tk()
print("15 Ww Inside1 =<"+str(Ww)+">")
#Ww2 = int(Ww) * 2 # fails<"+str(WwZ)+">")
print("17 WwZ Inside2 =
root.mainloop()but only after the program
Ww2 = int(Ww) * 2 #Works
print("21 Ww Outside2 =<"+str(WwZ)+">") # Can I have
loops?
SGA<learn2program@gmail.com>
-----Original Message-----
From: Alan Gauld
Sent: Monday, February 26,2024 4:04 AM
To: Steve GS<Gronicus@SGA.Ninja>;
Subject: Re: RE: Problemresizing a window and button
On 26/02/2024 07:56, SteveGS via Python-list wrote:
discoveryThen there is that
functionelement: Why is my original
idea not working? I still
cannot pass the value back
from the function. What is
different about this
giventhat others would have
see the code below.me the value?
There is nothing different,
print() is a function likeany other.
In this case it is calledafter you close the window, ie
But any other functioncalled inside
mainloop - eg any otherevent handler can also access
For example, if you added abutton:
def printW(): print("ButtonWw = ", Ww)
bw = tk.Button(root,text="Print Width",
bw.pack()the value on demand.
You would be able to print
root.winfo_width()import tkinter as tk
Ww = None
def on_configure(*args):
global Ww
Ww =
=<"+str(Ww)+">")print("Ww Inside
root.bind('<Configure>',on_con
root = tk.Tk()
<"+str(Ww)+">")root.mainloop()
print("Ww Outside =
--Program web site
Alan G
Author of the Learn to
Follow my photo-blog onFlickr at:
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
for the values whenever youIt's better just to ask tk
need them, as you do inside
your handler.
How would that be done?
Sysop: | Tetrazocine |
---|---|
Location: | Melbourne, VIC, Australia |
Users: | 6 |
Nodes: | 8 (0 / 8) |
Uptime: | 47:43:51 |
Calls: | 45 |
Files: | 21,492 |
Messages: | 63,554 |