如何在所有按钮和标签后面添加矩形图形?

时间:2019-07-22 17:17:13

标签: python button label kivy kivy-language

我想在小部件的每一行后面添加一个圆角矩形,该小部件包含3个按钮和两个水平放置的标签。每个组的内容通过底部的input_text和'+'按钮动态添加。我的所有主要部件都在工作,但是我无法获得圆角矩形图形。

这是到目前为止我得到的: enter image description here

我希望创建这样的东西:

enter image description here

请让我知道我哪里出了问题以及如何解决它。我在Kivy中还很新,所以我只是在学习。

谢谢。

class Trackers(Screen):
    storage = {}
    path = ''

def on_pre_enter(self):
    self.path = App.get_running_app().user_data_dir + '/'
    self.loadData()
    for tracker, num in self.storage.items():
        self.ids.track.add_widget(Tracker(text=tracker, number=num, data=self.storage))

def addWidget(self):
    text_input = self.ids.text_input.text
    num = '0'
    if text_input not in self.storage.keys():
        self.ids.track.add_widget(Tracker(text=text_input, number=num, data=self.storage))
        self.storage[text_input] = '0'
        self.ids.text_input.text = ''
        self.saveData()


class Tracker(BoxLayout):
    def __init__(self, text='', number='', data={}, **kwargs):
        super().__init__(**kwargs)
        self.ids.label.text = text
        self.ids.count_add.text = number

class Pess(App):

    def build(self):
        Config.set('graphics', 'width', '600')
        Config.set('graphics', 'height', '800')
        from kivy.core.window import Window
        Window.clearcolor = get_color_from_hex('#262829')

        return ScreenGenerator()

 ##### --> .kv file
<Trackers>:
    BoxLayout:
        orientation: 'vertical'
        ActionBar:
            height: 45
            size_hint_y: None
            background_image: ''
            background_color: rgba('#0B3242')
            ActionView:
                ActionPrevious:
                    title: '[b]TRACKERS[/b]'
                    font_size: 21
                    color: rgba('#AFB7BA')
                    markup: True
                    on_release: app.root.current = 'menu'
                ActionButton:
                    text: 'SEND'
                    color: rgba('#AFB7BA')
                    on_release: root.send()
        ScrollView:
            BoxLayout:
                id: track
                orientation: 'vertical'
                font_size: 15
                size_hint_y: None
                height: self.minimum_height
        BoxLayout:
            size_hint_y: None
            height: 45
            TextInput:
                id: text_input
                hint_text: 'Add Trackers'
                multiline: False
            Button:
                text: '+'
                size_hint_x: None
                width: 60
                on_release: root.addWidget()
                background_color: rgba('#1D7332')

<Tracker>:
    count_add: count_add
    size_hint_y: None
    height: 45
    padding: 4

    Button:
        text: '[b]X[/b]'
        markup: True
        size_hint_x: None
        width: 60
        on_release: app.root.get_screen('track').delete_storage(root)

    Label:
        id: label
        font_size: 20

    Label:
        id: count_add
        font_size: 20
        text: '0'

    Button:
        text: '[b]-[/b]'
        markup: True
        size_hint_x: None
        width: 60
        on_release: app.root.get_screen('track').subtract_num(root)

    Button:
        text: '[b]+[/b]'
        markup: True
        size_hint_x: None
        width: 60
        on_release: app.root.get_screen('track').add_num(root)

1 个答案:

答案 0 :(得分:0)

在“ kv”文件中,您可以像这样将图形添加到Canvas的{​​{1}}中:

Tracker

您可能想在<Tracker>: count_add: count_add size_hint_y: None height: 45 padding: 20, 4, 20, 4 # to keep widgets a bit away from the sides canvas.before: # use before to keep this under any widgets Color: rgba: 1, 0, 0, 1 # any color you want Rectangle: pos: self.pos[0] + self.height/2, self.pos[1] size: self.size[0] - self.height, self.height Ellipse: pos: self.pos[0], self.pos[1] size: self.height, self.height Ellipse: pos: self.pos[0] + self.width - self.height, self.pos[1] size: self.height, self.height ID track上增加一些间距,以使BoxLayout小部件不会显示为已连接。

这是我运行的代码的完整版本。由于您未提供所有代码,因此注释了几行:

Tracker