Как указать значение переменных с помощью kivy - PullRequest
1 голос
/ 11 июля 2020

Я новичок в этом, поэтому могу представить, что это выглядит ужасно. Мои проблемы начинаются со строки 43. Я взял кусочки другого кода, который нашел, чтобы начать обучение. Мне не удалось найти ответы на kivy, например, около python.

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout

Builder.load_string("""
<Calc>:
    # This are attributes of the class Calc now
    a: _a
    b: _b
    c: _c
    d: _d
    e: _e
    result: _result
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        ScreenManager:
            size_hint: 1, .9
            id: _screen_manager
            Screen:
                name: 'screen1'
                GridLayout:
                    cols:1
                    Label:
                        text: 'Grats'
                    TextInput:
                        id: _a
                    Label:
                        text: 'Alcohol Sales'
                    TextInput:
                        id: _b
                    Label:
                        text: 'Food Sales'
                    TextInput:
                        id: _c
                    Label:
                        id: _result
                    TextInput:
                        id: _d
                        text: '0.06'
                    TextInput:
                        id: _e
                        text: '0.05'
                    Button:
                        text: 'sum'
                        # You can do the opertion directly
                        on_press: _result.text = int(_a.text) - (str(int(root.b)) * int(float(root.d.text)) + str(int(root.c)) * int(float(root.e.text))
                    Button:
                        text: 'product'
                        # Or you can call a method from the root class (instance of calc)
                        on_press: root.product(*args)
                     
            Screen:
                name: 'screen2'
                Label: 
                    text: 'The second screen'
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, .1
            Button:
                text: 'Go to Screen 1'
                on_press: _screen_manager.current = 'screen1'
            Button:
                text: 'Go to Screen 2'
                on_press: _screen_manager.current = 'screen2'""")

class Calc(FloatLayout):
    # define the multiplication of a function
    def product(self, instance):
        # self.result, self.a and self.b where defined explicitely in the kv
        self.result.text = int(_a.text) - (str(int(self.b.text) * int(self.d.text)))
        

class TestApp(App):
    def build(self):
        return Calc()

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

У меня есть похожее приложение, работающее только с python, которое дает чаевые. Я хотел бы добавить к этому GUI, но не могу заставить математическую часть работать.

1 Ответ

0 голосов
/ 11 июля 2020

Для правильного кодирования вам необходимо контролировать ввод текста. Потому что, если пользователь передаст что-то из этого, программа получит сбой. Но изучите мой код и посмотрите, как вы можете исправить свою ошибку в кодах .kv. Вам необходимо улучшить свой код. Удачи.

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
Builder.load_string("""
<Calc>:
    # This are attributes of the class Calc now
    a: _a
    b: _b
    c: _c
    d: _d
    e: _e
    result: _result
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        ScreenManager:
            size_hint: 1, .9
            id: _screen_manager
            Screen:
                name: 'screen1'
                GridLayout:
                    cols:1
                    Label:
                        text: 'Grats'
                    TextInput:
                        id: _a
                    Label:
                        text: 'Alcohol Sales'
                    TextInput:
                        id: _b
                    Label:
                        text: 'Food Sales'
                    TextInput:
                        id: _c
                    Label:
                        id: _result
                    TextInput:
                        id: _d
                        text: '0.06'
                    TextInput:
                        id: _e
                        text: '0.05'
                    Button:
                        text: 'sum'
                        on_press: _result.text = str(float(_a.text)-float(_b.text)*float(_d.text)+float(_c.text)*float(_e.text))
                    Button:
                        text: 'product'
            Screen:
                name: 'screen2'
                Label: 
                    text: 'The second screen'
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, .1
            Button:
                text: 'Go to Screen 1'
                on_press: _screen_manager.current = 'screen1'
            Button:
                text: 'Go to Screen 2'
                on_press: _screen_manager.current = 'screen2'""")
class Calc(FloatLayout):
    def product(self, instance):
        pass #For improve, you need to use ObjectProperty for check textinputs.
class TestApp(App):
    def build(self):
        return Calc()
if __name__ == '__main__':
    TestApp().run()
...