<tfoot id='zsg15'></tfoot>

    <bdo id='zsg15'></bdo><ul id='zsg15'></ul>

    <legend id='zsg15'><style id='zsg15'><dir id='zsg15'><q id='zsg15'></q></dir></style></legend>

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

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

    1. 如何将纯 python 中动态创建的按钮添加到用 Kivy

      时间:2023-10-10
      <i id='Wsklm'><tr id='Wsklm'><dt id='Wsklm'><q id='Wsklm'><span id='Wsklm'><b id='Wsklm'><form id='Wsklm'><ins id='Wsklm'></ins><ul id='Wsklm'></ul><sub id='Wsklm'></sub></form><legend id='Wsklm'></legend><bdo id='Wsklm'><pre id='Wsklm'><center id='Wsklm'></center></pre></bdo></b><th id='Wsklm'></th></span></q></dt></tr></i><div id='Wsklm'><tfoot id='Wsklm'></tfoot><dl id='Wsklm'><fieldset id='Wsklm'></fieldset></dl></div>

          <tfoot id='Wsklm'></tfoot>
              <bdo id='Wsklm'></bdo><ul id='Wsklm'></ul>

            • <small id='Wsklm'></small><noframes id='Wsklm'>

                <tbody id='Wsklm'></tbody>

                <legend id='Wsklm'><style id='Wsklm'><dir id='Wsklm'><q id='Wsklm'></q></dir></style></legend>
                本文介绍了如何将纯 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 布局中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                相关文章

                最新文章

                <legend id='XKsTx'><style id='XKsTx'><dir id='XKsTx'><q id='XKsTx'></q></dir></style></legend>
              • <small id='XKsTx'></small><noframes id='XKsTx'>

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

                    <tfoot id='XKsTx'></tfoot>
                      <bdo id='XKsTx'></bdo><ul id='XKsTx'></ul>