python - KIVY:按下按钮时在子布局中添加按钮(on_release)

标签 python python-3.x kivy kivy-language

屏幕有多个布局,所有布局都是框布局的子布局。当单击其他布局中的按钮时,我试图在最后一个子布局(网格布局)中添加一个按钮。代码不会崩溃,但也没有发生任何事情。我想在单击按钮时将按钮添加到带有 id Drinkslayout 的布局,例如百事可乐、雪碧

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'

最佳答案

好吧,问题出在您的 drinksSelect 方法上,您还需要保留屏幕的值

试试这个:

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'

输出:

first

second

我希望代码现在有点清楚了

关于python - KIVY:按下按钮时在子布局中添加按钮(on_release),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46984515/

相关文章:

python - Curl 和 Python Requests (get) 报告不同的 http 状态代码

python - 如何在 SQLAlchemy 对象实例中查询一对多关系?

python - 在 kivy 中添加搜索过滤器

python - Python 中等效的 'nth_element' 函数是什么?

python - Tkinter Tic Tac Toe 在某个盒子中绘制形状

python - 寻找主要因素的差异

Python:多维字典到数组

python - 我已经为 GCSE Computing 创建了一个食谱程序,但我缺少一个步骤

python - 阿拉伯语文本的 Kivy 文本输入

python - Kivy中将BoxLayout切换为垂直并在Python代码中通过id获取小部件