Kivy GridLayout Error : have no cols or rows set, layout is not triggered

≡放荡痞女 提交于 2021-02-11 16:50:40

问题


I'm trying to make a simple app that takes name, grade, language (just for practice).

Here's the Code:

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout  
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button


class Mylays(GridLayout):
    def __init__(self,**kwargs):
        super(Mylays,self).__init__(**kwargs)
        self.top_grid = GridLayout() # Widget above main widget to hold all text and input boxes.
        self.cols = 2 # no.of columns 
        
        self.top_grid.add_widget(Label(text="Name : ",font_size=20))  # add a widget with "Name:" in it.
        self.name = TextInput(multiline=False)      # make a input box with multiline False
        self.top_grid.add_widget(self.name)                  # place the input box to widget

        self.top_grid.add_widget(Label(text="class :"))
        self.hisclass = TextInput(multiline=False)     
        self.top_grid.add_widget(self.hisclass)  

        self.top_grid.add_widget(Label(text="Lang:"))
        self.lang = TextInput(multiline=False)
        self.top_grid.add_widget(self.lang)

        self.add_widget(self.top_grid)

        
        self.click = Button(text="Boom!",font_size=25)
        self.click.bind(on_press=self.buttonfunction)
        self.add_widget(self.click)
    
        

    def buttonfunction(self, instance):
        name = self.name.text
        CLASs = self.hisclass.text
        langu = self.lang.text
        x = "Hi {0},Ik you are from {1}. I also likes {2} Language .".format(name,CLASs,langu)
        self.add_widget(Label(text=x))
        self.name.text = ""
        self.hisclass.text = ""
        self.lang.text = ""

class firstapp(App):
    def build(self):
        return Mylays()
    
if __name__ == "__main__":
    firstapp().run() 

Although it runs I get the layout all wrong and just the button on the screen with following error:

[WARNING] <kivy.uix.gridlayout.GridLayout object at 0x04254108> have no cols or rows set, layout is not triggered.

Output Window image


回答1:


You are getting that error because you are not setting cols or rows for a GridLayout. In this case, the GridLayout in question is the one created by:

self.top_grid = GridLayout()

The fix is to set cols or rows for that GridLayout. Try adding a line like:

self.top_grid.cols = 2

just after creating self.top_grid.



来源:https://stackoverflow.com/questions/65505973/kivy-gridlayout-error-have-no-cols-or-rows-set-layout-is-not-triggered

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!