ubuntu TypeError:run()缺少1个必需的位置参数:"自我"

nbysray5  于 2022-12-22  发布在  其他
关注(0)|答案(1)|浏览(134)

我在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()

在.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

wydwbb8l

wydwbb8l1#

kv文件不支持多行输入,on_release方法需要引用一个函数,一般放在widget(root.your_function)或者app(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()

相关问题