【问题标题】:KIVY: adding buttons in a sub layout when a button is pressed (on_release)KIVY:按下按钮时在子布局中添加按钮(on_release)
【发布时间】:2017-10-27 23:01:03
【问题描述】:

屏幕有多个布局,都是盒子布局的子布局。当单击其他布局中的按钮时,我试图在最后一个子布局(网格布局)中添加一个按钮。代码不会崩溃,但也没有任何反应。我想在单击按钮时将按钮添加到带有 id 饮料布局的布局中,例如百事可乐、精灵

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'

【问题讨论】:

  • 都是你的kv码吗?
  • 不只是一部分
  • 我们需要更多信息来帮助您
  • 现在我已经添加了整个 Kivy 代码

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


【解决方案1】:

好的,问题是你的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'

输出

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

【讨论】:

  • 谢谢。有没有办法限制制作的盒子数量并固定按钮 b1 的尺寸?
  • 您能否再解释一下代码。我是新手,不认识kivy
  • 再次感谢。最后一件事你能告诉如何将 b1 按钮放在右边
  • 是的,当然我已经再次编辑了代码,在按钮之前添加了一个网格布局,然后编辑drinksSelect
猜你喜欢
  • 2012-09-23
  • 1970-01-01
  • 2021-04-17
  • 2021-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
相关资源
最近更新 更多