我正在使用Win7,Eclipse,python 2.7 + kivy框架来开发一个openGL应用程序
我试图通过鼠标点击检查Widget的一些碰撞点(x,y)。我创建了一个具有特定大小的布局,但是当进入on_touch_down(鼠标单击)回调时,布局大小奇怪地改变了(这是问题)。让我告诉你我的代码:
class PlayScreen(Screen):
layout = None
def __init__(self):
self.layout = StackLayout(size=(Types.SCREEN_SIZE_WIDTH, 100))
#then I create widgets inside the layout:
self.lblScore = Label(text='SCORE:', size_hint=(.1, .1))
self.lblScoreValue = Label(text='0', size_hint=(.1, .1))
.....
self.layout.add_widget(self.lblScore)
self.layout.add_widget(self.lblScoreValue)
#here the debugger shows me self.layout size to be(800, 100)
#and then I want to test if I click on the self.layout:
def on_touch_down(self, touch):
bCanAddTower = True
if self.layout.collide_point(touch.x, touch.y) == True:
print "colision with menu"
#here self.layout has size=(800, 600)(here is the problem) the entire window size, and of course I get into collision all the time.
有没有人知道为什么on.touch_down方法中self.layout的大小会发生变化?
答案 0 :(得分:1)
初始大小是因为您使用该大小实例化窗口小部件,因此您之后立即运行的所有代码都会看到此值。但是,它的父窗口小部件是一个Screen,它是一个Layout类(特别是RelativeLayout),它会自动调整其子项的大小以填充自身,除非您设置了一些其他选项。此自动调整大小仅发生在__init__
之后(但在下一帧之前),这就是on_touch_down或任何其他方法将看到新大小的原因。
在这种情况下,您可以将size_hint=(None, None)
添加到self.layout实例中。这只是告诉父屏幕不要手动控制它的大小,所以它将保持你设置它。
从长远来看,您可能希望使用某种比例尺寸设置而不是固定值,因为完全固定的尺寸在不同的屏幕尺寸,像素密度等方面不会以相同的方式显示。