Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

python kivy isuues with pos_hint for image

I am trying to get a image to position itself at the bottom right hand corner of the screen with pos_hint but it always leaves a gap between the bottom of the image and the screen, im still very new to kivy and have looked around for a solution but cant find anything that helps, thanks in advance

class Building(Image):
def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.source = 'Building.png'
    self.size_hint = 0.4,1
    self.pos_hint = {'x':0.7, 'y':0.1}

This is where i call it

self.build = Building()
self.add_widget(self.build)

This is a link to the how it looks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are two issues that may be causing the gap. One is that your code:

self.pos_hint = {'x':0.7, 'y':0.1}

is setting a gap of one tenth of the container height.

The second issue is that Image, by default, will not stretch the underlying image, and will not distort the image (stretching more in one direction than the other). So, you can get what you want by modifying your Building class like this:

class Building(Image):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.source = 'Building.png'
        self.size_hint = 0.4,1
        self.pos_hint = {'right':1.0, 'y':0}   # position image at right, bottom
        self.allow_stretch = True  # allow image to be stretched
        self.keep_ratio = False  # allow distortion

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...