Как получить доступ к атрибуту динамически создаваемой кнопки в Kivy - PullRequest
0 голосов
/ 14 марта 2019

Я пытаюсь создать свой календарь в Киви. Мне удалось поместить его в виджет Popup, но теперь я не знаю, как получить атрибут. Я пытаюсь получить текст от динамически созданной кнопки, которая представляет дату. Моя проблема возникает здесь. Я не знаю как это сделать. Ниже приведен код и файл kv. Кроме того, если кому-то нужен «календарь», вы можете скачать и изменить его в соответствии с вашими потребностями. Это бесплатно :) 1001 *


Py file

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.properties import StringProperty, ObjectProperty, ListProperty, NumericProperty
import datetime


class Pocetni(BoxLayout):
    def calendar(self):
        Calendar().open()


class Calendar(Popup):
    td = datetime.datetime.today()
    day = td.day
    fdm = td.replace(day=1)
    d = "{:%w}"
    mg = "{:%B %Y}"
    month = StringProperty()
    change = ObjectProperty()
    days = ListProperty()
    first = NumericProperty()
    dani = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Calendar, self).__init__(**kwargs)
        self.month = str(self.mg.format(self.td))
        self.change = self.td

        self.update_days()

        self.first = self.d.format(self.fdm)

        self.draw_date()

    def update_days(self):
        for i in range(1, 32):
            prov = str(i)+'.'+str(self.change.month)+'.'+str(self.change.year)+'.'
            if i >= 28:
                try:
                    datetime.datetime.strptime(prov, "%d.%m.%Y.")
                    self.days.append(i)
                except ValueError:
                    pass
            else:
                self.days.append(i)

    def draw_date(self):
        if int(self.first) == 0:
            self.first = str(int(self.first) + 6)
        else:
            self.first = str(int(self.first) - 1)

        for i in range(int(self.first)):
            self.ids.dani.add_widget(Label(text=""))

        for j in self.days:
            if j == self.day and self.change.month == self.td.month and self.change.year == self.td.year:
                self.ids.dani.add_widget(Button(text=str(j), color=(1, 0, 0, 1),
                                                background_color=(1, 1, 1, .3),
                                                on_press=self.ispis))
            else:
                self.ids.dani.add_widget(Button(text=str(j),
                                                color=(0, 0, 0, 1),
                                                background_color=(1, 1, 1, .3)))

        self.days = []

    def next(self):
        self.ids.dani.clear_widgets()

        if self.change.month < 12:
            add = self.change.month + 1
            up = self.change.replace(month=add)
            self.change = up
            self.month = str(self.mg.format(up))
            self.fdm = self.change.replace(day=1)
            self.first = self.d.format(self.fdm)
            self.update_days()
            self.draw_date()
        else:
            start = self.change.month - 11
            new = self.change.year + 1
            up = self.change.replace(month=start, year=new)
            self.change = up
            self.month = str(self.mg.format(up))
            self.fdm = self.change.replace(day=1)
            self.first = self.d.format(self.fdm)
            self.update_days()
            self.draw_date()

    def previous(self):
        self.ids.dani.clear_widgets()

        if self.change.month > 1:
            add = self.change.month - 1
            up = self.change.replace(month=add)
            self.change = up
            self.month = str(self.mg.format(up))
            self.fdm = self.change.replace(day=1)
            self.first = self.d.format(self.fdm)
            self.update_days()
            self.draw_date()
        else:
            start = self.change.month + 11
            new = self.change.year - 1
            up = self.change.replace(month=start, year=new)
            self.change = up
            self.month = str(self.mg.format(up))
            self.fdm = self.change.replace(day=1)
            self.first = self.d.format(self.fdm)
            self.update_days()
            self.draw_date()

    def ispis(self, a):
        print(self.ids.dani.ids)
        print("Stiglo")


class CalendarApp(App):
    def build(self):
        return Pocetni()


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

Kv файл

<Datumi@Label>:
font_size: self.height / 4

<Date@Label>:
    color: 0, 0, 1, .8
    font_size: self.height / 2
    italic: True

<Calendar@Popup>:
    dani: dani
    title: "Calendar"
    title_size: '20sp'
    title_align: "center"
    separator_color: 1, 0, 0, 1
    size_hint: .9, .7
    auto_dismiss: False
    canvas.before:
        Color:
            rgba: 0, 0, 1, 1
        Rectangle:
            size: self.size
            pos: self.pos

    BoxLayout:
        orientation: "vertical"
        canvas.before:
            Color:
                rgba: 1, 1, 1, 1
            Rectangle:
                size: self.size
                pos: self.pos

        BoxLayout:
            size_hint_y: .15
            orientation: "horizontal"
            padding: 10
            canvas.before:
                Color:
                    rgba: .12, .54, .97, 1
                Rectangle:
                    size: self.size
                    pos: self.pos

            Label:
                font_size: self.height / 1.5
                size: self.texture_size
                text_size: self.size
                halign: "left"
                valign: "middle"
                text: root.month
                italic: True
                bold: True
            Button:
                size_hint_x: .2
                text: "back"
                on_press: root.previous()
            Button:
                size_hint_x: .2
                text: "next"
                on_press: root.next()

        BoxLayout:
            size_hint_y: .1
            orientation: "horizontal"
            Date:
                text: "Mon"
            Date:
                text: "Tue"
            Date:
                text: "Wed"
            Date:
                text: "Thu"
            Date:
                text: "Fri"
            Date:
                text: "Sat"
            Date:
                text: "Sun"

        GridLayout:
            id: dani
            cols: 7
            rows: 6

<Pocetni>:
    Button:
        text: "Poziv"
        on_press: root.calendar()

Красный номер - текущий день

Red number is current day

1 Ответ

0 голосов
/ 14 марта 2019

Один из способов сделать это - добавить on_press=self.ispis к каждой кнопке, а не только к текущей дате.

            self.ids.dani.add_widget(Button(text=str(j),
                                            color=(0, 0, 0, 1),
                                            background_color=(1, 1, 1, .3),
                                            on_press=self.ispis))

Итак, теперь, если вы немного измените свою функцию ispis,

def ispis(self, button):
    print(button.text)
    if button.color == [1, 0, 0, 1]:
        print("Stiglo")

вы получите текст кнопки, а также проверите, что текущий день нажат ...

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...