【问题标题】:function takes exactly 2 arguments (1 given) kivy timer函数只需要 2 个参数(1 个给定) kivy timer
【发布时间】:2016-06-13 05:59:37
【问题描述】:

我试图让用户输入时间,当他们启动计时器时,用户插入的时间将从输入的时间开始倒计时。

但是我得到错误 updatetimer() 恰好需要 2 个参数(给定 1 个)

从输入返回值的最佳方法是什么,以便在脚本的其余部分中使用它。

这是我正在使用的代码:

#!/usr/bin/kivy
import kivy

from random import random
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from random import random
from random import choice
from kivy.properties import StringProperty
import time
from kivy.clock import Clock

from functools import partial


check=0

Builder.load_string("""
<MenuScreen>:
    GridLayout:
        cols: 1
        TextInput:
            id: ti
        Button:
            text: 'do something!'
            on_press: root.updatetimer(ti.text)
        Button:
            id: btn_0
            text: "press me to start timer"
            on_press: root.val0()
        Label:
        text:'timer'
    Label:
            id: timer
            text:str(30)
""")

class MenuScreen(Screen):
    def updatetimer(self,timeup):
        return timeup


    def val0(self):
        global check
        if(check==0):
            check=check+1
            Clock.schedule_interval(partial(my_callback, self), 1)

test = MenuScreen()

def my_callback(screen, dt):
    t = test.updatetimer()
    t -= 1
    screen.ids.timer.text = str(t)

sm = ScreenManager()
menu_screen = MenuScreen(name='menu')
sm.add_widget(menu_screen)

class TestApp(App):

    def build(self):
        return sm 

提前感谢您的建议

【问题讨论】:

  • 在您的函数my_callback(screen, dt) 中有以下语句:t = test.updatetimer()。对象test 被认为是第一个参数(self),但您仍然拥有timeup。所以这个错误是有道理的
  • 应该将 t = test.updatetimer() 更改为什么,以使 t 等于用户输入的时间?

标签: python timer kivy


【解决方案1】:

首先,我会稍微整理一下代码,并让方法名称更具描述性并保留在类中。

Builder.load_string("""
<MenuScreen>:
    GridLayout:
        cols: 1
        TextInput:
            id: ti
        Button:
            id: btn_0
            text: "press me to start timer"
            on_press: root.startTimer(int(ti.text))
        Label:
            id: output
            text: "Here"
""")

class MenuScreen(Screen):
    time = 0

    def printTime(self, t):
        self.ids.output.text = str(t)

    def tick(self, *largs):
        self.time -= 1
        self.printTime(self.time)

    def startTimer(self, t):
        self.printTime(t)
        self.time = t
        Clock.schedule_interval(partial(self.tick), 1)

这里我们在类上有 3 个方法,一个在开始时调用,一个每次迭代,一个打印辅助函数。当然,这个计数器不会停在 0,这很可能是你想要的,但我会把这部分留给你。

请记住,类中的方法会将self 作为其第一个参数传入。如果你想最终取消它,我会看看 Clock.schedule_interval 方法的作用:

https://kivy.org/docs/api-kivy.clock.html#kivy.clock.ClockBase.schedule_interval

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 2014-09-04
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 2018-08-20
    相关资源
    最近更新 更多