我正在尝试学习Mark Vasilkov的"Kivy Blueprints“这本书。在第21页,他介绍了一个更新标签文本的函数。
项目文件夹中有两个文件(参见下面的代码)。在类ClockApp中,定义了函数update_time(self, nap)。我正在使用带有python插件的Intellij Idea社区,集成开发环境告诉我nap是一个未使用的参数。如果我删除nap作为参数,我会得到一个错误的update_time() takes 1 positional argument but 2 were given。怎样才能摆脱这个伪参数呢?
# Source: Chapter 1 of Kivy Blueprints
# File: main.py
from time import strftime
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.utils import get_color_from_hex
Window.clearcolor = get_color_from_hex("#101216")
# from kivy.core.text import LabelBase
class ClockApp(App):
def update_time(self, nap):
self.root.ids.time.text = strftime("[b]%H[/b]:%M:%S")
def on_start(self):
Clock.schedule_interval(self.update_time, 1)
if __name__ == "__main__":
ClockApp().run()还有一个额外的clock.kv文件
# File: clock.kv
BoxLayout:
orientation: "vertical"
Label:
id: time
text: "[b]00[/b]:00:00"
font_name: "Roboto"
font_size: 60
markup: True发布于 2018-08-09 15:51:02
绑定总是传递额外的信息,例如,在本例中,它向我们发送调用函数的确切时间段。如果你不想使用它,你可以使用lambda方法:
class ClockApp(App):
def update_time(self):
self.root.ids.time.text = strftime("[b]%H[/b]:%M:%S")
def on_start(self):
Clock.schedule_interval(lambda *args: self.update_time(), 1)如果您只想使警告“未使用的参数”静默,则可以使用_
class ClockApp(App):
def update_time(self, _):
self.root.ids.time.text = strftime("[b]%H[/b]:%M:%S")
def on_start(self):
Clock.schedule_interval(self.update_time, 1)https://stackoverflow.com/questions/51761130
复制相似问题