2

The screen has multiple layouts all child of a box layout. I am trying to add a button in the last sub layout (grid-layout) when a button in other layout is clicked. The code does not crash but nothing happens too. I want to add buttons to the layout with id drinkslayout when a button is clicked eg pepsi,sprite

Python:

class MainScreen(Screen):
    pass

class AnotherScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass
class Stacks(StackLayout):
    pass


present=Builder.load_file('hellokivy2.kv')  #Specifying location of kv file     

class MainApp(App):

    def build(self):
        return present
    def drinksSelect(self,value): #creating a button by referring the id of the layout in which to create button
        print(value)
        yolo = AnotherScreen()
        yolo2 = yolo.ids.newButtonLayout
        button = Button(text="value",size_hint= (0.2,0.4))
        yolo2.add_widget(button)


if __name__ == "__main__":
    MainApp().run()

KV

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    AnotherScreen:

<MainScreen>:
name: "main"

canvas.before:
    Color:
        rgba: 1, 1, 1, 1
    Rectangle:
        pos: self.pos
        size: self.size

BoxLayout:

    orientation: 'vertical'
    size:root.size

    Button:
        text: "Slideshow"

    GridLayout:

        cols: 2

        Button:
            text: "Burger"


        Button:
            text: "Drinks"
            on_release: app.root.current = "other"

        Button:
            text: "Fries"

        Button:
            text: "Others"
    Label:
        text: "Your Cart"
        font_size: 40
        color:(0,0,0,1)
        size_hint_y: None
        canvas.before:
            Color:
                rgba:150,10,200,0.5
            Rectangle:
                pos: self.pos
                size: self.size
<AnotherScreen>:
    name: "other"
    canvas.before:
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

BoxLayout:
    size:root.size
    orientation: 'vertical'
    GridLayout:
        size_hint_y:0.1
        orientation: 'vertical'
        rows: 1
        Button:
            color: 1,1,1,1
            font_size: 25
            size_hint_y:0.1
            text: "Back Home"
            on_release: app.root.current = "main"
        Label:
            text: "Project BAM"
            canvas.before:
                Color:
                    rgba: 0,0,0,1
                Rectangle:
                    pos: self.pos
                    size: self.size
        Button:
            color: 1,1,1,1
            font_size: 25
            size_hint_y:0.1
            text: "Profile"
            on_release: app.root.current = "main"
    Button:
        color: 1,1,1,1
        font_size: 25
        size_hint_y:0.9
        text: "Whats New"
        on_release: app.root.current = "main"

    GridLayout:
        rows: 2
        Button:
            text: "Pepsi"
            on_release: app.drinksSelect(1)

        Button:
            text: "7up"
            on_release: app.drinksSelect(2)
        Button:
            text: "Fanta"
            on_release: app.drinksSelect(3)
        Button:
            text: "Mountain Dew"
            on_release: app.drinksSelect(4)
        Button:
            text: "Diet Pepsi"
            on_release: app.drinksSelect(5)
        Button:
            text: "Sprite"
            on_release: app.drinksSelect(6)

    GridLayout:
        id: drinksLayout
        size_hint_y: 0.3
        orientation: 'horizontal'
        rows: 1

        Button:
            id: label1
            size_hint: 0.2,0.4
            background_normal:'1.jpg'
            text: 'B1'
        Button:
            id: label2
            size_hint: 0.2,0.4
            background_normal:'1.jpg'
            text: 'B1'
        Button:
            id: label3
            size_hint: 0.2,0.4
            background_normal:'1.jpg'
            text: 'B1'
4
  • Is it all your kv code ? Commented Oct 27, 2017 at 23:33
  • not all just part of it Commented Oct 27, 2017 at 23:50
  • we need more of it to help you Commented Oct 27, 2017 at 23:51
  • Well now i have added the entire Kivy code Commented Oct 28, 2017 at 4:27

1 Answer 1

2

Ok the problem was your drinksSelect method, also you need to keep the value of your screens

Try this:

py:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.properties import ObjectProperty


class MainScreen(Screen):
    pass


class AnotherScreen(Screen):
    dl = ObjectProperty()
    l = 0
    limit = 3 # Fix your limit here 3 is an example


class ScreenManagement(ScreenManager):
    m_s = ObjectProperty() # Here I will keep the value of the MainScreen
    a_s = ObjectProperty() # Here the AnotherScreen so I can use them later


class Stacks(StackLayout):
    pass


present = Builder.load_file('hellokivy2.kv')  # Specifying location of kv file


class MainApp(App):

    def build(self):
        return present

    def drinksSelect(self,value):  # creating a button by referring the id of the layout in which to create button
        print(value)
        if self.root.a_s.l < self.root.a_s.limit: # You know what I mean
            button = Button(text=str(value), size_hint= (0.2,0.4))
            self.root.a_s.ids['place_remaining'].add_widget(button)
            self.root.a_s.l += 1

if __name__ == "__main__":
    MainApp().run()

kv:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    m_s: m_s # so I can use it in the .py file
    a_s:a_s # same here
    transition: FadeTransition()
    MainScreen:
        id: m_s
    AnotherScreen:
        id: a_s

<MainScreen>:
    name: "main"

    canvas.before:
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:

        orientation: 'vertical'
        size:root.size

        Button:
            text: "Slideshow"

        GridLayout:

            cols: 2

            Button:
                text: "Burger"


            Button:
                text: "Drinks"
                on_release: app.root.current = "other"

            Button:
                text: "Fries"

            Button:
                text: "Others"
        Label:
            text: "Your Cart"
            font_size: 40
            color:(0,0,0,1)
            size_hint_y: None
            canvas.before:
                Color:
                    rgba:150,10,200,0.5
                Rectangle:
                    pos: self.pos
                    size: self.size
<AnotherScreen>:
    name: "other"
    dl : drinksLayout
    canvas.before:
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:
        size:root.size
        orientation: 'vertical'
        GridLayout:
            size_hint_y:0.1
            orientation: 'vertical'
            rows: 1
            Button:
                color: 1,1,1,1
                font_size: 25
                size_hint_y:0.1
                text: "Back Home"
                on_release: app.root.current = "main"
            Label:
                text: "Project BAM"
                canvas.before:
                    Color:
                        rgba: 0,0,0,1
                    Rectangle:
                        pos: self.pos
                        size: self.size
            Button:
                color: 1,1,1,1
                font_size: 25
                size_hint_y:0.1
                text: "Profile"
                on_release: app.root.current = "main"
        Button:
            color: 1,1,1,1
            font_size: 25
            size_hint_y:0.9
            text: "Whats New"
            on_release: app.root.current = "main"

        GridLayout:
            rows: 2
            Button:
                text: "Pepsi"
                on_release: app.drinksSelect(1)

            Button:
                text: "7up"
                on_release: app.drinksSelect(2)
            Button:
                text: "Fanta"
                on_release: app.drinksSelect(3)
            Button:
                text: "Mountain Dew"
                on_release: app.drinksSelect(4)
            Button:
                text: "Diet Pepsi"
                on_release: app.drinksSelect(5)
            Button:
                text: "Sprite"
                on_release: app.drinksSelect(6)

        GridLayout:
            id: drinksLayout
            size_hint_y: 0.3
            orientation: 'horizontal'
            rows: 1
            GridLayout:
                id: place_remaining
                rows: 1
                size_hint_x: 80
            Button:
                id: label1
                width: 40 # Set the size_hint_x to None and then set the width if you want a fixed dimension
                size_hint: None,0.4
                background_normal:'1.jpg'
                text: 'B1'
            Button:
                id: label2
                width: 40
                size_hint: None,0.4
                background_normal:'1.jpg'
                text: 'B1'
            Button:
                id: label3
                width: 40
                size_hint: None,0.4
                background_normal:'1.jpg'
                text: 'B1'

Outputs:

first

second

I hope that the code is a bit clear now

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Is there a way to limit the number of boxes made and fix the dimensions of button b1?
and can you please explain the code a bit more. I am new and dont know kivy
thanks again. one last thing can you tell how to position the b1 button to the right
Yes of course I've edited the code again, add a grid layout before the buttons then edit the drinksSelect

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.