Помогите разобраться с кодом (Python + Kivy)

OlegChumak

Новичок
Пользователь
Июн 22, 2022
1
0
1
Приветствую.
Помогите пожалуйста разобраться, что не так я делаю.

Python:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
from kivy.core.window import Window

Window.size = (480, 720)

from kivy.config import Config
Config.set('kivy', 'keyboard_mode', 'systemanddock')


def get_brutto(x, y, c):
    r = str(round(y / x) * 100 - 100)
    r1 = str(round(c / x) * 100 - 100)

    return {'r': r, 'r1': r1}

class Container(GridLayout):

    def calculate(self):
        try:
            x = int(self.text_input.x)
            y = int(self.text_input.y)
            c = int(self.text_input.c)

        except:
            x = 0

        brutto = get_brutto(x, y, c)

        self.x.text = brutto.get('x')
        self.y.text = brutto.get('y')
        self.c.text = brutto.get('c')
        self.r.text = brutto.get('r')
        self.r1.text = brutto.get('r1')

class MyApp(App):
    def build(self):
        return Container()


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

Код:
<ItemLabel@Label>:
    font_size: '25sp'
    haline: 'left'
    valign: 'middle'
    text_size: self.size

<Container>:
    rows: 6
    text_input: x
    text_input: y
    text_input: c

    AnchorLayout:
        size_hint: 1, 1
        rows:3

        GridLayout:
            rows: 3
            FloatLayout:
                TextInput:
                    id: x
                    font_size: '45sp'
                    pos_hint: {'top': 1}
                    multiline: False
                    font_size: 40
            FloatLayout:
                TextInput:
                    id: y
                    font_size: '45sp'
                    pos_hint: {'top': 1}
                    multiline: False
                    font_size: 40
            FloatLayout:
                TextInput:
                    id: c
                    font_size: '45sp'
                    pos_hint: {'top': 1}
                    multiline: False
                    font_size: 40

    GridLayout:
        cols: 2

        BoxLayout:
            orientation: 'vertical'
            padding: [30, 0, 0, 0]
            ItemLabel:
                text: 'Номинальный вес'

            ItemLabel:
                text: 'Минимальный вес'

            ItemLabel:
                text: 'Максимальный вес'

        BoxLayout:
            orientation: 'vertical'
            padding: [30, 0, 0, 0]
            size_hint: 1, 1
            ItemLabel:
                id: x
                text: ''

            ItemLabel:
                id: r
                text: ''

            ItemLabel:
                id: r1
                text: ''

    BoxLayout:
        size_hint: 0.9, 0.5
        padding: [30, 0, 30, 20]
        Button:
            #size_hint: 0.5, 0.4
            pos_hint: {'center_x': 0.5, 'y': 0.1}
            text: 'Результат'

            on_release:
                root.calculate()

    BoxLayout:
        size_hint: 0.9, 0.5
        padding: [30, 0, 30, 20]
        Button:
            #size_hint: 0.5, 0.4
            pos_hint: {'center_x': 0.5, 'y': 0.1}
            text: 'SinglePack'
    BoxLayout:
        size_hint: 0.9, 0.5
        padding: [30, 0, 30, 20]
        Button:
            #size_hint: 0.5, 0.4
            pos_hint: {'center_x': 0.5, 'y': 0.1}
            text: 'MultiPack'

Traceback (most recent call last):
File "C:\Users\Professional\PycharmProjects\pythonProject4\main.py", line 44, in <module>
MyApp().run()
File "C:\Users\Professional\PycharmProjects\pythonProject4\venv\lib\site-packages\kivy\app.py", line 955, in run
runTouchApp()
File "C:\Users\Professional\PycharmProjects\pythonProject4\venv\lib\site-packages\kivy\base.py", line 574, in runTouchApp
EventLoop.mainloop()
File "C:\Users\Professional\PycharmProjects\pythonProject4\venv\lib\site-packages\kivy\base.py", line 339, in mainloop
self.idle()
File "C:\Users\Professional\PycharmProjects\pythonProject4\venv\lib\site-packages\kivy\base.py", line 383, in idle
self.dispatch_input()
File "C:\Users\Professional\PycharmProjects\pythonProject4\venv\lib\site-packages\kivy\base.py", line 334, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\Professional\PycharmProjects\pythonProject4\venv\lib\site-packages\kivy\base.py", line 302, in post_dispatch_input
wid.dispatch('on_touch_up', me)
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Professional\PycharmProjects\pythonProject4\venv\lib\site-packages\kivy\uix\behaviors\button.py", line 179, in on_touch_up
self.dispatch('on_release')
File "kivy\_event.pyx", line 727, in kivy._event.EventDispatcher.dispatch
File "kivy\_event.pyx", line 1307, in kivy._event.EventObservers.dispatch
File "kivy\_event.pyx", line 1191, in kivy._event.EventObservers._dispatch
File "C:\Users\Professional\PycharmProjects\pythonProject4\venv\lib\site-packages\kivy\lang\builder.py", line 55, in custom_callback
exec(__kvlang__.co_value, idmap)
File "C:\Users\Professional\PycharmProjects\pythonProject4\my.kv", line 82, in <module>
root.calculate()
File "C:\Users\Professional\PycharmProjects\pythonProject4\main.py", line 30, in calculate
brutto = get_brutto(x, y, c)
UnboundLocalError: local variable 'c' referenced before assignment
 

regnor

Модератор
Команда форума
Модератор
Июл 7, 2020
2 650
472
83
ошибка говорит о том, что у вас не определена переменная c

попробуйте так, но перехватывать все ошибки, это плохо
Python:
        try:
            x = int(self.text_input.x)
            y = int(self.text_input.y)
            c = int(self.text_input.c)

        except:
            x = 0
            y = 0
            c = 0
 

Форум IT Специалистов