Kivy在标签或按钮内制作网格布局

时间:2018-09-29 09:46:15

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

在猕猴桃的“标签”或“按钮”中是否可以具有类似于网格的布局。

我有一个应用程序,其中包含带有产品信息的CSV文件,我想用CSV文件中的行填充MainScreen。 每行应如下所示:

Row 最后,应按下“标签”或“按钮”以打开一个弹出窗口,以确认产品数量并进行确认。
甚至有可能还是我从错误的角度接近它?

我还没有任何代码可以用行填充MainScreen,但这是到目前为止的样子。 澄清。目前,我不需要导入CSV文件的帮助,但可以使用符合以上条件的显示方法(图片)

到目前为止,代码如下:
ATmain.py

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.properties import StringProperty

Window.clearcolor = (1,1,1,1)
Window.size = (270, 480)


class LoginScreen(Screen):
    input = StringProperty("")


class MainScreen(Screen):
    username = StringProperty('')


class ScreenManagement(ScreenManager):
    pass


presentation = Builder.load_file("app.kv")


class ATApp(App):
    presentation = Builder.load_file("app.kv")
    def build(self):
        return presentation


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

app.kv:

# File name: main.py
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
#:kivy 1.10.1

ScreenManagement:
    transition: FadeTransition()
    LoginScreen:
        id: login
    MainScreen:
        username: login.input



<LoginScreen>:
    name: "login"
    canvas:
        Color:
            rgba: [1,1,1]
        Rectangle:
            pos: self.pos
            size: self.size
    FloatLayout:
        rows:2
        cols:1
        background_color: 1,1,1,1
        Label:
            size_hint: 0.3, 0.05
            pos_hint: {"x": 0.5 - 0.3/2, "center_y": 0.4}
            text:"Kasutaja"
            color: 0,0,0,1
        TextInput:
            id: input
            size_hint: (0.3, None)
            height: 30
            pos_hint: {"x": 0.5 - 0.3/2, "center_y": 0.3}
            multiline: False

        Button:
            id: confirm_login
            text: "Login"
            size_hint: 0.15, 0.07
            pos_hint: {"x": 0.5 - 0.15/2, "center_y": 0.2}
            background_color: 0.9,0.9,0.9,1
            on_press: self.background_color = (1,0,0,1)
            on_release: root.input = input.text; app.root.current = "main"

<MainScreen>:
    name: "main"
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size 
    Label:

        id:name
        text: root.username
        color: (0,0,0,1)
        size_hint_y: None
        height: 30
        size_hint_x: 1
        pos_hint: {"right": 1, "top": 1}


    BoxLayout:
        orientation: "vertical"
        size_hint_y: None
        size_hint_x: 1
        pos_hint_x: None
        pos_hint_y: 1

        Button:
            text: "Item1"
            color: (0,0,0,1)
            height: self.height
            size_hint_y: None
            size_hint_x: 1
            pos_hint: {"right": 1, "top": 0}

如果有人能指出正确的方向,我将非常感激!

1 个答案:

答案 0 :(得分:0)

奇异的破解方法是简单地使用GridLayout或任何布局,然后给您的布局按钮属性,这样就可以单击:

from kivy.behaviors import ButtonBehavior

#then make a clickable grid
class GridButton(GridLayout, ButtonBehaviour):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
    #Then do whatever you want to do

我猜想的另一种方法是使用on_touch_down回调并检查触摸是否在小部件的范围内