VScode doesn't use the kivy file - python

I'm new to programming and trying to use Kivy to create a simple game.
I am following a tutorial, and I'm using VSCode, but I can't understand why the code I write in the .kvfile doesn't is used by the main.pyfile.
I have two files in the directory.
main.py
from kivy.app import App
from kivy.uix.widget import Widget
class MainWidget(Widget):
pass
class TheLabApp(App):
pass
TheLabApp().run()
thelab.kv
MainWidget:
<MainWidget>:
Button:
text: 'Hello'
size: 400, 200
I have installed a Kivy extension, and of course the Kivy module. But when I run the code the only thing that appears is a black screen, without the button.
What is happening?

I found the solution. It is required to save the .kv file before running the code, it is just such a sily thing, but if it isn't saved it is read like if it was empty, or never changed.

Related

Kivy button embedded in code doesn't appear on the screen

I don't know what I am doing wrong. I follow the tutorial but button appears only if i use .kv file.If I want to add it directly in python code,it doesn't appear.
class StackLayoutExample(StackLayout):
def _init__(self,**kwargs):
super().__init__(**kwargs)
b = Button(text='Z')
self.add_widget(b)
TheLabApp().run()
`
I tried to google it but still it works only with .kv file

Error while drawing kivy rectangle using canvas

I need some help with kivy,i am pretty new to kivy and I made a class to to draw a rectangle as a background.
I am pretty sure I did everything correctly but there is an error,so here is my code
The .py file
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
class Background(Widget):
pass
class MY_browser(App):
def build(self):
return Background
MY_browser().run()
The .kv file
Floatlayout:
Background:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
I have tried a lot of things but no difference so if anyone can help I would really appreciate it
A couple problems:
In your build() method, the return Background is returning a class, but the build() method is expected to return a Widget instance. Perhaps this should be return Background().
In your kv, the indentation is incorrect. All indentations should be a multiple of the same number of spaces (typically 4). The indentation of Background is too large.
Your kv does not provide a rule for Background, so when Background() is returned, it will simply be an empty Widget. If you want to return Background(), you should have a <Background>: rule in the kv.
If your kv file is named my_browser.kv, then you do not need a build() method for your App class at all.
If your kv file is not named as above, then your build method can be return Builder.load_file("kv file name"), where kv file name is replaced by the correct name of your kv file.
The Floatlayout in your kv is misspelled. It should be FloatLayout.

Kivy: Instance of 'XXXX' has no 'bind' member

I am writing a program in Kivy, and whenever I am trying to bind a function to a Button or any other widget I get the following problem from pylint: "Instance of 'Button' has no 'bind' member" and the lines turn red. I am completely new to Kivy and this really bugs me.
The program works perfect when I execute it, and the compiler does not seem to have any problem with my bindings. What have I done wrong? Am I missing an import or anything, or is there something wrong with my environment?
Attached you find a code snippet I wrote as an example.
from kivy.app import App
from kivy.uix.button import Button
class MainApp(App):
def build(self):
button = Button(text='Hello from Kivy',
size_hint=(.5, .5),
pos_hint={'center_x': .5, 'center_y': .5})
button.bind(on_press=self.on_press_button)
return button
def on_press_button(self, instance):
print('You pressed the button!')
if __name__ == '__main__':
app = MainApp()
app.run()
It's likely that whatever linter you are using is not able to find the bind method because it comes from cython code. You need to configure it differently (if possible), or use a different linter.

Dynamically deleting and loading Screen object in Kivy

I am currently developing an embedded system with kivy.
Therefore, I found that if I make many screens, it slows down the program a lot.
Is there a good way to dynamically control screens so it does not slow down?
For instance, when I have 4 screens in ScreenManager like below,
MyScreenManager:
id: myscreenmanager
transition: FadeTransition()
SCRN_LOADING:
SCRN_IDLE:
SCRN_CALCULATING:
SCRN_RESULT:
Would it be possible to:
innitially load SCRN_LOADING first.
loads SCRN_IDLE and SCRN_CALCULATING while loading.
when loading is done, remove SCRN_LOADING screen object.
loads SCRN_RESULT while calculating.
when going back to idle, remove SCRN_RESULT screen object.
I am guessing this could improve performance.
Currently, the screen lags really hard. So I might have to restart the whole project using C if I can't solve the performance issue.
Please help me out!
I suppose you could declare your screens outside kv and then add them as required in your screen manager,In your kv
MyScreenManager:
id: myscreenmanager
transition: FadeTransition()
In your Window class:
from kivy.uix.screenmanager import ScreenManager, Screen
...
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.sm = self.ids.myscreenmanager
self.loading = Screen(name='SCRNLOADING')
self.idle = Screen(name='SCRN_IDLE')
self.calc = Screen(name='SCRN_CALCULATING')
self.sm.add_widget(self.loading)
self.set_idle()
def add_scrn(self):
self.sm.add_widget(self.idle)
self.sm.add_widget(self.calc)
self.sm.remove_widget(self.loading)
Im not really used to the Clock class but I'm sure you will need it here to load your screens correctly

Programmatically controlling a carousel in kivy

I'm new with kivy, but I have decent experience with Python and Tkinter. I'm trying to control a carousel in kivy programmatically. Essentially, I have an external python program which I want to use to automatically switch images in the carousel. To make an example, I have some code in another file:
import time
while True:
time.sleep(1)
#do something to control the carousel
and then I have my kivy app:
import kivy
from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.image import AsyncImage
class CarouselApp(App):
self.srcs = ["/a/bunch.png", "/of/paths.jpg", "/to/images.png."]
def build(self):
self.carousel = Carousel(direction="right")
for i in range(0, len(self.srcs)):
src = self.srcs[i]
image = AsyncImage(source=src, allow_stretch=True)
self.carousel.add_widget(image)
return self.carousel
if __name__ == "__main__":
CarouselApp().run()
I would like to be able to control which slide is displayed in the carousel using the top code, but I'm not sure how I would go about doing that, since I can't execute anything after App.run()
I have investigated kivy's Clock module, but I'm not sure that would work for me since I want to switch slides when certain conditions are satisfied rather than on a time basis. The time example I gave is simply an example of my line of thinking.
Any help would be greatly appreciated!
I strongly suggest using a Kivy file to handle this situation. Second, AsyncImage is used when you want to use an image that will be downloaded from the Internet, and as I can see you have the images you are going to use are locally stored, so use Image instead.
I think you should implement the method on_start in the class which is extending App (CarouselApp in this case) and schedule a function using Clock as,
def on_start(self):
Clock.schedule_interval(self.my_callback, 1)
Then in the same class (CarouselApp) you should define my_callback:
def my_callback(self, nap):
if condition_is_satisfied: # this is supposed to be your condition
# control carousel

Categories

Resources