Export_to_png only saves background - python

I recently learnt about the Python framework kivy and started to follow the simple paint app tutorial. Now I want to save what was drawn as a .png file but instead of the colored dots it only exports the black background.
This is my code example:
import kivy
kivy.require("1.9.1")
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line
class MyPaintWidget(Widget):
def on_touch_down(self,touch):
color=(0,0,1)
with self.canvas:
Color(*color)
d=30.
Ellipse(pos=(touch.x-d/2., touch.y-d/2.), size=(d,d))
class MyPaintApp(App):
def build(self):
parent=Widget()
self.painter=MyPaintWidget()
ssbtn=Button(text='Save')
ssbtn.bind(on_release=self.save_screenshot)
parent.add_widget(self.painter)
parent.add_widget(ssbtn)
return parent
def save_screenshot(self,obj):
self.painter.export_to_png("screenshot.png")
if __name__ == '__main__':
MyPaintApp().run()
What am I doing wrong? Thanks for your help.

You are missing one thing - Widget won't set its size and position automatically to whole Window size, but only to default ones i.e. pos = [0, 0] size = [100, 100] because Window != Layout and even adding it to something that doesn't inherit from a Layout won't make it better (Widget != Layout) which you can easily see if you look a that saved png (is too small).
self.painter=MyPaintWidget(size=[800,600])
will make the job for you if you plan to stay only on pc and only with the default Window size. If not, then use:
from kivy.core.window import Window
self.painter=MyPaintWidget(size=Window.size)
which seems to be more practical, yet you're still not there. An optimal solution would be using a Layout which does these things for you such as BoxLayout, GridLayout or some others.
And a perfect solution for you would be to use a StencilViewin a Layout, which when is placed won't take a screenshot of the whole canvas, but only the part you'd think it'll take i.e. the part sized as the Widget itself placed on its position.
Try drawing outside of the red box. Inspector will provide you with showing widgets in a color and other functions, so definitely try it as it was mentioned in the comments.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.stencilview import StencilView
from kivy.graphics import Color, Ellipse, Line, Rectangle
class MyPaintWidget(StencilView):
def on_touch_down(self,touch):
color=(0,0,1)
with self.canvas:
Color(*color)
d=30.
Ellipse(pos=(touch.x-d/2., touch.y-d/2.), size=(d,d))
class MyPaintApp(App):
def build(self):
parent=Widget()
self.painter=MyPaintWidget(size=[i/2.0 for i in Window.size])
with self.painter.canvas:
Color(1, 0, 0, 0.3)
Rectangle(pos=self.painter.pos, size=self.painter.size)
ssbtn=Button(text='Save')
ssbtn.bind(on_release=self.save_screenshot)
parent.add_widget(self.painter)
parent.add_widget(ssbtn)
return parent
def save_screenshot(self,obj):
self.painter.export_to_png("screenshot.png")
MyPaintApp().run()

Related

Get global position of widget in Kivy?

I have an image located relative to the screen size with RelativeLayout. How can I get the global (/window) position of that image, so that I can detect if touch coordinates overlap the image?
this is my current implimentation:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.relativelayout import RelativeLayout
class mainApp(App):
def build(slef):
appRoot = FloatLayout()
relPos = RelativeLayout()
button = Image()
relPos.pos_hint = {"right":1,"top":1.5}
relPos.add_widget(button)
#attempting to print the coordinates of the Image in the window
print(relPos.to_parent(*button.pos))
return relPos
if __name__ == '__main__':
mainApp().run()
As you can see, I'm attempting to use to_parent to get the position relative to the parent. However, I don't think it works pos_hint.
I've also tried using AnchorLayout, and have had similar issues
Note: while I know I can use the window_size, allong with a bit of math, I would like a more robust solution that can handle more complex situations

Trying to use inheritance in kivy python

