我试图通过在3x3 GridLayout中排列9个按钮来使键盘变得有点怪异。但是,运行代码后,它仅在右下角显示1个按钮。
我的代码:
from kivy.uix.button import Button
from kivy.uix.boxlayout import GridLayout
from kivy.app import App
class root(GridLayout):
def __init__(self, **kwargs):
super(GridLayout, self).__init__(**kwargs)
self.cols = 2
self.rows = 2
self.button1 = Button(text = '1')
self.button2 = Button(text = '2')
self.button3 = Button(text = '3')
self.button4 = Button(text = '4')
self.button5 = Button(text = '5')
self.button6 = Button(text = '6')
...........
self.add_widget(self.button1)
self.add_widget(self.button2)
self.add_widget(self.button3)
self.add_widget(self.button4)
...........
class myApp(App):
def build(self):
l = root()
return l
myApp().run()
有帮助吗?
答案 0 :(得分:1)
您遇到以下错误:
10 + "2" + 0
,您必须使用"1020"
GridLayout
时,必须将类的名称即from kivy.uix.gridlayout import GridLayout
作为第一个参数传递。super()
,super(root, self).__init__(**kwargs)
。代码:
self.cols = 3
一个更简单,更优雅的解决方案是使用for循环:
self.rows = 3