No effects on size_hint , python, kivy

Viewed 19

I try to change the size_hint of a button in Python, Kivy, but every value i put there the size of the button remain the same.. for the pos is changing but for the size_hint no, and if i change from pos to pos_hint the button is stuck in the corner of the window and from there i cant change nothing... also i tried to stick the pos of the button on a position where is a text in the photo but every time when i resize the kivy window the button and image are changing theyr position.. how i can solve this probelm ?? THANKSSS !!

from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.button import Button
from random import choice


class MyImage(Image):
    images = ["Q1.png", "Q2.png", "Q3.png"]  # ........

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        correct = Button(pos=(200, 200), size_hint=(.1, .1), on_release=self.random_image, background_color=(1, 1, 1, 0.2))
        self.add_widget(correct)
        self.random_image()

    def random_image(self, *_):
        self.source = choice(self.images)


class RandomQuestionApp(App):
    def build(self):
        return MyImage()


randomApp = RandomQuestionApp()
RandomQuestionApp().run()

1 Answers

Try size instead of size_hint.

correct = Button(
    pos=(200, 200),
    size=(100, 100), 
    on_release=self.random_image, 
    background_color=(1, 1, 1, 0.2)
)

size : This is for static sizing of widgets and takes two arguments i.e. (width, height). Default size of the button = (100, 100).

size_hint : This is for dynamic sizing of the button and provide hint of size. It contains two arguments i.e. width and height it can be floating values.By default, all widgets have their size_hint=(1, 1).

If there is nothing that influences the button size, your dynamics won't do anything. Size on the other hand would define a fixed size of the button where 100x100 is the default.

Related