Kivy Label для отображения значения переменной - PullRequest
0 голосов
/ 31 мая 2019

На этикетке не отображается значение price, кто-нибудь может мне помочь, почему он не работает?

Код выглядит следующим образом

Builder.load_string("""
<Mngr>:
    Tela2:
        name: 'tela2'

    Tela5:
        name: 'tela5'

<Tela2>:
    ngd_input: input1
    BoxLayout:
        spacing: 20
        orientation: 'vertical'
        Spinner:
            id: spinner_1
            text: "Regiao"
            values: "Norte", "Nordeste", "Centro-Oeste", "Sudeste", "Sul"
            on_text: root.region_define(spinner_1.text)
        TextInput:
            id: input1
            text: "Necessidade de Geracao (W)"
            on_text: root.ngd_define()

        Button:
            text:  'go to tela5'
            on_press: root.op_dimoff()
            on_release: app.root.current = 'tela5'

<Tela5>:
    cost_output: costa
    Label:
        id: costa
""")

class Mngr (ScreenManager):
    pass

class Tela2(Screen):

    ngd = ObjectProperty()

    def region_define(self, text):

        # valores de irradiação global para as regiões (em KWh/m²*dia)
        inorte: float = 4.825
        inordeste: float = 5.483
        icentro: float = 5.082
        isudeste: float = 4.951
        isul: float = 4.444

        # valores médios do KWh para cada região (em R$/kWh)
        kwh_norte: float = 0.871
        kwh_nordeste: float = 0.308
        kwh_centro: float = 0.290
        kwh_sudeste: float = 0.322
        kwh_sul: float = 0.320


        if text == 'Norte':
            self.irrad = inorte
            self.kwh = kwh_norte

        elif text == 'Nordeste':
            self.irrad = inordeste
            self.kwh = kwh_nordeste

        elif text == 'Centro-Oeste':
            self.irrad = icentro
            self.kwh = kwh_centro

        elif text == 'Sudeste':
            self.irrad = isudeste
            self.kwh = kwh_sudeste

        else:
            self.irrad = isul
            self.kwh = kwh_sul

    def ngd_define(self):

        self.ngd = int(self.ngd_input.text)

    def op_dimoff(self):

        self.gmin = float(self.ngd / self.irrad)

        # dimensionamento potência do inversor
        self.pot_seg = self.gmin * 1.3
        self.inv = (600, 1000, 1500, 2000, 3000)
        self.pri_inv = (1434, 1852.2, 1924, 2604, 3899)
        x = 0
        for x in range(0, len(self.inv)):
            if self.pot_seg <= self.inv[int(x)]:
                self.pot_inv = self.inv[int(x)]
                self.price_inv = self.pri_inv[int(x)]
                break
            else:
                self.x += 1

        self.price = float((int(self.ngd) * 0.87 * 1.75) + self.price_inv)

        cost_v = str(self.price)

        n1 = Tela5()
        n1.catch_values(cost_v)

class Tela5 (Screen):

    cost = StringProperty()

    def catch_values(self, cost):
        self.cost = cost
        self.cost_output.text = self.cost
        print(cost)
        print(self.cost)

class Doideira(App):
    def build(self):
        return Mngr()

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

1 Ответ

0 голосов
/ 31 мая 2019

Основная причина

Вы создали экземпляр другого экземпляра экрана, Tela5, который отличается от другого экземпляра, созданного в файле kv.Ваш второй экземпляр Tela5 не имеет представления.Поэтому обновленное значение не отображается.

Решение

Замените n1 = Tela5() на n1 = self.manager.get_screen("tela5"), чтобы получить экземпляр Tela5, созданный в файле kv.

Фрагменты - пи-файл

    self.price = float((int(self.ngd) * 0.87 * 1.75) + self.price_inv)

    cost_v = str(self.price)

    n1 = self.manager.get_screen("tela5")
    n1.catch_values(cost_v)

Вывод

Tela2 - Input Tela5 - Result

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