I'm having a very hard time trying to understand Kivi's ScrollView, is there anyone who can help me? The thing is that I have a page with a label, multiple switches and a button to confirm the choices of those switches, how do I make a working ScrollView for this?
check this tuto: https://www.youtube.com/watch?v=lPaeoSv0rjo&t=23s
but instead of a Label in the ScrollView use a layout like GridLayout or something and remember that the scrollView accepts one widget as a child so u have to add a layout scrollview and layout it self can accept many widgets
ScrollView:
size: # whatever u need
pos: # whatever u need
GridLayout:
size_hint_y: None ## this is mandatory
id: my_scroll
height: self.minimum_height
cols: 1
and in ur main.py u can just:
self.ur_widget = Button(text='btn', size_hint_y=None, size_hint_x= 1.0)
self.ids.my_scroll.add_widget(self.ur_widget)
u can play with this and play with layouts to find whats best suited for u:
Related
I have a ScrollView inside of a BoxLayout that changes size rather dramatically when the screen size changes. I am making an iOS app that displays text inside of a ScrollView in the top half of the screen, I then have a non scroll Label directly under that.
I have been using an iPhone 12/13 for the layout design however when I run it on an iPhone 8 (with the smaller screen real estate), the ScrollView overlaps the Label text under it considerably. When I run the two different Xcode phone simulators side by side, the ScrollView area on the iPhone 8 actually seems to increase in size for some weird reason.
I have been using size_hint and pos_hint for all the widgets, which has generally been working okay, just not really with this ScrollView. I have tried adjusting all of the sizes of everything but there doesn't seem like a happy middle that I can use to suit both screen types.
I have been using height in my BoxLayout as the parent for the ScrollView, and not sure if I'm using it right either.
I will include my BoxLayout with the ScrollView in it. Please could someone have a look to see if I'm doing something obviously wrong here? I hope I don't need to make different layouts for every different screen type.
BoxLayout:
pos_hint: {"top": .9, "center_x": .5}
size_hint: 1, .3
size_hint_y: None
height: 460
ScrollView:
Label:
id: brief_res_1
markup: True
font_size: '13sp'
do_scroll_x: True
size_hint_y: None
size_hint_x: None
size: self.texture_size
halign: "left"
valign: "center"
text: ""
I'm trying to create kivy MDSwiper with vertical ScrollView in it. Here is my code.
<MySwiperItem#MDSwiperItem>
do_scroll: True
width_mult: 1
ScrollView:
MDBoxLayout:
orientation: 'vertical'
adaptive_height: True
<MySwiper#MDSwiper>
items_spacing: 0
do_scroll: True
MySwiperItem:
MySwiperItem:
MySwiperItem:
And it nicely scrolls vertically only. And the horizontal scroll from mdswiper disappeared.
I have also tried to create GridLayout instead of BoxLayout and even change the source of kivy swiper class, but the effect is still awful. Help please
I'm making a game in kivy and when I close the screen (android) or try to resize the window (linux) some widgets that I've moved away from the screen return to their starting position.
I created a minimal reproducible example for that:
example.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class GameCanvas(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def start(self):
# move the label widget far away
self.children[0].pos = 10000, 10000
class Example(App):
pass
if __name__ == '__main__':
Example().run()
example.kv
GameCanvas:
<GameCanvas>:
Button:
text: 'Start Game'
size: root.width, root.height / 2
on_release: root.start()
Label:
text: 'Game Title'
size: root.width, root.height / 2
Before pressing the button:
After pressing the button:
After resizing:
As the attached images show after I press the button the label 'disappears' (moves position) but when I try to resize the Window it comes back.
Why does this happen? Also why does it happen on android as well? Does it have to do with some sort of events in kivy?
Thanks in advance!
The behavior you see is the intended behavior of the BoxLayout. The BoxLayout positions its children. So, you can move the Label, but as soon as the BoxLayout gets a chance (as in a size change event), it positions its children as intended. If you want to control the position of the children widgets, then you should use a Layout that does not position its children, perhaps a FloatLayout or RelativeLayout
I was wondering if there is a way to get a static background in a Kivy application with a screen manager. With static, I mean that the background remains as it is, even when switching screens. I am using a .kv file for the layout. I'd guess it has something to do with the placement order within a .kv file.
Thanks!
You can use a float layout as the root widget in every screen and add to that float layout the image and the other layout in your screen, here is a code example in kv:
Screen: # Screen 1
id: Home
FloatLayout:
Image:
source: "path to the image"
BoxLayout: # Here you put your other layout
# And here the code you had
Screen: # Screen 2
id: Another Screen
FloatLayout:
Image:
source: "path to the image"
BoxLayout: # Here you put your other layout
# And here the code you had
This is the solution that I know, it might not be perfect for you, but I will leave the other options to others...
FloatLayout:
Image:
ScreenManager:
would be enough I think but be careful, if the transition of ScreenManager is ShaderTransition(or a sub-class thereof), it doesn't respect the pixels in the background, so the animation during the transition may not work properly.
I am learning the basics of Kivy and going through tutorials. I noticed that when I start a Kivy app, the opacity of the labels are not consistent. Sometimes when I start the app, some labels are full opacity while others are half opacity.
Sometimes I start the app and some labels are entirely opaque and missing.
I can't figure out why this is happening. All of the labels have the same definition and I believe should not be behaving this way.
I have tried just closing and starting the app over and over to see if there is a pattern and it seems that the first label in the top left is always consistent while the other 3 labels on the 3 other buttons are not.
I have also tried out some of the demo apps in kivy and the demo apps are showing this behavior as well.
Here are the files that I am using:
test.py
import kivy
kivy.require('1.10.1')
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
class GridLayoutApp(App):
def build(self):
return GridLayout()
if __name__ == '__main__':
glApp = GridLayoutApp()
glApp.run()
gridlayout.kv
<GridLayout>:
cols: 2
rows: 2
spacing: 10
padding: 10
Button:
text: "1st"
size_hint_x: None
width: 200
Button:
text: "2nd"
Button:
text: "3rd"
size_hint_x: None
width: 200
Button:
text: "4th"
I expected all of the buttons to have the same opacity. Sometimes the program does get this right but most of the time, the opacity is off for some reason.
I have no idea how to even approach this problem so any suggestions are very much appreciated!
It's a bug that appeared during an sdl2 version update. It's fixed in Kivy 1.11, released a couple of days ago, make sure your Kivy is up to date.