gpt4 book ai didi

python - kivy 使用线程和 matplotlib 运行 python 代码

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:29 25 4
gpt4 key购买 nike

我创建了这个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。为此我向您推荐:

编辑:

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() 与任何代码分开,否则,如果您多次单击该按钮,您将启动其他线程。现在测试一下。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com