Kivy GridLayout中的ID的空列表 [英] Empty list of ids in a Kivy GridLayout

查看:67
本文介绍了Kivy GridLayout中的ID的空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与Kivy开发一个玩具应用程序.

I am trying to develop a toy app with Kivy.

到目前为止,我有一个9 x 9 TextInput小部件的网格,这些小部件已通过编程方式添加到GridLayout(我在kv文件中使用ID"grid"来引用它).

So far I have a grid of 9 x 9 TextInput widgets that have been added programmatically to a GridLayout (I refer to it by the id "grid" in my kv file).

由于某些原因,尽管我为每个TextInput小部件分配了一个id,但是在the get_widget_from_id中获得的id字典为空.

For some reason, although I am assigning an id to each of the TextInput widgets, the ids dictionary that I get in the get_widget_from_id is empty.

我想念什么?

main.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.clock import Clock

class SudokuGame(Widget):
    # Initialize the grid of text inputs
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        grid = self.ids["grid"]
        for i in range(81):
            row = i // 9
            col = i  % 9
            grid.add_widget(TextInput(id = str(row) + "-" + str(col)))

    # Try to retrieve one of the widgets from its id
    def get_widget_from_id(self, *args):
        print(self.ids["grid"].ids)

class SudokuApp(App):
    def build(self):
        game = SudokuGame()
        Clock.schedule_once(game.get_widget_from_id)
        return game

if __name__ == '__main__':
    SudokuApp().run()

sudoku.kv

#:kivy 1.9.1

<TextInput@SudokuGame>:
    text: ""
    font_size: 0.7 * self.width
    padding: 0.3 * self.width, (self.height - self.line_height) / 2
    input_filter: lambda text, from_undo: text if ( 0 < int(text) < 10 and len(self.text) == 0 ) else ""
    multiline: False
    cursor_color: [0, 0, 0, 0]

<SudokuGame>:
    canvas.before:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size
    BoxLayout:
        orientation: "vertical"
        size: root.size
        GridLayout:
            id: grid
            rows: 9
            cols: 9
            line_size: 6
            canvas:
                Color:
                    rgb: 0, 0, 0
                Rectangle:
                    pos: self.x + self.width / 3 - self.line_size / 2, self.y
                    size: self.line_size, self.height
                Rectangle:
                    pos: self.x + 2 * self.width / 3 - self.line_size / 2, self.y
                    size: self.line_size, self.height
                Rectangle:
                    pos: self.x, self.y  + self.height / 3 - self.line_size / 2
                    size: self.width, self.line_size
                Rectangle:
                    pos: self.x, self.y  + 2 * self.height / 3 - self.line_size / 2
                    size: self.width, self.line_size
        BoxLayout:
            orientation: "horizontal"
            size_hint: 1, 0.1
            Button:
                text: "Solve"
            Button:
                text: "Clear"

推荐答案

仅从kv id中填充id dict.窗口小部件具有的Kivy属性实际上是无关的,并且我不确定它是否用于任何东西.

The ids dict is populated only from kv ids. The Kivy property that widgets have is actually unrelated, and I'm not sure if it's used for anything.

这篇关于Kivy GridLayout中的ID的空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