【问题标题】:missing 1 required positional argument dt缺少 1 个必需的位置参数 dt
【发布时间】:2019-10-10 17:33:16
【问题描述】:

我想借助这个在标签上显示每个计数增量 kivy.clock 包

class MainWindow(BoxLayout):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
    count=0
    def my_callback(self):
        global count
        count +=1
        us= self.ids.tet

        if count == 10:
            us.text=count
            return False
        print("call back is called")
        Clock.schedule_interval(self.my_callback, 1)

  count +=1

NameError: name 'count' 未定义

【问题讨论】:

  • 为什么要在 my_callback() 中定义 dt?没用
  • 哦,谢谢,我已经通过删除它尝试过了,但我得到了新错误 NameError: name 'count' is not defined
  • 你必须在外面定义“count=0”才能在你的函数中使用“global count”
  • 对不起,我是stackoverflow的新手,我想向你展示我的完整代码,所以请等待我正在编辑它
  • 你需要在类外定义计数

标签: python kivy


【解决方案1】:

按钮绑定 - on_press / on_release 事件

如何将 my_callback 函数绑定到按钮事件

解决方案 - 按钮 on_press 事件

以下 sn-ps 显示如何将 on_press 事件绑定到回调。

片段 - kv 文件

Button:
    text: 'Invoke method'
    on_press: root.btn_callback(self)

片段 - py 文件

def btn_callback(self, instance):
    print(f"\nButton.text={instance.text}")

使用 Kivy Clock - 调用 callback() 方法

当使用Kivy Clock object 来安排对方法的调用而不使用lambda 函数时,您必须声明一个位置参数dt(表示增量时间)。

片段

def my_callback(dt):
    print(dt)

Clock.schedule_interval(my_callback, 1)    # call callback every 1 second

def my_callback():
    print("my_callback called")

Clock.schedule_interval(lambda dt: callback(), 1)    # call callback every 1 second

示例

以下示例说明了显示定义为类属性的计数器。

main.py

​​>
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.properties import NumericProperty
from kivy.lang import Builder

Builder.load_string("""
<MainWindow>:
    Label:
        id: tet
        font_size: sp(50)
    Button:
        text: 'Invoke method'
        on_press: root.btn_callback(self)
""")


class MainWindow(BoxLayout):
    count = NumericProperty(0)

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        Clock.schedule_interval(self.my_callback, 1)

    def my_callback(self, dt):
        print(f"my_callback is called: count={self.count}, dt={dt}")
        self.count += 1

        if self.count == 10:
            self.ids.tet.text = str(self.count)

    def btn_callback(self, instance):
        print(f"\nButton.text={instance.text}\n")


class TestApp(App):

    def build(self):
        return MainWindow()


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

输出

【讨论】:

  • 谢谢 :),还有一个问题,我如何将 my_callback 函数绑定到按钮事件,我已经尝试过了,但是当我按下按钮时它会出错: TypeError: my_callback() 缺少 1 个必需的位置参数: 'dt'
  • 请参阅更新的帖子,将按钮的on_press 绑定到回调。
【解决方案2】:

试试这个-

class MainWindow(BoxLayout):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
    def my_callback(self):
        global count
        count +=1
        us= self.ids.tet

        if count == 10:
            us.text=count
            return False
        print("call back is called")
        Clock.schedule_interval(self.my_callback, 1)

count = 0

您必须在您正在创建的类之外定义count=0,并且说global count,您正在使这个变量成为全局变量,这意味着现在您的函数可以在全局范围内编辑该变量。如果你想了解范围,你应该在谷歌上搜索更多。

This might be helpful to begin with.

【讨论】:

    猜你喜欢
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 2019-11-12
    • 2019-12-23
    • 2021-02-01
    • 2020-11-15
    相关资源
    最近更新 更多