日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Kivy和Python線程-如何在它們之間獲取數(shù)據(jù)

 印度阿三17 2019-11-20

我在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()

現(xiàn)在-此代碼只需一個(gè)按鈕即可打開kivy應(yīng)用程序.任務(wù)是:按下按鈕,一些數(shù)據(jù)應(yīng)顯示在線程代碼中(通過“ count”方法執(zhí)行).

問題是相反的-線程代碼應(yīng)更改按鈕的文本.我嘗試了兩種方法:

>直接編寫:app.button.text = str(self.counter)
>通過“ set_button”方法編寫:app.button.text = self.set_button(self.counter)

它們都顯示錯(cuò)誤“屬性錯(cuò)誤:’MyApp’對象沒有屬性’按鈕’”.

有沒有任何方法可以直接交換數(shù)據(jù)而無需請求,即使不使用“ thread_object”在此處進(jìn)行指針操作

def __init__ (self, thread_object):

謝謝你的幫助.

解決方法:

這可能會(huì)解決您的所有問題.那就是我喜歡在處理線程和奇異語言時(shí)進(jìn)行編碼的方式.

這是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"

現(xiàn)在,您在評論中說,您在動(dòng)態(tài)加載GUI方面遇到困難.所以,這是一個(gè)例子.

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()
來源:https://www./content-1-567301.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多