繁体   English   中英

触摸时绘制小部件

[英]Drawing widget on touch down

我再次需要你的帮助,拜托。 我正在尝试创建一个简单的菜单 (topMenu),它将更新左侧(中心屏幕的一侧)的按钮网格。 您能否看看下面并提出建议,尽管在后台正确执行了代码,但为什么屏幕上没有绘制任何内容? (控制台日志:添加按钮:0 - 24)。

顶部菜单.py:

import Categories
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
import Left

class TopMenu(GridLayout):
    def __init__(self, **kwargs):
        super(TopMenu, self).__init__(**kwargs)
        print("Top is alive!")

        self.drawBtns()

    # Get buttons from the category and draw them on the top screen
    def drawBtns(self):
        mainCats = Categories.Categories()
        for i in range(len(mainCats.mainCats)):
            but = MenuButton(mainCats.mainCats[i])
            self.add_widget(but)

# Custom class to create a button that contains all required parameters
class MenuButton(ToggleButton):
    def __init__(self, btnText, **kwargs):
        super(MenuButton, self).__init__(**kwargs)

        self.text = btnText
        self.group = "topMenuGroup"
        self.bind(on_press = self.topMenuPressed)

    ############################### FIRE EVENT IN THIS METHOD ############################
    def topMenuPressed(event, self):
        print("Pressed " + self.text + ":" + self.state)
        if self.state == "down":
            left = Left.Left()
            left.addBtns()

左.py:

from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

class Left(GridLayout):
    def __init__(self, **kwargs):
        super(Left, self).__init__(**kwargs)
        print("Left is alive!")
    ####################### DRAW BUTTONS TO THE GRID LAYOUT AND DISPLAY ON THE SCREEN #########
    def addBtns(self):
        for i in range(0, 25):
            print("Adding button: " + str(i))
            but = Button(text="X" + str(i) + "\n23." + str(i))
            self.add_widget(but)

我认为我的问题可能与某些屏幕刷新有关? 但我不知道如何咬这个:)

@John Anderson,感谢您让我走上正轨(再次!)。 我做了一些小的研究,并提出了下面的想法。 它工作得很好,但我不确定这是否是解决这个问题的最好/最优雅的方法。 任何建议将不胜感激。 现在我可以继续我的项目了:-)

左.py

class Left(GridLayout):
    leftInstances = [] # Store all instances of this class
    def __init__(self, **kwargs):
        super(Left, self).__init__(**kwargs)
        self.__class__.leftInstances.append(self) # Add instance to instances list

    def drawBttns(self):
        for i in range(0, 25):
            but = Button(text="X" + str(i))
            self.add_widget(but)

和 TopMenu.py:

    def topMenuPressed(event, self):
        print("Pressed " + self.text + ":" + self.state)
        print(Left.Left.leftInstances)
        Left.Left.leftInstances[0].drawBttns()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM