我创建了以下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和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()


编辑2:

我已经放弃显示thread.kv文件:

<MyThread>:
    Button:
        text: "use thread"
        on_release: root.read_it()
    Label:
        id: lbl
        text: "Numbers"

最佳答案

对于绘图,花园中有Graph,如果您不想只玩图形指令,这将使您的生活更轻松-尽管这是一种不错的做法。

在Kivy中,您可以轻松访问例如具有此属性的小部件中的.text属性,即LabelTextInput和其他一些属性。

mylabel = Label(text=<string>)
mylabel.text = <string 2>


您要问的基本上是“将python转换为kivy”,因为除了仅使用python的代码外,我什么都没尝试过-不好,您不会学习kivy。为此,我建议您:


kivy.org tutorials
inclement的tutorialCrash Course(视频)
Python Threads with Kivy


编辑:

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。现在进行测试。

关于python - kivy 与线程和matplotlib运行python代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37797965/

10-13 07:37