简体   繁体   English

TypeError:run() 缺少 1 个必需的位置参数:'self'

[英]TypeError:run() missing 1 required positional argument: 'self'

I wrote a simple program in pycharm on Windows, then it ran.我在Windows上pycharm写了一个简单的程序,然后就跑了。 In order to get the apk file, I installed ubuntu on a virtual machine.为了获取apk文件,我在虚拟机上安装了ubuntu。 Then I installed pip, paycharm, kivy. Qivy installed through the terminal according to the instructions with their site.然后我安装了 pip,paycharm,kivy。Qivy 根据他们网站的说明通过终端安装。 I typed the code and got an error:run() missing 1 required positional argument: 'self'.我输入代码并收到错误:run() 缺少 1 个必需的位置参数:“self”。 I tried to google but I couldn't find anything really.我试着用谷歌搜索,但我真的找不到任何东西。

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

class Container(FloatLayout):
    pass

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

        return Container()


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

in.kv file in.kv文件

<Container>:
    Button:
        background_normal: ''
        background_color: 0.5, 0, 1, .5
        size_hint: 0.3, 0.3
        pos_hint: {'center_x' : 0.5 , 'center_y':0.5}
        text:'Push'
        color: 0,1,0.5,1
        on_release:
            self.text = 'on release'

full error traceback完整的错误回溯

the.kv file does not support multi-line entries so far as I know.据我所知,.kv 文件不支持多行条目。 The method on_release needs to reference a function and you would normally put that in the widget (root.your_function) or app (app.your_function). on_release 方法需要引用 function,您通常会将其放在小部件 (root.your_function) 或应用程序 (app.your_function) 中。 I changed the answer to use build_string only for convenience;为了方便起见,我将答案更改为使用 build_string; it is a good idea to use a separate.kv file in your application as you did.像您一样在您的应用程序中使用单独的 .kv 文件是个好主意。

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

Builder.load_string('''
<Container>:
    Button:
        background_normal: ''
        background_color: 0.5, 0, 1, .5
        size_hint: 0.3, 0.3
        pos_hint: {'center_x' : 0.5 , 'center_y':0.5}
        text:'Push'
        color: 0,1,0.5,1
        # self argument here will be the button object
        on_release: app.button_callback(self)
''')


class Container(FloatLayout):
    pass


class MyApp(App):

    def button_callback(self, my_button):
        print(my_button.text)
        self.count += 1
        my_button.text = f"on_release {self.count}"

    def build(self):
        self.count = 0
        return Container()


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Panda3D:TypeError:run() 缺少 1 个必需的位置参数:'self' - Panda3D: TypeError: run() missing 1 required positional argument: 'self' 类型错误:exe() 缺少 1 个必需的位置参数:&#39;self&#39; - TypeError: exe() missing 1 required positional argument: 'self' TypeError:str()缺少1个必需的位置参数:&#39;self&#39; - TypeError: str() missing 1 required positional argument: 'self' TypeError:toString()缺少1个必需的位置参数:&#39;self&#39; - TypeError: toString() Missing 1 required positional argument: 'self' 捕获 TypeError:缺少 1 个必需的位置参数:'self' - Catching TypeError: Missing 1 required positional argument: 'self' TypeError: main() missing 1 required positional argument: 'self' - TypeError: main() missing 1 required positional argument: 'self' TypeError:endturn()缺少1个必需的位置参数:&#39;self&#39; - TypeError: endturn() missing 1 required positional argument: 'self' 类型错误:kollision() 缺少 1 个必需的位置参数:&#39;self&#39; - TypeError : kollision() missing 1 required positional argument: 'self' TypeError:itertuples()缺少1个必需的位置参数:“ self” - TypeError: itertuples() missing 1 required positional argument: 'self' 类型错误:get() 缺少 1 个必需的位置参数:“self” - TypeError: get() missing 1 required positional argument: 'self'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM