简体   繁体   English

类型错误:函数需要 1 个位置参数,但 2 个被赋予了 kivy python

[英]TypeError: Function takes 1 positional argument but 2 were given kivy python

I tried to add instance after the self, but still not working我试图在自我之后添加实例,但仍然无法正常工作

this is the error message这是错误信息

File "kivy\_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch

File "kivy\_event.pyx", line 1214, in kivy._event.EventObservers.dispatch

File "kivy\_event.pyx", line 1138, in kivy._event.EventObservers._dispatch

TypeError: sss() takes 1 positional argument but 2 were given

here is the full kivy python code这是完整的 kivy python 代码

import kivy

from kivy.uix.widget import Widget

from kivy.properties import ObjectProperty

from kivy.app import App

from kivy.uix.label import Label

from kivy.uix.gridlayout import GridLayout

from kivy.uix.textinput import TextInput

from kivy.uix.button import Button

from kivy.uix.screenmanager import ScreenManager, Screen



class Touch(Widget):

    def __init__(self, **kwargs):

        super(Touch, self).__init__(**kwargs)

        self.cols = 1

        self.Rc1 = Button(text="Submit", font_size=40)

        self.Rc1.bind(on_press=self.sss)

        self.add_widget(self.Rc1)


    def sss(self):

        self.Rc1 = Button(text="Push Me !",

                          font_size="20sp",

                          background_color=[1, 0, 0, 1],

                          color=(1, 1, 1, 1),

                          size=(32, 32),

                          size_hint=(.2, .2),

                          pos=(300, 250))

class MyApp(App):

    def build(self):

        return Touch()


if __name__ == "__main__":

    MyApp().run()

When you click button then it executes your function with some information.当您单击按钮时,它会使用一些信息执行您的功能。 In different GUIs it can use different information.在不同的 GUI 中,它可以使用不同的信息。 In some GUIs it can be info about event, in others it can be info about widget, etc.在某些 GUI 中,它可以是有关事件的信息,在其他 GUI 中,它可以是有关小部件的信息等。

Kivy sends information about clicked widget and you have to receive it Kivy发送有关单击的小部件的信息,您必须接收它

def sss(self, widget):

If you want to change existing Button then don't create new button - it will not change existing if you assing it to the same variable.如果您想更改现有Button则不要创建新按钮 - 如果您将其分配给相同的变量,它不会更改现有按钮。 You would have to remove old button and add new button using add_widget()您必须删除旧按钮并使用add_widget()添加新按钮

You have existing button in self.Rc1 so you can do您在self.Rc1有现有按钮,因此您可以执行

def sss(self, widget):
    self.Rc1.text = "Push Me !"
    self.Rc1.font_size = "20sp"
    self.Rc1.background_color = [1, 0, 0, 1]
    self.Rc1.color = (1, 1, 1, 1)
    self.Rc1.size = (32, 32)
    self.Rc1.size_hint = (.2, .2)
    self.Rc1.pos = (300, 250)

But more interesting is that you get also this widget in sss(self, widget): and you can do但更有趣的是,您还可以在sss(self, widget):获得此小部件,并且您可以这样做

def change_button(self, widget):
    widget.text = "Push Me !"
    widget.font_size = "20sp"
    widget.background_color = [1, 0, 0, 1]
    widget.color = (1, 1, 1, 1)
    widget.size = (32, 32)
    widget.size_hint = (.2, .2)
    widget.pos = (300, 250)

This way you can use the same function with different buttons这样你就可以用不同的按钮使用相同的功能

在此处输入图片说明

import kivy
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.uix.button import Button


class Touch(Widget):

    def __init__(self, **kwargs):
        super(Touch, self).__init__(**kwargs)
        
        #self.all_buttons = []
        
        for row in range(10):
            for col in range(10):
                x = col * 100
                y = row * 100
                button = Button(text=f"{row},{col}", pos = (x, y))
                button.bind(on_press=self.change_button)
                
                self.add_widget(button)
                
                #self.all_buttons.append(button)

    def change_button(self, widget):
        if widget.background_color == [1,1,1,1]:
            widget.background_color = [1, 0, 0, 1]
        else:
            widget.background_color = [1, 1, 1, 1]


class MyApp(App):

    def build(self):
        return Touch()

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

暂无
暂无

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

相关问题 TypeError:method()接受1个位置参数,但给出2个(Python和Kivy) - TypeError: method()takes in 1 positional argument but 2 were given (Python and Kivy) TypeError: function() 接受 1 个位置参数,但给出了 2 个 - TypeError: function() takes 1 positional argument but 2 were given 类型错误:<function> 接受 X 位置参数,但给出 X</function> - TypeError: <Function> takes X positional argument but X were given Python3 TypeError:接受1个位置参数,但给定2个 - Python3 TypeError: takes 1 positional argument but 2 were given Python 继承 - 类型错误:__init__() 采用 1 个位置参数,但给出了 4 个 - Python Inheritance - TypeError: __init__() takes 1 positional argument but 4 were given Python Selenium TypeError:__init__() 需要 1 个位置参数,但给出了 2 个 - Python Selenium TypeError: __init__() takes 1 positional argument but 2 were given Python tqdm TypeError: <lambda> ()接受1个位置参数,但给出了2个 - Python tqdm TypeError: <lambda>() takes 1 positional argument but 2 were given 类型错误:__init__() 需要 1 个位置参数,但给了 2 个 Python - TypeError: __init__() takes 1 positional argument but 2 were given Python TypeError:parse()接受1个位置参数,但给出了6个 - TypeError: parse() takes 1 positional argument but 6 were given 类型错误:需要 1 个位置参数,但给出了 2 个 - TypeError: takes 1 positional argument but 2 were given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM