Python 3 kivy AttributeError - PullRequest
       22

Python 3 kivy AttributeError

0 голосов
/ 18 февраля 2019

Мне нужна помощь с этим кодом, кроме того, он работает, но при вычитании возникает ошибка.Я тренирую эти коды, и я новичок в Python.В будущем я хочу сделать специальный калькулятор.

Код на .py:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout

class Gerenciador(ScreenManager):
    pass
class Menu(Screen):
    pass

class Addition(Screen):
    def dadd(self):
        try:
            tx1 = float(self.ids.tx1.text)
            tx2 = float(self.ids.tx2.text)

            tg1 = (self.ids.tg1.state == 'down')
            tg2 = (self.ids.tg2.state == 'down')

            if tg1:
                self.ids.tr.text = str(tx1 + tx2)
            elif tg2:
                self.ids.tr.text = str((tx1 + tx2)+1)
        except:
            self.ids.tr.text = 'error'

class Subtraction(Screen):
    def dsub(self):
        try:
            tx1 = float(self.ids.tx3.text)
            tx2 = float(self.ids.tx4.text)

            tg1 = (self.ids.tg3.state == 'down')
            tg2 = (self.ids.tg4.state == 'down')

            if tg1:
                self.ids.tr.text = str(tx3 - tx4)
            elif tg2:
                self.ids.tr.text = str((tx3 - tx4)-1)
        except:
            self.ids.tr.text = 'error'

class Arithmetic(App):
    def build(self):
        return Gerenciador()

Arithmetic().run()

и .kv:

<Gerenciador>:

    Menu:
        name: 'menu'
    Addition:
        name: 'addition'
    Subtraction:
        name: 'subtraction'
<Menu>:
    BoxLayout:
        orientation:'vertical'
        Button:
            text: 'Addition'
            on_release:app.root.current = 'addition'
        Button:
            text: 'Subtraction'
            on_release:app.root.current = 'subtraction'
        Button:
            text: 'Multiplication'
        Button:
            text: 'Division'

<Addition>:
    BoxLayout:
        orientation:'vertical'
        BoxLayout:
            size_hint_y:None
            height:120
            ActionBar:
                ActionView:
                    ActionPrevious:
                        title:'Back'
                        on_release:app.root.current = 'menu'
        BoxLayout:
            ToggleButton:
                id:tg1
                text: 'Addition'
                group: '1'
            ToggleButton:
                id:tg2
                text: '+1'
                group: '1'
        BoxLayout:
            TextInput:
                id:tx1
                multiline:False
                input_type:'number'
                input_filter:'float'
            Label:
                text:'+'
            TextInput:
                id:tx2
                multiline:False
                input_type:'number'
            Label:
                text:'='
            Label:
                id: tr
                text:'Result'
        BoxLayout:
            orientation:'vertical'
            BoxLayout:
            BoxLayout:
                Button:
                    text: 'Calc'
                    on_release: root.dadd() 

<Subtraction>:
    BoxLayout:
        orientation:'vertical'
        BoxLayout:
            size_hint_y:None
            height:120
            ActionBar:
                ActionView:
                    ActionPrevious:
                        title:'Back'
                        on_release:app.root.current = 'menu'
        BoxLayout:
            ToggleButton:
                id:tg3
                text: 'Subtraction'
                group: '2'
            ToggleButton:
                id:tg4
                text: '-1'
                group: '2'
        BoxLayout:
            TextInput:
                id:tx3
                multiline:False
                input_type:'number'
                input_filter:'float'
            Label:
                text:'-'
            TextInput:
                id:tx4
                multiline:False
                input_type:'number'
            Label:
                text:'='
            Label:
                id: tr0
                text:'Result'
        BoxLayout:
            orientation:'vertical'
            BoxLayout:
            BoxLayout:
                Button:
                    text: 'Calc'
                    on_release: root.dsub()

Когда я бегу на экране вычитаниявозникает ошибка.Я понял, почему, кроме того, он работает, и он не работает в вычитании.

File "kivy\properties.pyx", line 839, in kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:12654)
AttributeError: 'super' object has no attribute '__getattr__'

1 Ответ

0 голосов
/ 18 февраля 2019

В вашем Subtraction классе вы ссылаетесь на self.ids.tr, но в вашем Subtraction классе нет id: tr.Возможно, это должно быть self.ids.tr0 в 3 местах в классе Subtraction.

Есть также ссылки на неопределенные tx3 и tx4.Я подозреваю, что класс Subtraction должен выглядеть так:

class Subtraction(Screen):
    def dsub(self):
        try:
            tx3 = float(self.ids.tx3.text)
            tx4 = float(self.ids.tx4.text)

            tg1 = (self.ids.tg3.state == 'down')
            tg2 = (self.ids.tg4.state == 'down')

            if tg1:
                self.ids.tr0.text = str(tx3 - tx4)
            elif tg2:
                self.ids.tr0.text = str((tx3 - tx4)-1)
        except:
            self.ids.tr0.text = 'error'
...