Python Kivy 蓝图一书中的 kivy 代码
Python kivy code from book Kivy Blueprints
我正在尝试阅读 Mark Vasilkov 的书 "Kivy Blueprints"。在第 21 页,他介绍了更新标签文本的功能。
项目文件夹中有两个文件(见下面的代码)。在 class ClockApp 中定义了函数 update_time(self, nap)
。我正在使用带有 python 插件的 Intellij Idea Community,集成开发环境 (IDE) 告诉我 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
绑定总是传递附加信息,例如在本例中,它向我们发送调用函数的确切时间段。如果你不想使用它,你可以使用 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)
如果您只想消除警告:"unused parameter" 您可以使用 _
:
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)
我正在尝试阅读 Mark Vasilkov 的书 "Kivy Blueprints"。在第 21 页,他介绍了更新标签文本的功能。
项目文件夹中有两个文件(见下面的代码)。在 class ClockApp 中定义了函数 update_time(self, nap)
。我正在使用带有 python 插件的 Intellij Idea Community,集成开发环境 (IDE) 告诉我 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
绑定总是传递附加信息,例如在本例中,它向我们发送调用函数的确切时间段。如果你不想使用它,你可以使用 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)
如果您只想消除警告:"unused parameter" 您可以使用 _
:
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)