我尝试编写一个简单的时钟应用程序。效果很好,但是我想知道是否有办法使用Drawing()类中的simple_clock(App)中的方法并将信息存储在其中?
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Line
from kivy.core.image import Image
from kivy.uix.button import Button
from kivy.clock import Clock
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.gridlayout import GridLayout
class another_screen(Screen):
pass
class Drawing():
pass
presentation = Builder.load_file("simple_clock_kivy.kv")
class simple_clock(App):
sw_started = False
sw_seconds = 0
def update_clock(self, nap):
if self.sw_started:
self.sw_seconds += nap
print(self.sw_seconds)
def start_stop(self):
self.sw_started = True
def reset(self):
if self.sw_seconds:
self.sw_seconds = 0
else:
pass
def stop_clock(self):
self.sw_started = False
def on_start(self):
Clock.schedule_interval(self.update_clock, 0)
def build(self):
return presentation
if __name__=="__main__":
simple_clock().run()
<another_screen>:
name: "other"
FloatLayout:
Drawing:
id: other
Button:
id: start_stop
text: 'Start'
size_hint: 0.2, 0.05
pos_hint: {"x": 0, "bottom": 1}
on_press: app.start_stop()
Button:
id: reset
text: 'Reset'
size_hint: 0.2, 0.05
pos_hint: {"x": 0.6, "bottom": 1}
on_press: app.reset()
Button:
id: stop
text: 'Stop'
size_hint: 0.2, 0.05
pos_hint: {"x": 0.8, "bottom": 1}
on_press: app.stop_clock()
有人按下“开始”按钮,开始计时,开始绘制; “停止”按钮,时间停止。而且我想将绘图的持续时间保存到变量中。
答案 0 :(得分:0)
在class App()
以外的任何课程中
App.get_running_app()
获取对象app
App.get_running_app().update_clock()
App.get_running_app().sw_seconds