使用 kivymd 制作應用程式以盡可能快地掃描條形碼,我創建了這個應用程式,它帶有一個文本欄位,該欄位將獲取 QR 碼并存盤它,如果用戶輸入一次,焦點將消失,必須手動聚焦它才能從用戶那里獲得另一個輸入。有可能在不失去注意力的情況下進行多次輸入嗎?
主檔案
from kivymd.app import MDApp
from kivymd.uix.screen import Screen
class Layout(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def check(self):
item = self.ids.qrcode
print(item.text)
class Core(MDApp):
def build(self):
self.theme_cls.primary_palette = "Green"
return Layout()
if __name__ == '__main__':
Core().run()
內核檔案
<Layout>:
cols:1
MDTextField:
id: qrcode
hint_text: "QR Code"
focus: True
helper_text: "Enter Shipment QR"
helper_text_mode: "on_focus"
icon_right: "qrcode-scan"
icon_right_color: app.theme_cls.primary_color
pos_hint:{'center_x': 0.5, 'center_y': 0.9}
size_hint_x:None
width:300
on_text_validate: root.check()
uj5u.com熱心網友回復:
如果你放入self.ids.qrcode.focus = True你的check方法,它會被過早呼叫。你會專注,然后就會失去焦點。
您可以Clock.schedule_once(self.refocus_ti)在您的check方法中使用以下方法:
def refocus_ti(self, *args):
self.ids.qrcode.focus = True
使用上面的代碼,它將在您的check方法之后重新聚焦文本欄位。
完整的代碼片段:
# core.kv
<Layout>:
cols:1
qrTextInput: qrcode
MDTextField:
id: qrcode
hint_text: "QR Code"
focus: True
helper_text: "Enter Shipment QR"
helper_text_mode: "on_focus"
icon_right: "qrcode-scan"
icon_right_color: app.theme_cls.primary_color
pos_hint:{'center_x': 0.5, 'center_y': 0.9}
size_hint_x:None
width:300
on_text_validate: root.check()
from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.clock import Clock
Builder.load_file("kv/core.kv")
class Layout(Screen):
qrTextInput = ObjectProperty(None)
def __init__(self, **kwargs):
super().__init__(**kwargs)
def check(self):
item = self.ids.qrcode # Or access with self.qrTextInput.text
print(item.text)
Clock.schedule_once(self.refocus_ti)
def refocus_ti(self, *args):
self.qrTextInput.focus = True
class Core(MDApp):
def build(self):
self.theme_cls.primary_palette = "Green"
return Layout()
if __name__ == '__main__':
Core().run()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/394372.html
