I cant seem to find any examples of interfacing kivy mapview with python for things like moving the map or adding markers from the python side of things.
What I mean is I would like to eventually pull data from a CSV file and have it control the information on screen. This is straightforward with labels and buttons. But I'm not able to make the same methides work with mapview.
All references I find on line controlled all logic inside the .KV files. None of the examples I find use python to reach in and change things inside the .KV file.
This is the best example I have tested so far. but as you can see all logic is inside the .KV file. https://github.com/kivy-garden/mapview/blob/develop/examples/map_browser.py
I want to be able to send data from the python side to the .kv side and have it update the screen as its being sent. Like Lat and lon positions, markers and so on.
What I am use to doing something like this.
Python side
from kivy_garden.mapview import MapView
from kivy.lang import Builder
from kivymd.app import MDApp
class MainApp(MDApp):
def build(self):
return Builder.load_file('MapViewApp.kv')
MainApp().run()
MapViewApp.kv
MapView:
id: map
lat: 36
lon: -115
zoom: 10
Related
I have an application that at a certain point needs to check if a variable within its Python code has one value or another, and also requires a button capable of changing that variable from one value to another to also save that information in a text file and that the variable loads it the next time the application is started.
Here I prepared a small program to exemplify what I am looking for:
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang.builder import Builder
from kivy.core.window import Window
Window.size=(200,200)
data=open('data.txt','r') #Let's say there is a text file called "data" that contains the number 0
Builder.load_string("""
<Screen>
BoxLayout:
orientation:'vertical'
size:root.width,root.height
Label:
text:'Switch On' if app.var==1 else 'Switch Off' #Initially then the label should say "Switch Off"
Button:
text:'Switcher'
on_release: #Change app.var from 0 to 1 or from 1 to 0
""")
class Screen(Widget):
pass
class App(App):
def build(self):
self.var=data.read()
#Save the changes made by the Button
return Screen()
if __name__=='__main__':
App().run()
How can I do this or something similar? A better and simpler way?
You should define a function and bind it to a button. The documentation can really help you here! you can bind your function using my_button.bind(on_press=on_button_click) where:
def on_button_click():
this.my_var += 1
Let me know if you think the documentation is unclear!
I want to change the source path of an image from outside of the class/screen where the image is shown.
I have a callback function called my_callback, which will be called at some point during runtime of the app:
def my_callback():
# do stuff
MDApp.get_running_app().manager.get_screen('my_class').ids.imageID.source = "my_image.png"
MDApp.get_running_app().manager.current = "my_class"
I would expect the above two lines of code to do the following:
Switch the kivy screen to the my_class screen
Update the source of the image with the id "imageID" (as defined in my .kv file).
Outcome (1) is successful, but outcome (2) is not: rather than showing the image "my_image.png", a black shape is shown of equivalent dimensions to the image "my_image.png".
How can I fix this?
Note that MDApp is used here in place of App, as I am using the KivyMD library for my project.
I still don't know why the method I posted in the question doesn't work, but I have found an alternative solution.
The function my_callback now looks like this:
def my_callback():
# do stuff
MDApp.get_running_app().manager.current = "my_class"
image = AsyncImage(source='my_image.png')
MDApp.get_running_app().manager.get_screen('my_class').ids.floatLayoutID.add_widget(image)
And in the .kv file, there is a nested floatLayout:
<MyClass>:
name: "my_class"
FloatLayout:
FloatLayout:
id: floatLayoutID
Was wondering why the Kivy code kept on showing me the same black window despite doing some updates on the kv file. Then noticed I had a typo on the buidl() method.
From the docs "...implementing its build() method so it returns a Widget instance (the root of your widget tree)
...", you have to implement the method.
Why does this code run and give the default black window?
# game.py
from kivy.app import App
from kivy.uix.widget import Widget
class Game(Widget):
pass
class GameApp(App):
def buidl(self):
return Game()
GameApp().run()
The kv file
#game.kv
<Game>:
canvas:
Color:
rgb: .5,.5, 1.0
Rectangle:
pos: 0,0
size: self.size
Running kivy 1.11.1 python 3.7
Kivy apps have a default build() method, which you can see here; it just returns an empty widget. Generally kivy has two methods to create the root widget tree, either through overriding build() or by defining a root widget in a kv file. For more information see the documentation on creating an application.
Your quote can be found in kivy basics, before your quoted sentence:
Creating a kivy application is as simple as:
I guess the authors decided to keep the basic tutorial easy and did not mention the default implementation of build, as it doesn't really do anything useful. They also omitted the kv way of defining the root widget; again I would guess to not overwhelm the reader in this first introduction.
I have noticed that most examples I foind online don't have an external .kv file. They define all the instances internally. However they also say that having an external .kv file is a good practice. Which is better to do? If having external .kvfiles are better, then how am I supposed to use the code which uses internal code and turn it into external .kv files?
For example, doing this ->
from kivy.app import App
from kivy.uix.scatter import Scatter
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
class TutorialApp(App):
def build(self):
b = BoxLayout(orientation='vertical')
t = TextInput(font_size=150,
size_hint_y=None,
height=200,
text='default')
f = FloatLayout()
s = Scatter()
l = Label(text='default',
font_size=150)
t.bind(text=l.setter('text'))
f.add_widget(s)
s.add_widget(l)
b.add_widget(t)
b.add_widget(f)
return b
if __name__ == "__main__":
TutorialApp().run()
Instead of-
<ScatterTextWidget>:
orientation: 'vertical'
TextInput:
id: my_textinput
font_size: 150
size_hint_y: None
height: 200
text: 'default'
FloatLayout:
Scatter:
Label:
text: my_textinput.text
font_size: 150
"Internal" usage of kv is through Builder class, which allows you even to load external file. Those examples are worded in a Builder.load_string(...) way because it's way simpler to have a small example in one place in one file.
How to convert it to the external one? Simple, copy&paste the string from Builder.load_string() into a separate .kv file with a name of your class that inherits from App(your main class with build()) and that's it. It'll load the same thing from the external file.
Why it's better or worse? Isn't any of that actually. It's like comparing "java" and python style i.e. either putting everything out of your file and having basically this construction a'la java, where the main file contains this:
class This(something):
SpecialClass.doThis()
AnotherClass.doThat()
and other classes(or different things) are in separate files. Or this construction:
class Special(...):
...
class Another(...):
...
class This(something):
Special.do_this()
Another.do_that()
Both of them are useful and you'll find yourself working with a mix between them. It's about transparency and clearness of your code, but maybe you don't want to have a hundred of files... or a 2MB main.py, pretty much compromise of how do you decide to code.
Edit:
The python vs kv is a funny "fight", but except a for(and while?) loop you can pretty much do everything necessary inside kv in such an easy way! Kv is here to make writing easier e.g. remove too much stuff like add_widget() or basically making an empty class just to rename a widget or to change its size for using it in one place.
With python in a 500line file without kv you won't do that much as with 100 extra lines in kv. The documentation has important parts in python and maybe it's even targeted for users who can't/don't want to use kv. Also this and all examples highly depend on the author of an example and that particular part of the docs.
Which returns me back to the java vs python style of coding I mentioned before. It's pointless to do complicated stuff just because you think it'll feel/look better if you can do it cleaner and more readable i.e. don't just go one way if you have a tool that increase your speed of coding exponentially. Find the middle way.
How do I use the kivy module garden.graph inside the kv file? I only found documentation that explained how to use it in the main python script.
I imported the kivy.garden.graph in the python file, and I can add the Graph inside the kv file, but I didn't find any documentation how to set the size, plots etc.
Graph:
id: graph_test
plot: MeshLinePlot
this gives an error since MeshLinePlot is not defined, though I imported it on the python side.
any help would be highly appreciated, maybe we could then add this info to the graph's github readme as well.
Building on the answer from piwnk:
I added this to the .kv file:
#:import MeshLinePlot kivy.garden.graph.MeshLinePlot
<SetGraph>:
graph_test : graph_test
Graph:
id: graph_test
plot: MeshLinePlot
xlabel:'X'
ylabel:'Y'
x_ticks_minor:5
x_tics_major:25
y_ticks_major:1
y_grid_label:True
x_grid_label:True
padding:5
x_grid:True
y_grid:True
xmin:-0
xmax:100
ymin:-1
ymax:1
pos: 0, root.height / 6
size: root.width * 2 / 3 , root.height * 18 / 24
In main.py, I added:
from math import sin
from kivy.garden.graph import Graph, MeshLinePlot
class SetGraph(Widget):
graph_test = ObjectProperty(None)
update_graph(self):
plot = MeshLinePlot(color=[1, 0, 0, 1])
plot.points = [(x, sin(x / 10.)) for x in range(0, 101)]
self.graph_test.add_plot(plot)
class graphLayoutApp(App):
def build(self):
disp = SetGraph()
disp.update_graph()
return disp
if __name__ == '__main__':
graphLayoutApp().run()
I have changed my original tested solution to more descriptive names. Hopefully, I have not made any mistakes. Let me know if the solution is not complete.
The answer from Mattis Asp was very helpful but didn't quite work for me. I am new to this, so maybe these things are too obvious to need stating. But in case it helps someone else at my level, I had to:
Indent the properties under the Graph: declaration in the kv file (to get around an "invalid data after declaration" exception from the kv parser.
Add these includes:
language: lang-py
from kivy.properties import ObjectProperty
from kivy.app import App
from kivy.uix.widget import Widget
to the top of the python file.
Name the kv file to match the app class definition, so: graphLayout.kv (I had called it graph.kv so it was just ignored -- newbie mistake!)
I was getting "invalid property name" for graph_test : graph_test. So I commented that out and used the id instead, changing the line
self.graph_test.add_plot(plot)
to:
self.ids["graph_test"].add_plot(plot)
I bet at least some of these changes have to do with version differences in kivy so, for clarity, I am using kivy 1.9.1 and python 2.7.13.
Had the same problem. Here's the solution:
Generally, according to kivy documentation, in kv file:
#:import name x.y.z
is equivalent to:
from x.y import z as name
So you should use the following:
#:import MeshLinePlot kivy.garden.graph.MeshLinePlot
Worked in my case with Graph class but, to be honest, I didn't managed to add this plot to the graph yet.
I think inclement was on the right track. Using #:import should be able to import the file.
write this in the kv file:
#:import MeshLinePlot
it should be able to import the module, as the kv documentation shows, also