I'm currently learning kivy and was following a tutorial, while following the tutorial I ran into a problem where uppon running my code only a black screen appears
this is what my .py file looks like:
from kivy.uix.widget import Widget
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.metrics import dp
from kivy.app import App
class stack_layout_example(StackLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
"""
# orientation without the kv
self.orientation = "lr-bt"
"""
for i in range(0, 100):
size = 100
button = Button(
text = str(i+1),
size_hint=(None, None),
# ( width , height )
size=(size, size)
)
self.add_widget(button)
class the_labApp(App):
pass
the_labApp().run()
and this is my .kv file:
scroll_view_example:
<scroll_view_example#ScrollView>:
stack_layout_example:
<stack_layout_example>:
You're missing the build method in the the_labApp app. It should be like the following code snippet:
class the_labApp(App):
def build(self):
return stack_layout_example()
Thank You very much for your answers guys... I already found the solution in the kivy discord
i just re-arrange the .kv file to
<ScrollViewExample#ScrollView>:
StackLayoutExample:
size_hint: 1, None
height: self.minimum_height
<StackLayoutExample>:
ScrollViewExample:
also the kv looks a bit different now because it now follows pep8(i think)
Related
Hello Guys i create a page using kivymd using an gif because i couldn't code it so i make a video for it than i convert it to gif.
the point is the code doesn't work and the error wasn't clear any help please
this the code of the splashloading.kv
MDFloatLayout:
md_bg_color:1,1,1,1
md_bg_color: (255/255, 250/255, 245/255, 1)
Image:
source:"assets/start.gif"
size_hint:.50,.50
pos_hint:{"center_x":.5,"center_y":.8}
and the code for the main.py I comment the first page just to see the splashloading page.
from kivy.uix.screenmanager import ScreenManager
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.clock import Clock
from kivy.core.text import LabelBase
Window.size =(350, 580)
class StartPage(MDApp):
def build(self):
LabelBase.register(name='Lemonada',fn_regular='fonts/Lemonada-VariableFont_wght.ttf')
global screen_manager
screen_manager = ScreenManager()
#screen_manager.add_widget(Builder.load_file("firstUse.kv"))
#screen_manager.add_widget(Builder.load_file("main.kv"))
screen_manager.add_widget(Builder.load_file("splashloading.kv"))
return screen_manager
def Login(self, *args):
screen_manager.current = "Login"
if __name__ == "__main__":
StartPage().run()
the error:
I am not sure about the gif, but I can give you code for a video player that has worked fairly smoothly in some of my projects.
Need to create a Video player class which can then be used in the .kv file
In .kv file
# This is only the code for the widget, the structure around it is not included
PlayerOpen:
id: player_open
opacity: 1
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
state: 'play'
Python code defining the widget
from kivy.uix.video import Video
video_list = ['your_video.mp4'] # Can uses a list of videos
class PlayerOpen(Video):
video_list = ['vid.mp4']
def __init__(self, **kwargs):
self.v = 0
super().__init__(source=video_list[0], state='stop', **kwargs)
def on_eos(self, *args):
print(f'on_eos: {self.eos} loaded: {self.loaded}, state: {self.state}')
if self.eos:
self.v += 1
if self.v == len(video_list):
print('End of playlist')
self.v = 0
return
self.source = video_list[self.v]
self.state = 'play' # must set state to play for video to be loaded
def on_loaded(self, *args):
print(f'loaded: {self.loaded}')
I know this isn't exactly what you asked for, but this might be able to solve your problem as you could still get the animation playing in your loading screen. Hope it helps
I created a stack layout with 100 buttons in it. This alone works but when I add the stack layout under scroll view layout nothing is displayed on the screen.without using scroll view
when scroll view is used
Python code
from kivy.app import App
from kivy.metrics import dp
from kivy.uix.stacklayout import StackLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
class stacklayoutex(StackLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
for i in range(100):
b=Button(text=str(i+1),size_hint=(None,None),size=(dp(100),dp(100)))
self.add_widget(b)
class MainWidget(Widget):
pass
class TheLabApp(App):
pass
TheLabApp().run()
KV file
scrollviewex:
<scrollviewex#ScrollView>:
stacklayoutex:
size_hint: 1,None
height:4000
<stacklayoutex>:
A couple problems:
Your class names must begin with upper case. Not following this rule can cause syntax errors in your kv.
Also, indentation error in your kv, it should be:
Scrollviewex:
<Scrollviewex#ScrollView>:
Stacklayoutex:
size_hint: 1,None
height:4000
<Stacklayoutex>:
im really new in Kivy and i would like to make an app where i could create a MDIconButton that is draggable, and if possible, droppable in any BoxLayout? Is that possible in KivyMD or Kivy? Also is there a Kivy function where whenever I hold down a button, it'll display some kind of small dialogue box that contains details that can be entered by the user. thanks!
Rather than use Drag-N-Drop from kivy-garden just use the DragBehavior class! It comes directly installed with Kivy and it'll save you having to install kivy-garden.
https://kivy.org/doc/stable/api-kivy.uix.behaviors.drag.html
Here is some example code of how it's used:
from kivy.uix.label import Label
from kivy.app import App
from kivy.uix.behaviors import DragBehavior
from kivy.lang import Builder
# You could also put the following in your kv file...
kv = '''
<DragLabel>:
# Define the properties for the DragLabel
drag_rectangle: self.x, self.y, self.width, self.height
drag_timeout: 10000000
drag_distance: 0
FloatLayout:
# Define the root widget
DragLabel:
size_hint: 0.25, 0.2
text: 'Drag me'
'''
class DragLabel(DragBehavior, Label):
pass
class TestApp(App):
def build(self):
return Builder.load_string(kv)
TestApp().run()
So I am just starting out with Kivy, and want to know how to
A: get the dimensions of the window (based on the platform)
The end goal is to launch the app on iOS, and there are multiple sized iPhones out there. How would I get the size of the screen of the device?
and B: somehow be able to refer to it
If I want to create a button that is centered on the screen, I need some way of getting the width_of_the_window and the length_of_the_window (these are obviously just made up variables).
Here is the code I am working on, although it is really basic so it probably won't provide much insight.
# Importing os
import os
# Importing Kivy, making sure its up to date, and importing App
import kivy
kivy.require('1.10.1')
from kivy.app import App
# Importing widgets, like buttons, drop down menus, etc
from kivy.uix.widget import Widget
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
button_one = Button(text='Test', font_size = 20, pos = (root.width-100, 100), size_hint = (.06, .06))
class calculator_app(App):
def build(self):
return button_one
calculator_object = calculator_app()
calculator_object.run()
Actually, you don't need the size of the window in order to do centering. Change your button creation line to :
button_one = Button(text='Test', font_size = 20, pos_hint={'center_x': 0.5, 'center_y': 0.5}, size_hint = (.06, .06))
Using pos_hint will position the button regardless of the window size. Have a look at the documentation for pos_hint
I'm writing a clock application (will eventually run on Raspberry Pi, but developing on Windows), and whenever a label updates the changed characters are drawn on top of the old ones, but the old one doesn't disappear.
It looks like this.
Here's my code:
main.py:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.properties import StringProperty
from kivy.config import Config
import time
class RPiClock(BoxLayout):
timeString = StringProperty()
def __init__(self, **kwargs):
super(RPiClock, self).__init__(**kwargs)
def update(self, *args):
self.timeString = str(time.strftime("%I:%M:%S %p"))
class RPiClockApp(App):
def build(self):
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '480')
appWindow = RPiClock()
Clock.schedule_interval(appWindow.update, 1)
return appWindow
if __name__ == "__main__":
RPiClockApp().run()
RPiClock.kv:
<RPiClock>
Label:
id: TimeLabel
text: root.timeString
font_size: '50sp'
What's causing these graphics to stick around?
I had a similar issue, try adding a colored canvas in the background -- in your case I would likely use a black rectangle that fills up your layout. It may solve your problem (it did for me).