如何将kivy文件中的Spinner ID与我从产品数据库中提供的数据连接起来



考虑到我想将self.listcode变量中的字符串加载到微调器的值中,我如何将我的id连接到python代码,我已经使用了self。ids。我也没有从KV文件中得到任何信息。提前感谢您的帮助。弹出的错误之一如下:

AttributeError: 'super' object has no attribute '__getattr__

我想如果我没有错的话,这与Screen没有访问ids 的可能性有关

KIVY文件###

<WindowManager>:
FirstWindow:


SecondWindow:

<FirstWindow>:
name: "menu"

canvas.before:    
Color:
rgba: (242/255, 202/255, 5/255, 1)        
RoundedRectangle:
size: self.size
pos: self.pos
radius: [55]


Image: 
source: 'Logoapp.png'

BoxLayout:
orientation: "horizontal"
size: 50, 50    
Button:
font_size: 20
size_hint: 0.3, 0.2
pos_hint: {"x":.5, "top":.2}
pos:100, 100
background_color: 0,0,0,0
text: "Cotizar"
color: 0,0,0,1
canvas.before:
Color:
rgba: (23/255,152/255,216/255,0.71)
RoundedRectangle:
size: self.size
pos: self.pos
radius:[55]
on_release: 
app.root.current = "cotiza"
root.manager.transition.direction = "left"

<SecondWindow>:
name: "cotiza"
FloatLayout:
CodigoSpin:
id: "Cod_prod"
pos: 20, 100
Label:
text: "Descripcion"
pos: 20, 35
CodigoSpin:
id: "Cod_prod2"
pos: 20, 200
Label:
text: "Descripcion"
pos: 20, 130

CodigoSpin:
id: "Cod_prod3"
pos: 20, 300  
Label:
text: "Descripcion"
pos: 20, 235
<CodigoSpin@Spinner>:

size_hint: None, None
size: 100, 44
text: "CodigoSpin" 

主程序

from types import new_class
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.config import Config
from kivy.core.window import Window
from kivy.properties import ListProperty
from kivy.uix.spinner import Spinner, SpinnerOption
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import Color, Rectangle, RoundedRectangle
import sqlite3
Window.size = (250, 500 ) #Changed the size of the screen to fit my phone
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen
class FirstWindow(Screen, FloatLayout):
pass

class SecondWindow(Screen, Widget):

def __init__(self, **kwargs):
super(SecondWindow, self).__init__(**kwargs)
#Create de Database connection
conn = sqlite3.connect("inventingt.db")
cursor = conn.cursor()
cursor.execute("""SELECT Codigo FROM masterinv """)
rows = cursor.fetchall()
#Create the variable listcode of objects from the database
self.listcode = [str(t[0]) for t in rows] 
#This method will initialize the values on the Spinner 
self.inicializarvalores()


def inicializarvalores(self):
self.ids.Cod_prod.values = self.listcode



class WindowManager(ScreenManager, Widget):

pass

class Buendes(App):
def build(self):
#Inicializamos el screen manager para inicializar las paginas

self.window = WindowManager()
#add widgets to window




#Agregamos el boton de inicializar la aplicacion
#Conectamos una base de datos



return self.window

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

将在.kv文件中创建的小部件连接到.py文件的其他方法是:

  • id赋予小部件。(✓你是以Cod_prod的身份完成的(
  • 在.kv文件中,类的顶部(在<SecondWindow>:下(,键入以下内容:Cod_prod:Cod_prod(☓ )
  • 导入此:from kivy.properties import ObjectProperty(☓ )
  • 现在,在class SecondWindow(在.py文件中(下,识别您的对象:Cod_prod = ObjectProperty()

现在您可以使用访问在.kv文件中创建的小部件

def inicializarvalores(self):
self.Cod_prod.values = self.listcode

我无法理解Spinner和Screen连接的情况。我也有同样的问题。也尝试了两种解决方案,但都无法获得解决方案。我无法在继承Screen的根类上为Spinner赋值。

我尝试过的:

  1. 在py文件上:self.ids.spinnerid.values=list->不起作用。但是py连接了旋转器
  2. 在kv文件上:Spinner:values:root.list->不起作用
  3. 尝试ListProperty、ObjectProperty->不起作用

我在kv文件上使用值:app.list来解决问题,并在app类上定义列表。

相关内容

  • 没有找到相关文章

最新更新