python 脚本为 运行 时如何在 kivy 中显示文本或弹出窗口
How to display text or Popup in kivy while python sript is running
大家好阅读这个话题。
我想做的是:在 if 语句中匹配值后自动在 Label 中显示文本。
第二个选项可以是 Popup insted Label。
第三种方式可以跳转到不同的屏幕。
请帮帮我,
我真的不知道如何在不使用任何按钮的情况下将它与 kivy 连接起来。
以下是代码:
python:
from kivy.app import App
import random
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class Main(ScreenManager):
pass
class Numbers(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def process(self):
num_list = ['four','three']
number = random.choice(num_list)
self.number.text = number
print(number)
while True:
if number == 'null':
pass
elif number == 'three':
pass
elif number == 'four':
self.root.ids.numb.text="four"
print(' for example Here i need connect code with kivy to display on screen text as string or jump to another screen')
break
else:
pass
kv = Builder.load_file('temp.kv')
class NumbersApp(App):
def build(self):
return kv
if __name__ == '__main__':
NumbersApp().run()
和kv文件:
Main:
Numbers:
<Numbers>:
number:numb
cols:1
RelativeLayout:
Label:
id: numb
text: 'Text display after getting \ninfo from script in while loop \nin process def'
font_size: '15'
hint_size: None, None
pos_hint: {'x': 0.04, 'y': -0.4}
color: 1,1,1,1
你的线路:
self.root.ids.numb.text="four"
不正确,因为 numb
id
在 Numbers
对象中,但您正试图访问 Numbers
对象的 root
属性.只需将该行更改为:
self.ids.numb.text = "four"
访问正确的ids
。
所以这里是您的 kv
的修改版本,其中包含一个 Button
以开始 process()
Main:
Numbers:
<Numbers>:
number:numb
cols:1
RelativeLayout:
Label:
id: numb
text: 'Text display after getting \ninfo from script in while loop \nin process def'
font_size: '15'
hint_size: None, None
pos_hint: {'x': 0.04, 'y': -0.4}
color: 1,1,1,1
Button:
text: 'Start Process'
size_hint: None, None
size: self.texture_size
pos_hint: {'center_x': 0.5, 'top': 1}
on_release: root.start_process()
这是您的 Numbers
class 的修改版本,它使用另一个 Thread
和 Clock.schedule_once()
:
class Numbers(Screen):
def start_process(self):
# run process() on another thread
threading.Thread(target=self.process, daemon=True).start()
def process(self):
print('process')
num_list = ['four','three']
number = random.choice(num_list)
print(number)
while True:
if number == 'null':
pass
elif number == 'three':
pass
elif number == 'four':
print(' for example Here i need connect code with kivy to display on screen text as string or jump to another screen')
Clock.schedule_once(self.set_text) # set text on the main thread
break
else:
pass
def set_text(self, dt):
self.ids.numb.text = "four"
大家好阅读这个话题。
我想做的是:在 if 语句中匹配值后自动在 Label 中显示文本。 第二个选项可以是 Popup insted Label。 第三种方式可以跳转到不同的屏幕。
请帮帮我, 我真的不知道如何在不使用任何按钮的情况下将它与 kivy 连接起来。 以下是代码:
python:
from kivy.app import App
import random
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class Main(ScreenManager):
pass
class Numbers(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def process(self):
num_list = ['four','three']
number = random.choice(num_list)
self.number.text = number
print(number)
while True:
if number == 'null':
pass
elif number == 'three':
pass
elif number == 'four':
self.root.ids.numb.text="four"
print(' for example Here i need connect code with kivy to display on screen text as string or jump to another screen')
break
else:
pass
kv = Builder.load_file('temp.kv')
class NumbersApp(App):
def build(self):
return kv
if __name__ == '__main__':
NumbersApp().run()
和kv文件:
Main:
Numbers:
<Numbers>:
number:numb
cols:1
RelativeLayout:
Label:
id: numb
text: 'Text display after getting \ninfo from script in while loop \nin process def'
font_size: '15'
hint_size: None, None
pos_hint: {'x': 0.04, 'y': -0.4}
color: 1,1,1,1
你的线路:
self.root.ids.numb.text="four"
不正确,因为 numb
id
在 Numbers
对象中,但您正试图访问 Numbers
对象的 root
属性.只需将该行更改为:
self.ids.numb.text = "four"
访问正确的ids
。
所以这里是您的 kv
的修改版本,其中包含一个 Button
以开始 process()
Main:
Numbers:
<Numbers>:
number:numb
cols:1
RelativeLayout:
Label:
id: numb
text: 'Text display after getting \ninfo from script in while loop \nin process def'
font_size: '15'
hint_size: None, None
pos_hint: {'x': 0.04, 'y': -0.4}
color: 1,1,1,1
Button:
text: 'Start Process'
size_hint: None, None
size: self.texture_size
pos_hint: {'center_x': 0.5, 'top': 1}
on_release: root.start_process()
这是您的 Numbers
class 的修改版本,它使用另一个 Thread
和 Clock.schedule_once()
:
class Numbers(Screen):
def start_process(self):
# run process() on another thread
threading.Thread(target=self.process, daemon=True).start()
def process(self):
print('process')
num_list = ['four','three']
number = random.choice(num_list)
print(number)
while True:
if number == 'null':
pass
elif number == 'three':
pass
elif number == 'four':
print(' for example Here i need connect code with kivy to display on screen text as string or jump to another screen')
Clock.schedule_once(self.set_text) # set text on the main thread
break
else:
pass
def set_text(self, dt):
self.ids.numb.text = "four"