【发布时间】:2022-01-24 10:34:20
【问题描述】:
大家好,
请是 python 的新手,我正在做一个小项目。 该项目根据扫描的条形码生成输入字段,每个输入字段都有一个唯一的 id。现在我的挑战是如何将特定输入字段的实例绑定或传递给函数或获取当前已修改的输入字段的 ID 其次,当您单击删除产品时,我希望将产品附加到要删除的按钮上
from kivy.lang import Builder
from kivy.factory import Factory
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen
from kivymd.uix.textfield import MDTextField
from kivymd.uix.button import MDRectangleFlatButton
import weakref
from kivy.properties import DictProperty
from kivy.properties import ObjectProperty
Builder.load_string(
"""
<Windows>:
ScrollView:
MDBoxLayout:
orientation: 'vertical'
padding: '48dp'
spacing: '15dp'
size_hint_y: None
height: self.minimum_height
id: data_layout
MDRaisedButton:
text: "open_window 2"
on_release: root.opener()
"""
)
class Windows(Screen):
got_txt = ObjectProperty()
dynamic_ids = DictProperty({})
def __init__(self, **kwargs):
super().__init__(**kwargs)
def opener(self):
#print(window_id)
#self.layout = GridLayout(cols = 5, row_force_default=True, row_default_height=40)
#window_id.open()
#self.table()
self.read_barcodes()
def build(self):
self.root = Factory.Windows()
def read_barcodes(self):
barcode = ['sfdfdf','fdfdfdfd','sdfdfd']
for barcode_info in barcode:
#d =str( str(len(self.tables_data) + 1) + '',''+barcode_info)
#self.tables_data.append('('+barcode_info+')')
product_code_id = barcode_info + "_product_code"
product_name_id = barcode_info + "_product_name"
product_qty_id = barcode_info + "_product_qty"
product_price_id = barcode_info + "_product_price"
product_amount_id = barcode_info + "_product_amount"
product_code = MDTextField(mode= "rectangle", text=str(barcode_info), readonly=True, opacity=0)
self.ids.data_layout.add_widget(product_code)
# We'll use a weak ref to add our dynamic id
self.ids[product_code_id] = weakref.ref(product_code)
self.dynamic_ids[id] = product_code_id
product_name = MDTextField(hint_text="Product Name",mode= "rectangle", text=str(barcode_info), readonly=True)
#product_name.bind(on_focus=self.focusedInpute())
self.ids.data_layout.add_widget(product_name)
# We'll use a weak ref to add our dynamic id
self.ids[product_name_id] = weakref.ref(product_name)
self.dynamic_ids[id] = product_name_id
product_qty = MDTextField(hint_text="qty",mode= "rectangle", text=str(1))
self.ids.data_layout.add_widget(product_qty)
# We'll use a weak ref to add our dynamic id
self.ids[product_qty_id] = weakref.ref(product_qty)
self.dynamic_ids[id] = product_qty_id
product_price = MDTextField(hint_text="U.Price",mode= "rectangle",text=str(200))
self.ids.data_layout.add_widget(product_price)
# We'll use a weak ref to add our dynamic id
self.ids[product_price_id] = weakref.ref(product_price)
self.dynamic_ids[id] = product_price_id
product_amount = MDTextField(hint_text="Amount",mode= "rectangle", text=str(200))
self.ids.data_layout.add_widget(product_amount)
# We'll use a weak ref to add our dynamic id
self.ids[product_amount_id] = weakref.ref(product_amount)
self.ids.data_layout.add_widget(MDRectangleFlatButton(text ="Remove Product"))
#self.ids.data_layout.add_widget(layout) , on_release=self.Remove_line(barcode_info)
#self.Reload_Table()
print("The id is " + str(self.ids[product_code_id]))
#print("The text is " + str(self.ids[product_code_id].text))
self.scaned = barcode_info
print(self.dynamic_ids)
def Remove_line(self, widgets):
print('hi')
#self.ids.data_layout.remove_widget(widgets)
#self.ids.data_layout.remove_widget(self.ids[widgets + "_product_code"])
#self.ids.data_layoutr.emove_widget(self.ids[widgets + "_product_name"])
#self.ids.data_layout.remove_widget(self.ids[widgets + "_product_qty"])
#self.ids.data_layoutr.remove_widget(self.ids[widgets + "_product_price"])
#self.ids.data_layoutr.remove_widget(self.ids[widgets + "_product_amount"])
#remove_widget()
def focusedInpute(self,insatnce, value):
#print(self)
# print(value)
if not value: # defocused
print(value)
class MainApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def build(self):
self.root = Factory.Windows()
if __name__ == "__main__":
MainApp().run()
【问题讨论】:
-
谢谢,我已将代码修改为可运行的sn-p。 @约翰安德森
-
那仍然不是
minimal。请修改您的示例以消除使用pyzbar和条形码扫描。无论这些事情在哪里完成,都只是制造虚假结果。 -
又修改了@JohnAnderson
-
代码
self.dynamic_ids[id] = product_code_id中的id是什么?
标签: python python-3.x kivy kivymd