我创建了这个python代码:
import threading
from drawnow import drawnow
import matplotlib.pyplot as plt
import random
import time
stop = []
timer = []
times = []
plt.ion()
class MyThread(threading.Thread):
def run(self):
c = 30
print 'TIMER START'
while not stop:
timer.append(c)
time.sleep(1)
c -= 1
print 'TIMER STOP', c, ","
times.append(c)
return c
def makefig():
#plt.ylim(0,5)
plt.plot(times, 'ro-', label='testgraph')
plt.grid(True)
def main_loop():
i = 0
while True:
i += 1
time.sleep(random.randint(0,3))
if i == 7:
i = 0
yield i
if __name__ == '__main__':
z = main_loop()
for x in z:
print x
print times
drawnow(makefig)
if x == 2:
m = MyThread()
m.start()
if x == 5:
stop.append('a')
if x == 6:
stop.pop(0)
timer = []
它让我从0到7返回数字,在生成新数字和运行倒数计时器之间的不同时间值介于值2和2之间。 5并将计数器值附加到列表中。我在matplotlib中绘制列表值。
问题:
如何使用kivy执行此代码,以便kivy显示计数器值而不是终端,所以kivy绘制值而不是matplotlib?
编辑:
这是我的代码到目前为止,但我无法解决相同的线程功能:
import threading
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty
import threading
from drawnow import drawnow
import matplotlib.pyplot as plt
import random
import time
class MyThread(BoxLayout):
#stop = []
#timer = []
#times = []
i = NumericProperty(0)
def run(self):
c = 30
print 'TIMER START'
while not stop:
timer.append(c)
time.sleep(1)
c -= 1
print 'TIMER STOP', c, ","
times.append(c)
return c
def main_loop(self):
while True:
self.ids.lbl.text = "{}".format(self.i)
print self.i
self.i += 1
time.sleep(random.randint(0,3))
if self.i == 7:
self.i = 0
def read_it(self):
threading.Thread(target = self.main_loop).start()
print 'started'
if self.i == 2:
print "Counter start"
if self.i == 5:
print "Counter stop"
class MyApp(App):
def build(self):
self.load_kv('thread.kv')
return MyThread()
if __name__ == "__main__":
app = MyApp()
app.run()
EDIT2:
我忘了展示thread.kv文件:
<MyThread>:
Button:
text: "use thread"
on_release: root.read_it()
Label:
id: lbl
text: "Numbers"
答案 0 :(得分:1)
为了绘图,花园里有Graph,如果你不想单独使用图形说明,这将使你的生活更轻松 - 虽然这是一个很好的做法。
在Kivy,您可以轻松访问,例如具有此属性的小部件中的.text
属性,即Label
,TextInput
和其他一些属性。
mylabel = Label(text=<string>)
mylabel.text = <string 2>
你所要求的基本上是“将python转换为kivy”,因为除了python-only代码之外,我没有看到你所尝试的任何东西 - 不好看,你不会学习kivy。为此我建议你:
inclement的tutorial和Crash Course(视频)
修改强>
from kivy.lang import Builder
Builder.load_string('''
<MyThread>:
Button:
text: "use thread"
on_release: root.run_thread()
Button:
text: "check values"
on_release: root.read_it()
Label:
id: lbl
text: "Numbers"
''')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty
import threading
import random
import time
class MyThread(BoxLayout):
i = NumericProperty(0)
def main_loop(self):
while True:
self.ids.lbl.text = "{}".format(self.i)
print self.i
self.i += 1
time.sleep(random.randint(0,3))
if self.i == 7:
self.i = 0
def run_thread(self):
threading.Thread(target = self.main_loop).start()
def read_it(self):
print 'started'
if self.i == 2:
print "Counter start"
if self.i == 5:
print "Counter stop"
class MyApp(App):
def build(self):
return MyThread()
MyApp().run()
您也可以选择on_* functions:
def on_i(self, *args):
print i # when i changes
Edit2:我删除了run()
,因为在该特定代码中无论如何都没有使用它。如果要再次调用该函数,则需要将Thread.start()与任何代码分开,否则如果在该按钮上单击多次,则将启动其他Thread。现在测试一下。