I am trying to create a screen in kivy. I want the screen to have a small main menu button on the top left corner, a big image in the center of the screen with about 80 percent of the screen size and than below everything I want a button, who's size is dependent of the size of the image.
In example, The main_menu button should be at the top left corner, and 5 percent of the screen's height and his width should be as small as his text allow. The image size should be 80 percent of the screen, The button below should have a height of 5 percent and width who is dependent on the image size.
My current code is this, but the when I run this code, the text on the button below shows the size of the window (1920, 1080) and (1, 1).
What I expect from it to show is the size of the image
#:kivy 0.0.0
<EditingScreen>:
GridLayout:
rows: 3
spacing: 10
AnchorLayout:
size_hint: None, 0.05
anchor_x: 'left'
anchor_y: 'top'
Button:
text: 'Main Menu'
on_press: root.manager.current = 'main'
Image:
id: some_image
source: 'img.png'
allow_stretch: True
keep_ratio: True
Button:
text: str(some_image.size) + ' ' + str(some_image.size_hint)
size_hint: None, 0.05
width: some_image.width
EDIT: Adding my python code: 1) screen_manager.py:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager
from editing_screen import EditingScreen
class Manager(ScreenManager):
def __init__(self, **kwargs):
super(Manager, self).__init__(**kwargs)
self.add_widget(MainScreen(name='main'))
self.add_widget(InstructionsScreen(name='instructions'))
self.add_widget(EditingScreen(name='editing'))
self.current = 'editing'
class ManagerApp(App):
def build(self):
return Manager()
if __name__ == '__main__':
ManagerApp().run()
2) editing_screen.py:
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
Builder.load_file('editing_screen.kv')
class EditingScreen(Screen):
pass
3) The first given code in a file named editing_screen.kv
In your editing_screen.kv
, the some_image.size
gives you the size of the Image
widget, which is not necessarily the size of the image itself. The actual size of the image can be accessed as the norm_image_size property of the Image
widget.