Kivy GridLayout错误:未设置列或行,未触发布局 [英] Kivy GridLayout Error : have no cols or rows set, layout is not triggered

查看:79
本文介绍了Kivy GridLayout错误:未设置列或行,未触发布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单的应用,该应用需要名称,等级,语言(仅用于练习).

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

以下是代码:

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:

[警告]< kivy.uix.gridlayout.GridLayout对象位于0x04254108>没有设置列数或行数,不会触发布局.

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

推荐答案

由于没有为GridLayout设置colsrows,所以出现了该错误.在这种情况下,所讨论的GridLayout是由以下人员创建的:

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()

解决方法是为该GridLayout设置colsrows.尝试添加如下一行:

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

self.top_grid.cols = 2

就在创建self.top_grid之后.

这篇关于Kivy GridLayout错误:未设置列或行,未触发布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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