我正在与Kivy进行游戏。我想将按钮尺寸设置为图像尺寸的一半。
这是我的.kv
<HomeScreen>:
Image:
source: "data/background.png"
Button:
background_color: 0, 0, 0, 0
width: root.width / 1.5
height: root.height / 7.5
pos: (root.width / 2) - (self.width / 2), root.height - root.height * 0.4
on_press: app.change("PlayScreen") # change screen function
Image:
pos: self.parent.x, self.parent.y
width: self.parent.width
height: self.parent.height
source: "data/button.png"
Label:
pos: self.parent.center_x - (self.width / 2), (self.parent.height / 2) - (self.height / 2.25) + self.parent.y
color: 0, 0, 1, 1
font_size: self.parent.height / 3
text: "Play"
这是我的Python:
class handler(App):
def __init__(self, **kwargs):
Builder.load_file("data/layouts.kv")
self.sm = getSM()
self.places = {0: "HomeScreen"}
super().__init__(**kwargs)
def build(self):
Window.size = (1000, 800)
Window.minimum_width, Window.minimum_height = Window.size
self.title = "Digital Drummer"
return self.sm
def change(self, to):
print(self.root.children[0].children[0].size)
if to == "Back":
print("Changing from " + self.sm.current + " to " + self.places[len(self.places) - 2])
self.root.transition = FadeTransition(duration=0.5)
self.sm.current = self.places[len(self.places) - 2]
del self.places[len(self.places) - 1]
else:
print("Changing from " + self.sm.current + " to " + to)
self.root.transition = FadeTransition(duration=0.5)
self.sm.current = to
self.places[len(self.places)] = self.sm.current
当我垂直调整其大小时,按钮图像会变大并随着背景图像居中而向下移动而向上移动。
我尝试过使用功能-
def getFrame(self):
print(self.children[0].width, self.children[0].height)
return self.children[0]
并运行root.getFrame()
,但是大小只是窗口大小,即使显示的图像不是。
如何获取背景图像的大小和位置?