python3kivyattributee

2024-05-18 07:33:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要这段代码的帮助,此外,它可以工作,但在减法中会出现错误。我正在训练这些代码,我是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()

和.千伏:

<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__'

Tags: textselfididsreleaseonbuttontg2
1条回答
网友
1楼 · 发布于 2024-05-18 07:33:13

Subtraction类中,引用了self.ids.tr,但是Subtraction类中没有id: tr。也许在Subtraction类中的3个位置应该是self.ids.tr0

还有对未定义的tx3tx4的引用。我怀疑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'

相关问题 更多 >