Kivy - 使用scrollview窗格格式化问题。

时间:2017-04-06 10:10:44

标签: python kivy

我正在尝试在Kivy中创建以下内容:https://imgur.com/a/RWDb6。该屏幕截图中的每个新配方都是我希望能够作为块添加到屏幕的内容。到目前为止,我有以下代码:

from kivy.app import App
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.checkbox import CheckBox 

from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.properties import StringProperty

class ShoppingScreen(Screen):

    def addRecipe(self):
        self.ids.grid.add_widget(RecipeLabel(recipeName = "test"))
        self.ids.grid.add_widget(Recipe())
        self.ids.grid.add_widget(Ingredient(ingredientName = "test", quantity="test"))

    def activate_checkbox(self):
        print("test")

class RecipeLabel(BoxLayout):
    recipeName = StringProperty('')

class Recipe(BoxLayout):
    ingredient = StringProperty('Ingredient')
    quantity = StringProperty('Quantity')

class theScreenManager(ScreenManager):
    pass

class Ingredient(BoxLayout):
    ingredientName = StringProperty('')
    quantity = StringProperty('')

root_widget = Builder.load_string('''
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
#:import Factory kivy.factory.Factory

theScreenManager:
    ShoppingScreen:

<ShoppingScreen>:
    name: 'shopping'
    dropdown: dropdown.__self__
    BoxLayout:
        orientation: 'vertical'
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, .3
            Button:
                text: '<'
                size_hint: .1, 1
                font_size: 75
                background_normal: ""
                background_color: 0.18, .5, .92, 1
                on_release: app.root.current = 'main' 

            Label:
                text: 'Shopping List'
                halign: 'left'
                font_size: 50
                canvas.before:
                    Color:
                        rgb: 0.18, .5, .92
                    Rectangle:
                        pos: self.pos
                        size: self.size
            Widget:
                size_hint: .1, 1
                canvas.before:
                    Color:
                        rgb: 0.18, .5, .92
                    Rectangle:
                        pos: self.pos
                        size: self.size
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, .4
            spacing: 50
            canvas.before:
                Color:
                    rgb: 0.8, 0.8, 0.8
                Rectangle:
                    pos: self.pos
                    size: self.size
            Label:
                text: 'Duration'
                font_size: 30
                color: 0.18, .5, .92, 1

            Button:
                id: btn
                text: 'Select a duration of time...'
                font_size: 15
                on_release: dropdown.open(self)
                height: '48dp'
                pos_hint: { 'top' : 0.75}
                size_hint: .8, .5

            DropDown:
                id: dropdown
                on_parent: self.dismiss()
                on_select: btn.text = '{}'.format(args[1])

                Button:
                    text: '1 week'
                    font_size: 15
                    size_hint_y: None
                    height: '48dp'
                    on_release: root.addRecipe()

                Button:
                    text: '2 weeks'
                    font_size: 15
                    size_hint_y: None
                    height: '48dp'
                    #on_release: root.formatScreen(5, 'Vegetarian Lasagne')

                Button:
                    text: '3 weeks'
                    font_size: 15
                    size_hint_y: None
                    height: '48dp'
                    #on_release: root.formatScreen(3, 'Tomato and linguine')
            Widget:
                size_hint: .02, 1

        BoxLayout:
            canvas.before:
                Color:
                    rgb: 1, 1, 1
                Rectangle:
                    pos: self.pos
                    size: self.size
            ScrollView:
                scroll_timeout: 250
                scroll_distance: 20
                do_scroll_y: True
                do_scroll_x: False

                GridLayout:
                    id: grid
                    height: self.minimum_height
                    cols: 1
                    spacing: 45
                    padding: 25
                    size_hint_y: None

<RecipeLabel>:
    Label:
        text: root.recipeName
        color: (0.18, .5, .92, 1)
        font_size: 30
        canvas.before:
            Color:
                rgb: 0.18, .5, .92
            Rectangle:
                pos: self.pos 
                size: self.size 
<Ingredient>:
    Label:
        text: root.ingredientName
        color: (0.18, .5, .92, 1)
        font_size: 15
    Label:
        text: root.quantity
        color: (0.18, .5, .92, 1)
        font_size: 15
    CheckBox:
        active: True
        on_active: root.activate_checkbox
        color: (0.18, .5, .92, 1)

<Recipe>:
    Label:
        text: 'Ingredient'
        color: (0.18, .5, .92, 1)
        font_size: 20
    Label:
        text: 'Quantity'  
        color: (0.18, .5, .92, 1)
        font_size: 20
    Widget:
        size_hint: .2, 1

''')

class temprecipeapp(App):
    def build(self):
        return root_widget

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

如果您运行此代码(只需将其复制并粘贴到Python文件中,如果您安装了Kivy就可以使用),请从下拉菜单中选择“1周”以加载其中一个“配方块”我是试图创造,两件事情出错了。首先,该复选框根本不可点击。我不能改变它的状态。其次,滚动视图顶部的“测试”标签的标签应该具有蓝色背景,但它根本不显示。我相信这两个问题都与scrollview / grid有关,但我无法确定。任何想法我应该做什么?

0 个答案:

没有答案