1. <small id='x9Raq'></small><noframes id='x9Raq'>

      <tfoot id='x9Raq'></tfoot>
        <bdo id='x9Raq'></bdo><ul id='x9Raq'></ul>
    2. <i id='x9Raq'><tr id='x9Raq'><dt id='x9Raq'><q id='x9Raq'><span id='x9Raq'><b id='x9Raq'><form id='x9Raq'><ins id='x9Raq'></ins><ul id='x9Raq'></ul><sub id='x9Raq'></sub></form><legend id='x9Raq'></legend><bdo id='x9Raq'><pre id='x9Raq'><center id='x9Raq'></center></pre></bdo></b><th id='x9Raq'></th></span></q></dt></tr></i><div id='x9Raq'><tfoot id='x9Raq'></tfoot><dl id='x9Raq'><fieldset id='x9Raq'></fieldset></dl></div>

      <legend id='x9Raq'><style id='x9Raq'><dir id='x9Raq'><q id='x9Raq'></q></dir></style></legend>
    3. 如何将纯 python 中动态创建的按钮添加到用 Kivy 语言编写的 kivy 布局中?

      时间:2023-06-07

    4. <tfoot id='jpPx1'></tfoot>
          <bdo id='jpPx1'></bdo><ul id='jpPx1'></ul>
            <tbody id='jpPx1'></tbody>
          <i id='jpPx1'><tr id='jpPx1'><dt id='jpPx1'><q id='jpPx1'><span id='jpPx1'><b id='jpPx1'><form id='jpPx1'><ins id='jpPx1'></ins><ul id='jpPx1'></ul><sub id='jpPx1'></sub></form><legend id='jpPx1'></legend><bdo id='jpPx1'><pre id='jpPx1'><center id='jpPx1'></center></pre></bdo></b><th id='jpPx1'></th></span></q></dt></tr></i><div id='jpPx1'><tfoot id='jpPx1'></tfoot><dl id='jpPx1'><fieldset id='jpPx1'></fieldset></dl></div>

          1. <legend id='jpPx1'><style id='jpPx1'><dir id='jpPx1'><q id='jpPx1'></q></dir></style></legend>

              <small id='jpPx1'></small><noframes id='jpPx1'>

              • 本文介绍了如何将纯 python 中动态创建的按钮添加到用 Kivy 语言编写的 kivy 布局中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我的问题是我需要根据可变数量的网格方块创建一个按钮网格,并将它们放置在网格布局上并使用屏幕管理器将它们显示在屏幕上.我知道如何使用简单的 for 循环在纯 python 中执行此操作,但是我用 kivy 语言为我的程序编写了布局,我不知道如何将按钮添加到网格布局中,因为我不知道如何在 kv 文件中正确引用它们.相关的python代码是:

                My problem is that I need to create a grid of buttons based on a variable number of grid squares, and place them on a grid layout and display them on a screen using the screen manager. I know how to do this in pure python using a simple for loop, but I wrote the layout for my program in kivy language, and I don't know how to add the buttons to the grid layout, because I don't know how to correctly reference them in the kv file. The relevant python code is:

                def buildMap():
                    index = 0
                    for index in range(0, numberOfGridBlocks):
                        mainMap.ids["Map"].add_widget(Button())
                        index = index + 1
                buildMap() 
                

                kv文件的相关部分是:

                The relevant part of the kv file is:

                ScreenManagement:
                    MainMenuScreen:
                    NewGameMenuScreen:
                    JoinGameMenuScreen:
                    TutorialMenuScreen:
                    SettingsMenuScreen:
                    MapScreen:
                
                <MenuButton>:
                    on_press: app.menuButtonPressed()
                    size_hint_y: .125
                    background_normal: "images/button.png"
                    background_down: "images/buttonPressed.png"
                
                <Button>:
                
                <BoxLayout>:
                    orientation: "vertical"
                <MapLayout>:
                
                <MapScreen>:
                    name: "mapScreen"
                    MapLayout:
                        id: "Map"
                        cols: 5
                

                推荐答案

                希望这个例子能让你明白:

                I hope this example makes it clear for you:

                test.kv:

                #:kivy 1.9.0
                ScreenManager:
                    MapScreen:
                
                <MapScreen>:
                    name: 'map'
                
                    GridLayout:
                        id: grid
                        cols: 1
                

                main.py:

                #!/usr/bin/env python
                # -*- coding: utf-8 -*-
                from kivy.app import App
                from kivy.uix.screenmanager import Screen
                from kivy.uix.button import Button
                from kivy.clock import mainthread
                
                NUMBER_OF_BUTTONS = 5
                
                
                class MapScreen(Screen):
                
                    @mainthread
                    def on_enter(self):
                        for i in xrange(NUMBER_OF_BUTTONS):
                            button = Button(text="B_" + str(i))
                            self.ids.grid.add_widget(button)
                
                
                class Test(App):
                    pass
                
                
                Test().run()
                

                @mainthead 装饰器需要稍微延迟函数,因此首先扫描 kv 文件,使 ids 列表可行.

                The @mainthead decorator is needed to slightly delay the function, so the kv file gets scanned first, making the ids list viable.

                这篇关于如何将纯 python 中动态创建的按钮添加到用 Kivy 语言编写的 kivy 布局中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:基维外观和感觉 下一篇:Python - 在应用程序中显示 Web 浏览器/iframe

                相关文章

                  <small id='6rEBH'></small><noframes id='6rEBH'>

                1. <tfoot id='6rEBH'></tfoot>
                    <bdo id='6rEBH'></bdo><ul id='6rEBH'></ul>
                    <legend id='6rEBH'><style id='6rEBH'><dir id='6rEBH'><q id='6rEBH'></q></dir></style></legend>
                    <i id='6rEBH'><tr id='6rEBH'><dt id='6rEBH'><q id='6rEBH'><span id='6rEBH'><b id='6rEBH'><form id='6rEBH'><ins id='6rEBH'></ins><ul id='6rEBH'></ul><sub id='6rEBH'></sub></form><legend id='6rEBH'></legend><bdo id='6rEBH'><pre id='6rEBH'><center id='6rEBH'></center></pre></bdo></b><th id='6rEBH'></th></span></q></dt></tr></i><div id='6rEBH'><tfoot id='6rEBH'></tfoot><dl id='6rEBH'><fieldset id='6rEBH'></fieldset></dl></div>