根据 .kv Kivy 中的 id 显示图像的问题
Problem to show image according to id in .kv Kivy
我在 python kivy 中有以下脚本,它计算数据库中 table 中有多少条记录,并根据总数显示图像 n[] 次。
如果总记录是 4 并且我执行 print[numero]
它打印 0,1,2,3,没关系。
但是当我想显示图像“weight.png”4 次时,它没有显示并给我一个错误:AttributeError: 'super' object has no attribute '__getattr__'
此处代码:
rollo.py
from email.mime import image
from itertools import count
from logging import root
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.config import Config
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen
import time
import serial
import psycopg2
from datetime import datetime, timezone
from kivy.uix.image import Image
from kivy.properties import StringProperty
from inicio import Inicio
class Rollo(Screen):
def __init__(self, **kwargs):
super(Rollo, self).__init__(**kwargs)
print('inicie')
#connect DB
con = psycopg2.connect(
host = "localhost",
database = "appsenior",
user = "postgres",
password = "qq")
cur = con.cursor()
#contar cantidad de rollos pesados
consulta = "SELECT count(*) pesos FROM peso_materia WHERE id_parada=100"
cur.execute(consulta)
rows = cur.fetchone()
numero = 0
#example rows[0] = 4 loop con 4
while numero < rows[0]:
#It should show me the image peso.png in the corresponding image id
self.ids.carga[numero] = StringProperty('peso.png')
numero = numero + 1
kivy.require('2.0.0')
Config.set('graphics', 'resizable', True)
Builder.load_file('rollo.kv')
class Root(GridLayout):
pass
class Main(App):
def build(self):
return Root()
if __name__ == '__main__':
Main().run()
rollo.kv
<Rollo@Screen>
name:'rollo'
canvas:
Color:
rgba: .9,.9,.9,1
Rectangle:
size: self.size
BoxLayout:
orientation: 'vertical' #Arrange four child objects vertically
BoxLayout:
size_hint_y: 0.2 #Vertical width 10%Occupy
Button: #Button to change the theme
size_hint_x: 0.3 #Width 30%Occupy
text: 'Peso de Rollos'
background_color: 0.9,0.1,0.9, 1
Button: #Show theme
size_hint_x: 0.3 #Width 70%Occupy
text: 'Peso de Bultos'
background_color: 0.1,0.1,0.1, 1
Button: #Show theme
size_hint_x: 0.3 #Width 70%Occupy
text: 'Peso de Canutos'
background_color: 0.1,0.1,0.1, 1
BoxLayout: #labels
size_hint_y: 0.1
canvas.before:
Color:
rgba: 4, .4, 0, 1
Rectangle:
pos: self.pos
size: self.size
Label:
text: '#1'
Label:
text: '#2'
Label:
text: '#3'
Label:
text: '#4'
Label:
text: '#5'
Label:
text: '#6'
Label:
text: '#7'
Label:
text: '#8'
Label:
text: '#9'
Label:
text: '#10'
Label:
text: '#11'
Label:
text: '#12'
BoxLayout:
size_hint_y: 0.2
id:boxcargan
Image:
id: carga0
Image:
id: carga1
source: ''
Image:
id: carga2
source: ''
Image:
id: carga3
source: ''
Image:
id: carga4
source: ''
Image:
id: carga5
source: ''
Image:
id: carga6
source: ''
Image:
id: carga7
source: ''
Image:
id: carga8
source: ''
Image:
id: carga9
source: ''
Image:
id: carga10
source: ''
Image:
id: carga11
source: ''
BoxLayout:
Label:
id: lblcorrecto
text:''
size_hint_y: 0.7
color: 237 , 112 , 64 , 1
font_size: 20
BoxLayout:
Label: #Canvas part (provisional)
text: 'POR FAVOR, COLOQUE EL ROLLO A PESAR'
size_hint_y: 0.9
color: 1 , 0 , 0 , 1
size_hint_x: 0.9
BoxLayout:
size_hint_y: 0.1 #Vertical width 10%Occupy
Button: #Undo button
text: 'PESAR'
on_press: root.extraer_peso()
Button: #All erase button
text: 'FINALIZAR'
您不能那样访问 ids
,您应该更改 Image
的 source
属性 而不是使用 StringProperty
。尝试更改:
self.ids.carga[numero] = StringProperty('peso.png')
至:
self.ids["carga" + str(numero)].source = 'peso.png'
但是 ids
在 __init__()
方法中将不可用(它们尚未设置)。所以你只需要稍微延迟使用它们,你可以用 Clock.schedule_once()
来做到这一点。这是您的 Rollo
class 的修改版本:
class Rollo(Screen):
def __init__(self, **kwargs):
super(Rollo, self).__init__(**kwargs)
print('inicie')
Clock.schedule_once(self.setup)
def setup(self, dt):
# connect DB
con = psycopg2.connect(
host="localhost",
database="appsenior",
user="postgres",
password="qq")
cur = con.cursor()
# contar cantidad de rollos pesados
consulta = "SELECT count(*) pesos FROM peso_materia WHERE id_parada=100"
cur.execute(consulta)
rows = cur.fetchone()
numero = 0
# example rows[0] = 4 loop con 4
while numero < rows[0]:
# It should show me the image peso.png in the corresponding image id
self.ids['carga' + str(numero)].source = 'peso.png'
numero = numero + 1
我在 python kivy 中有以下脚本,它计算数据库中 table 中有多少条记录,并根据总数显示图像 n[] 次。
如果总记录是 4 并且我执行 print[numero]
它打印 0,1,2,3,没关系。
但是当我想显示图像“weight.png”4 次时,它没有显示并给我一个错误:AttributeError: 'super' object has no attribute '__getattr__'
此处代码:
rollo.py
from email.mime import image
from itertools import count
from logging import root
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.config import Config
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen
import time
import serial
import psycopg2
from datetime import datetime, timezone
from kivy.uix.image import Image
from kivy.properties import StringProperty
from inicio import Inicio
class Rollo(Screen):
def __init__(self, **kwargs):
super(Rollo, self).__init__(**kwargs)
print('inicie')
#connect DB
con = psycopg2.connect(
host = "localhost",
database = "appsenior",
user = "postgres",
password = "qq")
cur = con.cursor()
#contar cantidad de rollos pesados
consulta = "SELECT count(*) pesos FROM peso_materia WHERE id_parada=100"
cur.execute(consulta)
rows = cur.fetchone()
numero = 0
#example rows[0] = 4 loop con 4
while numero < rows[0]:
#It should show me the image peso.png in the corresponding image id
self.ids.carga[numero] = StringProperty('peso.png')
numero = numero + 1
kivy.require('2.0.0')
Config.set('graphics', 'resizable', True)
Builder.load_file('rollo.kv')
class Root(GridLayout):
pass
class Main(App):
def build(self):
return Root()
if __name__ == '__main__':
Main().run()
rollo.kv
<Rollo@Screen>
name:'rollo'
canvas:
Color:
rgba: .9,.9,.9,1
Rectangle:
size: self.size
BoxLayout:
orientation: 'vertical' #Arrange four child objects vertically
BoxLayout:
size_hint_y: 0.2 #Vertical width 10%Occupy
Button: #Button to change the theme
size_hint_x: 0.3 #Width 30%Occupy
text: 'Peso de Rollos'
background_color: 0.9,0.1,0.9, 1
Button: #Show theme
size_hint_x: 0.3 #Width 70%Occupy
text: 'Peso de Bultos'
background_color: 0.1,0.1,0.1, 1
Button: #Show theme
size_hint_x: 0.3 #Width 70%Occupy
text: 'Peso de Canutos'
background_color: 0.1,0.1,0.1, 1
BoxLayout: #labels
size_hint_y: 0.1
canvas.before:
Color:
rgba: 4, .4, 0, 1
Rectangle:
pos: self.pos
size: self.size
Label:
text: '#1'
Label:
text: '#2'
Label:
text: '#3'
Label:
text: '#4'
Label:
text: '#5'
Label:
text: '#6'
Label:
text: '#7'
Label:
text: '#8'
Label:
text: '#9'
Label:
text: '#10'
Label:
text: '#11'
Label:
text: '#12'
BoxLayout:
size_hint_y: 0.2
id:boxcargan
Image:
id: carga0
Image:
id: carga1
source: ''
Image:
id: carga2
source: ''
Image:
id: carga3
source: ''
Image:
id: carga4
source: ''
Image:
id: carga5
source: ''
Image:
id: carga6
source: ''
Image:
id: carga7
source: ''
Image:
id: carga8
source: ''
Image:
id: carga9
source: ''
Image:
id: carga10
source: ''
Image:
id: carga11
source: ''
BoxLayout:
Label:
id: lblcorrecto
text:''
size_hint_y: 0.7
color: 237 , 112 , 64 , 1
font_size: 20
BoxLayout:
Label: #Canvas part (provisional)
text: 'POR FAVOR, COLOQUE EL ROLLO A PESAR'
size_hint_y: 0.9
color: 1 , 0 , 0 , 1
size_hint_x: 0.9
BoxLayout:
size_hint_y: 0.1 #Vertical width 10%Occupy
Button: #Undo button
text: 'PESAR'
on_press: root.extraer_peso()
Button: #All erase button
text: 'FINALIZAR'
您不能那样访问 ids
,您应该更改 Image
的 source
属性 而不是使用 StringProperty
。尝试更改:
self.ids.carga[numero] = StringProperty('peso.png')
至:
self.ids["carga" + str(numero)].source = 'peso.png'
但是 ids
在 __init__()
方法中将不可用(它们尚未设置)。所以你只需要稍微延迟使用它们,你可以用 Clock.schedule_once()
来做到这一点。这是您的 Rollo
class 的修改版本:
class Rollo(Screen):
def __init__(self, **kwargs):
super(Rollo, self).__init__(**kwargs)
print('inicie')
Clock.schedule_once(self.setup)
def setup(self, dt):
# connect DB
con = psycopg2.connect(
host="localhost",
database="appsenior",
user="postgres",
password="qq")
cur = con.cursor()
# contar cantidad de rollos pesados
consulta = "SELECT count(*) pesos FROM peso_materia WHERE id_parada=100"
cur.execute(consulta)
rows = cur.fetchone()
numero = 0
# example rows[0] = 4 loop con 4
while numero < rows[0]:
# It should show me the image peso.png in the corresponding image id
self.ids['carga' + str(numero)].source = 'peso.png'
numero = numero + 1