gpt4 book ai didi

python - TextInput 中的文本未更新

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

我正在尝试使用kv语言重做我已经完成的程序。第一个是用 python 完成的。现在,当我使用 kivy 语言时,我无法从用户界面中获取任何内容。在这个例子中。当条形码文本更改时,它应该更新它的文本属性,然后触发我的函数。问题是它无法识别文本输入框中的更改。

main6.py

class Barcode(TextInput):

l_prompt= ObjectProperty()
btext = StringProperty()

# capture barcode
def onText(self):
print ('in onText')
c=True
while c:
print('in loop')
time.sleep(1/10)
print(self.text)
if len(self.text) <=5:
c= True
else:
c = False
print('in else')
self.l_prompt.change_label('test')
Clock.schedule_once(self.parent.run_test,1)

class MainLayout(FloatLayout):

dt = str(datetime.today())
pafa = '' #pass/fail
barcode = '' #barcode number
counter = 0 #item counter

prompt_wid = ObjectProperty()
barcode_wid = ObjectProperty(rebind = True)
passfail_wid = ObjectProperty()
counterlabel_wid = ObjectProperty()


def __init__(self, **kwargs):
super(MainLayout, self).__init__(**kwargs)

#start test
#self.starttest(self)

self.l=Label()
#self.barcode_wid.bind(text=self.l.setter('text'))

def detect(self, d):
#detect when device is plugged in
t = True
while t:
time.sleep(1)
dev = usb.core.find(idVendor= 0x2996)

if dev is None:


t=False

self.prompt_wid.change_label('scan') #label change
#self.barcode_wid.focus=True #brings cursor up for input scan


j = threading.Thread(target=self.barcode_wid.onText)
j.daemon = True
j.start()

ex6.kv

#: kivy 1.9


<MainLayout>:
orientation: 'vertical'
size: root.size

prompt_wid: prompt
barcode_wid: barcode
passfail_wid: passfail
counterlabel_wid: counterlabel

canvas.before:
Color:
rgb: 0, 0, .6
Rectangle:
pos: self.pos
size: self.size

Prompt:
id: prompt

canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
pos: self.pos
size: self.size
Barcode:
id: barcode
l_prompt: prompt
focus: 'True'
text:'hey'

<Barcode>:

pos_hint: {'center_x': .5, 'top': 0.666 }
size_hint:(1,.3)
font_size: self.height/3
text: ''
multiline: 'False'
padding: 0,(self.height/3)

presentation = Builder.load_file('ex6.kv')


class MainApp(App):
def build(self):
ml = MainLayout()
Clock.schedule_interval(ml.detect, 2)
return MainLayout()


if __name__=="__main__":
MainApp().run()

我正在尝试使用 Kivy 属性,但我认为我缺少 py 和 kv 文件之间的基本链接,因为我的标签 (l_prompt) 也没有更新。一切都在幕后运行,但 UI 没有变化,除非在文本输入框中输入字符。

最佳答案

您的程序绝不会调用 onText 函数。您是否打算将其命名为 on_text,当 text 属性更改时会自动调用它?

关于python - TextInput 中的文本未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32656427/

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