You need to enable JavaScript to run this app. 使用Kivy开发Python计算器APP点击C按钮出现AttributeError报错求助
优惠活动
大模型
产品
解决方案
定价
更多

使用Kivy开发Python计算器APP点击C按钮出现AttributeError报错求助

问题排查及修复方案

1. 点击C按钮报错的核心原因

你把clear()方法定义在了从未使用的MyLayout类中,但KV文件里C按钮的on_press绑定的是root.clear(),这里的root指向的是当前的Screen1类,Screen1没有定义clear()方法,所以触发属性不存在错误。
同时你在KV中给TextInput设置的id是拼写错误的clac_input,后续即使方法绑定正确也会出现找不到组件的错误。

2. 缩进问题修复

Python代码需要严格遵守缩进规范:

  • 你calc.py第一行from kivy.app import App前有多余的3个空格,需要删除
  • if __name__ == "__main__"下的App().run()前多余的缩进要删除,和上一行保持同缩进

修改后代码

calc.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window

Window.size = (500,700)
Builder.load_file('calc.kv')

class Screen1(Screen):
    def clear(self):
        self.ids.calc_input.text = "0"

class WindowManager(ScreenManager):
    pass

class CalculatorApp(App):
    def build(self):
        return WindowManager()

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

注:删除了无用的MyLayout类,把clear()方法移到了Screen1类中,同时把你的App类改名避免和导入的App类重名冲突。

calc.kv

#:kivy 2.0.0
<WindowManager>:
    Screen1:

<Screen1>:
    BoxLayout:
        orientation:"vertical"
        size: root.width, root.height

        TextInput:
            id: calc_input
            text: "0"
            halign: "right"
            font_size : 65
            size_hint: (1, .15)

        GridLayout:
            cols:4
            rows:5

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "%"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "CE"

            Button:
                id: clear
                size_hint: (.2, .2)
                font_size: 32
                text: "C"
                on_press : root.clear()

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "/"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "7"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "8"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "9"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "x"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "4"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "5"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "6"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "-"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "1"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "2"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "3"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "+"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "+/-"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "0"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "."

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "="

注:修正了TextInput的id拼写,同时把TextInput移出了GridLayout,避免占用按钮的布局格子导致界面错位。

内容的提问来源于stack exchange,提问作者Akshit Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 04:36:04