shift widgets at center

68 views
Skip to first unread message

Degenerate Tech

unread,
Mar 30, 2024, 3:17:11 AMMar 30
to Kivy users support
# how to shift all widets at center ?
please see the code . i want this optimize way without adding many widgets .
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder


KV='''
ScrollView:
GridLayout:
cols:2
size_hint_y:None
height:self.minimum_height
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:
MyButton:

<MyButton@Button>:
size_hint:None,None
size:'60dp','60dp'

'''


class MyApp(App):
def build(self):
return Builder.load_string(KV)



if __name__=='__main__':
MyApp().run()

WS

unread,
Mar 30, 2024, 10:42:51 AMMar 30
to Kivy users support
see if it works


from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder


KV='''
FloatLayout:
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
size: self.size
pos: self.pos

ScrollView:
pos_hint: {'center_x': .5}
size_hint_x: None
width: grid.width
GridLayout:
id: grid
pos_hint: {'center_x': .5, 'center_y': .5}
#size_hint:None,None
#size:'60dp','60dp'

'''


class MyApp(App):
def build(self):
return Builder.load_string(KV)



if __name__=='__main__':
MyApp().run()

Degenerate Tech

unread,
Mar 30, 2024, 11:43:39 AMMar 30
to Kivy users support
i want
cols=2
but  shows
all widgets in one column only .

ElliotG

unread,
Mar 30, 2024, 2:41:10 PMMar 30
to Kivy users support
Let me know if this is what you are looking for.
The 'trick' is to but the object that you are animating into a RelativeLayout, so it becomes easy to manipulate the pos.  A widget in a GridLayout is sized and positioned by the grid.

from kivy.app import App
from kivy.lang import Builder
from kivy.animation import Animation
from kivy.uix.relativelayout import RelativeLayout
from kivy.metrics import dp
from kivy.utils import get_random_color
from kivy.properties import ColorProperty, ListProperty


kv = """
BoxLayout:
    orientation: 'vertical'
    spacing: dp(2)
    ScrollView:

        GridLayout:
            id: grid
            cols:2
            spacing: '2dp'
            size_hint_y:None
            height:self.minimum_height
    Button:
        size_hint_y: None
        height: dp(48)
        text: 'grow'
        on_release: app.grow_image()

<MyButton>:

    size_hint:None,None
    size:'60dp','60dp'
    Widget:
        id: w

        size_hint:None,None
        size:'60dp','60dp'
        canvas:
            Color:
                rgba: root.bg_color
            Rectangle:
                size: root.bg_size
                pos: root.bg_pos
"""


class MyButton(RelativeLayout):
    bg_color = ColorProperty()
    bg_size = ListProperty([dp(60), dp(60)])
    bg_pos = ListProperty([0, 0])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.anim_grow = (Animation(bg_size=(dp(62), dp(62)), bg_pos=(dp(-1), dp(-1)), duration=0.5) +
                          Animation(bg_size=(dp(60), dp(60)), bg_pos=(0, 0), duration=0.5))

    def grow(self):
        self.anim_grow.start(self)


class MyApp(App):
    def build(self):
        return Builder.load_string(kv)

    def on_start(self):
        for _ in range(22):
            self.root.ids.grid.add_widget(MyButton(bg_color=get_random_color()))

    def grow_image(self):
        for w in self.root.ids.grid.children:
            w.grow()


if __name__ == '__main__':
    MyApp().run()

Chisoul Munthali

unread,
Mar 30, 2024, 2:41:35 PMMar 30
to kivy-...@googlegroups.com
What desired result you want to achieve and in what context e.g in scrollview or other layout widgets like anchorlayout, boxlayout etc... the code provided gives little to no clue


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/b97661b1-7093-4f96-9152-5f8d63a6fc8dn%40googlegroups.com.

Degenerate Tech

unread,
Mar 30, 2024, 11:35:14 PMMar 30
to Kivy users support
@ElliotG
Your solution is not  in my this context of problem .

Degenerate Tech

unread,
Mar 30, 2024, 11:38:33 PMMar 30
to Kivy users support
i want to shift center widgets which in gridlayout .

Elliot Garbus

unread,
Mar 30, 2024, 11:54:47 PMMar 30
to kivy-...@googlegroups.com
Run the code. They are in a GridLayout. 
If there is an issue, be specific. 

Sent from my iPhone

On Mar 30, 2024, at 8:38 PM, Degenerate Tech <sksah...@gmail.com> wrote:

i want to shift center widgets which in gridlayout .

Degenerate Tech

unread,
Mar 31, 2024, 12:00:05 AMMar 31
to Kivy users support
In your code all widgets are in left side . i want to widgets should be in middle of grid layout .

elli...@cox.net

unread,
Mar 31, 2024, 12:29:39 AMMar 31
to kivy-...@googlegroups.com
The code has 2 columns,  on the left, just like yours.  Do you want the 2 column gridlayout in the center of  Window?


From: kivy-...@googlegroups.com <kivy-...@googlegroups.com> on behalf of Degenerate Tech <sksah...@gmail.com>
Sent: Saturday, March 30, 2024 9:00 PM
To: Kivy users support <kivy-...@googlegroups.com>
Subject: Re: [kivy-users] shift widgets at center
 

ElliotG

unread,
Mar 31, 2024, 12:34:53 AMMar 31
to Kivy users support
Here the columns are in the middle:

from kivy.app import App

from kivy.lang import Builder
from kivy.animation import Animation
from kivy.uix.relativelayout import RelativeLayout
from kivy.metrics import dp
from kivy.utils import get_random_color
from kivy.properties import ColorProperty, ListProperty


kv = """
BoxLayout:
    orientation: 'vertical'
    spacing: dp(2)
    ScrollView:
        BoxLayout:
            size_hint_y: None
            height: self.minimum_height
            Widget:

            GridLayout:
                id: grid
                cols:2
                spacing: '2dp'
                size_hint: None, None
                size:self.minimum_size
            Widget

           
    Button:
        size_hint_y: None
        height: dp(48)
        text: 'grow'
        on_release: app.grow_image()

<MyButton>:
    size_hint:None,None
    size:'60dp','60dp'
    Widget:
        id: w

        size_hint:None,None
        size:'60dp','60dp'
        canvas:
            Color:
                rgba: root.bg_color
            Rectangle:
                size: root.bg_size
                pos: root.bg_pos
"""


class MyButton(RelativeLayout):
    bg_color = ColorProperty()
    bg_size = ListProperty([dp(60), dp(60)])
    bg_pos = ListProperty([0, 0])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.anim_grow = (Animation(bg_size=(dp(62), dp(62)), bg_pos=(dp(-1), dp(-1)), duration=0.5) +
                          Animation(bg_size=(dp(60), dp(60)), bg_pos=(0, 0), duration=0.5))

    def grow(self):
        self.anim_grow.start(self)


class MyApp(App):
    def build(self):

        return Builder.load_string(kv)

    def on_start(self):
        for _ in range(22):
            self.root.ids.grid.add_widget(MyButton(bg_color=get_random_color()))

    def grow_image(self):
        for w in self.root.ids.grid.children:
            w.grow()


if __name__ == '__main__':
    MyApp().run()

Degenerate Tech

unread,
Mar 31, 2024, 1:08:39 AMMar 31
to Kivy users support
oh !! yes . it was so easy . i don't know why it is not came in mind !! my brain is very slow!!  😖 😖😖
Reply all
Reply to author
Forward
0 new messages