我正在尝试用计时器模块在指定的时间内使用kivy框架显示一些标签。这是我所拥有的,也是我想要包含在我的python kivy中的:
from threading import Timer
time_var = 2
def time():
print(time_var)
t = Timer(time_var, time)
t.start()
这是我的python kivy代码:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.config import Config
from threading import Timer
Config.set("graphics", "resizable", 0)
Config.set("graphics", "width", 400)
Config.set("graphics", "height", 500)
time_var = 2
class myLayout(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.t = Timer(time_var, self.timer)
layout = BoxLayout(orientation="vertical",
padding=20,
spacing=10)
btn = Button(text="Click")
btn.bind(on_press=self.t.start())
self.add_widget(layout)
layout.add_widget(btn)
def timer(self, obj):
popup = Popup(content=Label(text="timer"),
size_hint=(None, None),
size=(300, 200))
return popup.open()
class ReminderApp(App):
def build(self):
return myLayout()
if __name__ == "__main__":
ReminderApp().run()
但我想不出怎么才能让它工作。也许有一种方法可以使用kivy模块来实现。请帮帮我,我被卡住了..
发布于 2018-05-31 05:14:16
我想我弄明白了..。如果我错了,请纠正我
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.config import Config
from threading import Timer
Config.set("graphics", "resizable", 0)
Config.set("graphics", "width", 400)
Config.set("graphics", "height", 500)
time_var = 2
class myLayout(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
layout = BoxLayout(orientation="vertical",
padding=20,
spacing=10)
btn = Button(text="Click")
self.add_widget(layout)
layout.add_widget(btn)
btn.bind(on_press=self.timer)
def popup(self):
popup = Popup(content=Label(text="timer"),
size_hint=(None, None),
size=(300, 200))
popup.open()
def timer(self, obj):
timer = Timer(time_var, self.popup)
timer.start()
class ReminderApp(App):
def build(self):
return myLayout()
if __name__ == "__main__":
ReminderApp().run()
https://stackoverflow.com/questions/50613187
复制相似问题