簡體   English   中英

TypeError:run() 缺少 1 個必需的位置參數:'self'

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

我在Windows上pycharm寫了一個簡單的程序,然后就跑了。 為了獲取apk文件,我在虛擬機上安裝了ubuntu。 然后我安裝了 pip,paycharm,kivy。Qivy 根據他們網站的說明通過終端安裝。 我輸入代碼並收到錯誤:run() 缺少 1 個必需的位置參數:“self”。 我試着用谷歌搜索,但我真的找不到任何東西。

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文件

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

完整的錯誤回溯

據我所知,.kv 文件不支持多行條目。 on_release 方法需要引用 function,您通常會將其放在小部件 (root.your_function) 或應用程序 (app.your_function) 中。 為了方便起見,我將答案更改為使用 build_string; 像您一樣在您的應用程序中使用單獨的 .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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM