Kivy 和 Python 线程 - 如何在它们之间获取数据
Kivy and Python threading - how get data between them
我在使用 python(线程)和 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)
两者都出现错误"Attribute Error: 'MyApp' object has no attribute 'button'"。
有没有什么方法可以直接交换数据而不需要请求,甚至不需要在这里用 "thread_object"
做指针的事情
def __init__ (self, thread_object):
感谢您的帮助。
这可能会解决您所有的问题。这就是我在处理线程和 kivy 语言时喜欢的编码方式。
这里是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 方面遇到困难。所以,这里有一个例子。
threading.py
import threading
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty
from kivy.lang.builder import Builder
from kivy.uix.button import Button
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()
if __name__ == "__main__":
app = MyApp()
app.run()
我在使用 python(线程)和 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)
两者都出现错误"Attribute Error: 'MyApp' object has no attribute 'button'"。
有没有什么方法可以直接交换数据而不需要请求,甚至不需要在这里用 "thread_object"
做指针的事情def __init__ (self, thread_object):
感谢您的帮助。
这可能会解决您所有的问题。这就是我在处理线程和 kivy 语言时喜欢的编码方式。
这里是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 方面遇到困难。所以,这里有一个例子。 threading.py
import threading
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty
from kivy.lang.builder import Builder
from kivy.uix.button import Button
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()
if __name__ == "__main__":
app = MyApp()
app.run()