In my code, I used the same code I read to change the label widget to data, which is received from the database. I see data from MySql like "MARVEL’S SPIDER-MAN 2," but the program is showing an error.
productlistview.py
from kivy.app import App
from kivy.uix.screenmanager import Screen
from globalstate import GlobalState
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.gridlayout import GridLayout
import connect_db
Builder.load_file('productlistview.kv')
class ProductListViewMain(Screen):
def on_enter(self, *args):
GlobalState.is_user_authenticated = True
# Check user authentication status
if not GlobalState.is_user_authenticated:
# If not authenticated, go to the login screen
self.manager.current = 'home'
return
class SearchProduct(GridLayout):
pass
class ProductList(GridLayout):
def __init__(self, **kwargs):
super(ProductList, self).__init__(**kwargs)
self.load_products()
def load_products(self):
# Fetch data from MySQL
mydb = connect_db.db
cursor = mydb.cursor()
sql = "SELECT * FROM Product"
cursor.execute(sql)
fetch = cursor.fetchone()
# Close the connection
mydb.close()
if fetch:
print(fetch[1]) # MARVEL’S SPIDER-MAN 2
self.ids.product_name.text = str(fetch[1]) # Error
class ProductListViewApp(App):
def build(self):
self.root = ProductListViewMain()
return self.root
if __name__ == '__main__':
ProductListViewApp().run()
productlistview.kv
:
SearchProduct:
ProductList:
:
cols: 1
canvas:
Color:
rgba: 0.898039, 0.898039, 0.909804, 1
Rectangle:
pos: self.pos
size: self.size
RelativeLayout:
TextInput:
hint_text: 'Search'
font_size: 45
font_family: 'Roboto'
background_color: 0.952941, 0.952941, 0.952941, 1
foreground_color: 0.482353, 0.552941, 0.576471, 1
size_hint_y: None
height: 70
size_hint_x: None
width: 850
pos_hint: {'center_x':0.5, 'center_y':0.85}
:
cols: 5
row_force_default: True
row_default_height: 350
col_force_default: True
col_default_width: 250
BoxLayout:
Image:
id: product_image
allow_stretch: True
size_hint_y: None
size_hint_x: None
height: 250
width: 250
pos_hint: {'center_x': .5, 'center_y': .5}
BoxLayout:
Label:
id: product_name
font_family: 'Roboto'
font_size: 30
color: 0.482353, 0.552941, 0.576471, 1
size_hint: (0.146341, 0.0956522)
pos_hint: {'center_x': .5, 'center_y': .5}
BoxLayout:
Label:
id: product_description
text: 'Description'
font_family: 'Roboto'
font_size: 30
color: 0.482353, 0.552941, 0.576471, 1
size_hint: (0.146341, 0.0956522)
pos_hint: {'center_x': .5, 'center_y': .5}
BoxLayout:
Label:
id: product_price
text: 'Price'
font_family: 'Roboto'
font_size: 30
color: 0.482353, 0.552941, 0.576471, 1
size_hint: (0.146341, 0.0956522)
pos_hint: {'center_x': .5, 'center_y': .5}
BoxLayout:
Button:
id: add_cart
text: 'add to cart'
size_hint: (0.243902, 0.182609)
foreground_color: 0.482353, 0.552941, 0.576471, 1
background_color: 0.811765, 0.847059, 0.862745, 1
font_size: 30
pos_hint: {'center_x': .5, 'center_y': .5}
on_press: root.test()
Error
[INFO ] [Text ] Provider: sdl2
[INFO ] [GL ] NPOT texture support is available
MARVEL’S SPIDER-MAN 2
Traceback (most recent call last):
File "kivy/properties.pyx", line 961, in kivy.properties.ObservableDict.__getattr__
KeyError: 'product_name'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/fluke/Desktop/SEP410/Apps/productlistview.py", line 55, in
ProductListViewApp().run()
File "/Users/fluke/Desktop/SEP410/Apps/.venv/lib/python3.11/site-packages/kivy/app.py", line 955, in run
self._run_prepare()
File "/Users/fluke/Desktop/SEP410/Apps/.venv/lib/python3.11/site-packages/kivy/app.py", line 925, in _run_prepare
root = self.build()
^^^^^^^^^^^^
File "/Users/fluke/Desktop/SEP410/Apps/productlistview.py", line 51, in build
self.root = ProductListViewMain()
^^^^^^^^^^^^^^^^^^^^^
File "/Users/fluke/Desktop/SEP410/Apps/.venv/lib/python3.11/site-packages/kivy/uix/relativelayout.py", line 274, in __init__
super(RelativeLayout, self).__init__(**kw)
File "/Users/fluke/Desktop/SEP410/Apps/.venv/lib/python3.11/site-packages/kivy/uix/floatlayout.py", line 65, in __init__
super(FloatLayout, self).__init__(**kwargs)
File "/Users/fluke/Desktop/SEP410/Apps/.venv/lib/python3.11/site-packages/kivy/uix/layout.py", line 76, in __init__
super(Layout, self).__init__(**kwargs)
File "/Users/fluke/Desktop/SEP410/Apps/.venv/lib/python3.11/site-packages/kivy/uix/widget.py", line 366, in __init__
self.apply_class_lang_rules(
File "/Users/fluke/Desktop/SEP410/Apps/.venv/lib/python3.11/site-packages/kivy/uix/widget.py", line 470, in apply_class_lang_rules
Builder.apply(
File "/Users/fluke/Desktop/SEP410/Apps/.venv/lib/python3.11/site-packages/kivy/lang/builder.py", line 540, in apply
self._apply_rule(
File "/Users/fluke/Desktop/SEP410/Apps/.venv/lib/python3.11/site-packages/kivy/lang/builder.py", line 662, in _apply_rule
self._apply_rule(
File "/Users/fluke/Desktop/SEP410/Apps/.venv/lib/python3.11/site-packages/kivy/lang/builder.py", line 658, in _apply_rule
child = cls(__no_builder=True)
^^^^^^^^^^^^^^^^^^^^^^
File "/Users/fluke/Desktop/SEP410/Apps/productlistview.py", line 29, in __init__
self.load_products()
File "/Users/fluke/Desktop/SEP410/Apps/productlistview.py", line 45, in load_products
self.ids.product_name.text = str(fetch[1])
^^^^^^^^^^^^^^^^^^^^^
File "kivy/properties.pyx", line 964, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'. Did you mean: '__setattr__'?
0 comments:
Post a Comment
Thanks