Kivy TextInput background problems - python

I don't like the default gradient background for the unfocused TextInput so I edited the default texture in Paint. But it doesn't seem to recognize it. It's in a separate file and looks like this:
The texture I get is just plain white. Do I need a specific file with the texture or to move the texture to the place where it is in the default texture?
This is a test code:
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
class App1(App):
def build(self):
b = BoxLayout()
tx1 = TextInput()
tx = TextInput(background_normal = "E:\textinput_unfocused.png")
b.add_widget(tx1)
b.add_widget(tx)
return b
App1().run()
Oh, by the way. The source code is located in the E: disk, as well as the textinput_unfocused.png.

the path for Your background should be specified relative to the app file. So if they are both in the same directory just put:
tx = TextInput(background_normal = "textinput_unfocused.png")

Related

Kivy - I can't see the picture

I am developing a mobile application. There should be a picture in the introduction of my application. Even though I put the picture and show the way, the picture does not appear when I run the application. It is replaced by a white screen. How can I solve it?
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.image import Image
from kivy.animation import Animation, AnimationTransition
class loginImage(Image):
pass
class loginScreen(App):
def build(self):
img = Image()
return img
loginScreen().run()
My .kv code in here:
<-loginScreen>:
source: 'images.jpeg'
kv rules are for widgets, not the App class, so your source isn't applied to anything.
Set the source of the Image widget that you return.

Kivy Does Not Output Plain Text, Only Object Location

So I was trying to create a very basic text editor in Kivy. So I had got around the issue of text not showing up (via lambda). However, a new error appeared on the horizon; saving. I want to save automatically to a plain txt file. However, my current code only save an object (is it a pointer?) of the actual text input. Thank you all, SO!
import kivy
import os
kivy.require('1.10.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.lang import Builder
class ColdKivyApp(App): # I actually used to call it Zone but changed it to Cold cause it's cold outside ;)
def build(self):
f = BoxLayout(orientation='vertical')
txt = TextInput(multiline=True, cursor_blink=True, background_color=(1,1,1,1))
f.add_widget(txt)
txtstr = str(txt)
Clock.schedule_once(lambda *args: setattr(txt, "focus", True))
with open('testtxt.txt', 'w') as txtwriter:
txtwriter.write("" + txtstr)
txtwriter.close()
return f
if __name__ == '__main__':
ColdKivyApp().run()
Edit: Spelling
TextInput has a text property, that's where the current content is.
Replace txtstr = str(txt) in your code to txtstr = txt.text.

Kivy image not showing

Im working on a basic app using python and kivy and im trying to import an image which I'm basically going to use as a splash screen of sorts but for some reason I couldn't get it to work so I created a .py file and wrote some very simple code to try to get it to work:
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
Window.size = (360, 640)
class ImageTestApp(App):
def build(self):
label = Label(source="image.jpg")
return label
if __name__ == "__main__":
ImageTestApp().run()
The image is in the same directory of the .py file and the name of the image is exactly the same, I even made the window size the same as the image size and tried .PNG but nothing worked 😖
Label does not have an attribute called source. You should change Label to Image, and it will works. Please refer to example below for details.
Example
main.py
from kivy.app import App
from kivy.uix.image import Image
from kivy.core.window import Window
Window.size = (360, 640)
class ImageTestApp(App):
def build(self):
return Image(source="image.jpg")
if __name__ == "__main__":
ImageTestApp().run()
Output

Extending the Kivy ScrollView contents down

I have a FloatLayout as a child of a ScrollView with size_hint_y set to None. I want to be able to extend it as I add more and more content. The problem is that since Kivy's coordinate system starts at the bottom-left, when I add to the FloatLayout height, all the content stays at the bottom. Can I somehow make it extend down? Because I don't think that moving all widgets up is efficient, especially if there's a lot of them and I need to handle the position of all children as well.
Here is a snippet that explains the problematic behaviour:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
class TestApp(App):
def extend_h(self, *args):
global msg_float
msg_float.height += 50
def build(self):
global msg_float
msg_float = FloatLayout(size_hint_y = None)
bt1_main = Button(on_press = self.extend_h)
bl = BoxLayout()
sc = ScrollView()
sc.add_widget(msg_float)
bl.add_widget(sc)
bl.add_widget(bt1_main)
lb = Label(text = "Test",
size=(100,200),
size_hint = (None, None))
msg_float.add_widget(lb)
return bl
TestApp().run()
With a press of a button, the view extends and the "Test" label stays at the bottom, but I'd want it to stay on top.
You could use a relative layout instead of a float layout to fix the coords, but instead you should just omit using any of these, and add labels to a grid layout. Check examples at kivy repo:
https://github.com/kivy/kivy/blob/master/examples/widgets/scrollview.py
https://github.com/kivy/kivy/blob/master/examples/widgets/scrollview.kv

How to disable image stretching In the application kivy-example/demo/pictures/main.py

Kivy is provided with some demo. In one of them kivy-example/demo/pictures/main.py, some images can moved and stretched. I started to modify this app . My question is how to disable image stretching ?
Thanks
You can set unwanted interactions to false: http://kivy.org/docs/api-kivy.uix.scatter.html#control-interactions
Add to your .kv file:
<Picture>:
# ...
do_scale:False
# ...
Or to your class:
from kivy.properties import BooleanProperty
class Picture(Scatter):
# ...
do_scale = BooleanProperty(False)

Categories