ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Kivy和Python线程-如何在它们之间获取数据

2019-11-20 02:56:24  阅读:294  来源: 互联网

标签:kivy multithreading python


我在python(threading)和kivy上遇到一些问题:

这是一些代码:

import kivy
import threading 
import time
from kivy.app import App
from kivy.uix.button import Button

class Thread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)  
        self.counter = 0        
    def run(self):
        while True:
            print "Thread is running "+str(self.counter)
            app.button.text = self.set_button(self.counter)
            app.button.text = str(self.counter)
            time.sleep(0.5)
    def count(self):
        self.counter += 1
        app.button.text = str(self.counter)     
    def set_button(self, value):
        app.button.text = str(value)

class MyApp(App):
    def __init__ (self, thread_object):
        App.__init__(self) 
        self.thread_object = thread_object      
    def callback(self,instance):
        print('The button <%s> is being pressed' % instance.text)
        self.thread_object.count()
    def build(self):
        self.button = Button(text='Hello World')
        self.button.bind(on_press=self.callback)
        return self.button

thread = Thread()
thread.start()
app = MyApp(thread)
app.run()

现在-此代码只需一个按钮即可打开kivy应用程序.任务是:按下按钮,一些数据应显示在线程代码中(通过“ count”方法执行).

问题是相反的-线程代码应更改按钮的文本.我尝试了两种方法:

>直接编写:app.button.text = str(self.counter)
>通过“ set_button”方法编写:app.button.text = self.set_button(self.counter)

它们都显示错误“属性错误:’MyApp’对象没有属性’按钮’”.

有没有任何方法可以直接交换数据而无需请求,即使不使用“ thread_object”在此处进行指针操作

def __init__ (self, thread_object):

谢谢你的帮助.

解决方法:

这可能会解决您的所有问题.那就是我喜欢在处理线程和奇异语言时进行编码的方式.

这是thread.py文件

import threading   
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty

class Thread(BoxLayout):
    counter = NumericProperty(0)

    def Counter_function(self):
        self.counter += 1
        self.ids.lbl.text = "{}".format(self.counter)

    def First_thread(self):
        threading.Thread(target = self.Counter_function).start()
        self.counter += 1
        self.ids.lbl.text = "{}".format(self.counter)

class MyApp(App):
    def build(self):
        self.load_kv('thread.kv')
        return Thread() 

if __name__ == "__main__":
    app = MyApp()
    app.run()

这是thread.kv文件

<Thread>:
    Button:
        text: "use thread"
        on_release: root.First_thread()
    Button:
        text: "Hit me"
        on_release: root.Counter_function()
    Label:
        id: lbl
        text: "Numbers"

现在,您在评论中说,您在动态加载GUI方面遇到困难.所以,这是一个例子.

Builder.load_string('''
[SideBar@BoxLayout]:
    content: content
    orientation: 'vertical'
    size_hint: .2,1
    BoxLayout:
        orientation: 'vertical'
        # just add a id that can be accessed later on
        id: content

<Root>:
    Button:
        center_x: root.center_x
        text: 'press to add_widgets'
        size_hint: .2, .2
        on_press:
            sb.content.clear_widgets()
            root.load_content(sb.content)
    SideBar:
        id: sb
''')

class Root(BoxLayout):

    def load_content(self, content):
        for but in range(20):
            content.add_widget(Button(text=str(but)))

class MyApp(App):
    def build(self):
        return Root()

标签:kivy,multithreading,python
来源: https://codeday.me/bug/20191120/2041032.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有