I am trying to create a simple programe that animates my label widget to desired position upon clicking on the kivy window. Just trying to experiment with inheritance to access all the elements in the parent element.
ani is the parent class and transit is the child class. I am trying to access the label attribute in transit via inheritance. But this throws error.
from kivy.app import App
from kivy.uix.label import Label
from kivy.animation import Animation
from kivy.uix.widget import Widget
class transit(ani,Widget):
def __init__(self,**kwargs):
ani.__init__(self,**kwargs)
def on_touch_down(self,touch):
val = 5
print(touch.x,touch.y)
self.val +=10
animation = Animation(x = touch.x,y =touch.y,font_size=self.val,d=2,t='in_out_quad')
animation.start(self.parent.label)
class ani(App):
def __init__(self,**kwargs):
self.label = Label(text='increase')
def build(self):
return transit()
root = ani()
root.run()
I believe you are trying to use inheritance with the wrong class. If you want to move a Label, you need to put the Label in a container and make sure the size of the Label does not fill the container. Since the position of the Label is controlled by its container, you need to use inheritance with the container. Here is a simple code, similar to yours, that uses a FloatLayout as the container:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.animation import Animation
class MyFloatLayout(FloatLayout):
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
val = 5
label = App.get_running_app().label
animation = Animation(x = touch.x,y =touch.y,font_size=val,d=2,t='in_out_quad')
animation.start(label)
return super(MyFloatLayout, self).on_touch_down(touch)
class ani(App):
def build(self):
root = MyFloatLayout()
self.label = Label(text='increase', size_hint=(None, None), size=(100,40))
root.add_widget(self.label)
return root
if __name__ == '__main__':
ani().run()
This code uses inheritance to define a new MyFloatLayout class that adds a new on_touch_down() method that does the animation. Note that the new on_touch_down() also calls super(MyFloatLayout, self).on_touch_down(touch). Unless there is a specific reason for not doing that, you normally want to call the super method in an inherited method.

How to dynamically change Kivy window dimensions based on platform

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

kivy python widget instance or all widgets

Please help me with understanding classes/instances in python. I want to make a few buttons, and change color of the button, when it's clicked. I don't understand why on_touch_down changes the color of all the instances of the class, not the one that is touched. It's difficult for me to find answer because I don't know how to name it, I don't have much experience with objects. Please explain this. Thank you a million.
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.graphics import Color, Ellipse
class MemoWidget(Button):
def on_touch_down(self, touch):
self.background_color=[100,100,1,1]
class MyApp(App):
def build(self):
root = BoxLayout(orientation='vertical',spacing=4)
m1 = MemoWidget()
m2 = MemoWidget()
m3 = MemoWidget()
root.add_widget(m1)
root.add_widget(m2)
root.add_widget(m3)
return root
if __name__ == '__main__':
MyApp().run()
You might think that on_touch_down only affects the widget you touch. But it affects all widgets of that class.
So what you might want, is on_press or on_release, to only affect the widget itself.
class MemoWidget(Button):
def on_release(self):
self.background_color=[100,100,1,1]

Issues with changing color of basic Kivy app

I created a simple text-to-speech app with Kivy, using the FloatLayout option but am having trouble changing the color of the GUI without actually creating a .kv file (which I do not wish to do). The code of my app is here:
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
import requests
from threading import Thread
import os
class ButtonApp(App):
def talk(self):
self.info.text = self.text.text
command = "say %s" % (self.text.text)
os.system(command)
def say(self,instance):
t = Thread(target=self.talk)
t.start()
def build(self):
self.b = FloatLayout()
self.info = Label(text="Hello!", pos=(20,400) ,size_hint=(1,0.5), font_size="40sp")
self.text = TextInput(text='Hello!', pos=(20,200), size_hint=(1,0.5))
self.submit = Button(on_press=self.say,text='Submit',pos=(20,100), size_hint=(1,0.5))
self.b.add_widget(self.info)
self.b.add_widget(self.text)
self.b.add_widget(self.submit)
self.b.bind()
return self.b
if __name__ == "__main__":
ButtonApp().run()
Like I mentioned beforehand, all the suggestions I found doing prior research involved either Canvas (which I am not using), or creating a .kv file. Is there a pure python-kivy method of changing the color of a GUI?
You can do anything in pure python, though the reason you see so many kv examples is because it's easier and more concise due to being a more domain specific language, so I don't recommend avoiding it.
What kind of change do you actually want to make? For instance, you can change the background image of the Button with the background_normal or background_down properties (which take a filepath to an image), or tint its colour by setting its background_color to e.g. (1, 0, 0, 1) for red.

Categories

Resources