Python Kivy window resize is jittery - python

Hey there i am new to kivy and am using Python 3.8.10 and Kivy version: 2.1.0
i am trying to get the window resize working and pulled this working example.
I am on ElementaryOS
When i run the following code and resize the window, it jumps around the screen towards the left bottom corner and is super jittery, when resizing to full screen the content is not staying center but moving to the left bottom corner...
# To change the kivy default settings
# we use this module config
from kivy.config import Config
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', True)
# import kivy module
import kivy
# this restrict the kivy version i.e
# below this kivy version you cannot use the app
kivy.require("1.9.1")
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
# if you not import label and use it through error
from kivy.uix.label import Label
# defining the App class
class MyLabelApp(App):
def build(self):
# label display the text on screen
# markup text with different colour
l2 = Label(text ="[color = ff3333][b]Hello !!!!!!!!!!![/b] [color]\n [color = 3333ff]GFG !!:):):):)[/color]",
font_size ='20sp', markup = True)
return l2
# creating the object
label = MyLabelApp()
# run the window
label.run()

Related

os.system() don't let updating image source in kivy python

I am trying to update the image source on a changeImageSource function it changes the source instantly but when I use time.sleep() method in that function, function executes but doesn't update the source of the image. updates after time.sleep() call completed.
from kivy.app import App
from kivy.uix.image import AsyncImage
from kivy.uix.button import Button
from kivy.uix.widget import Widget
import time
# creating the App class
class MyApp(App):
def build(self):
parent = Widget()
#creating and adding image to widget
self.img = AsyncImage(
source='http://kivy.org/logos/kivy-logo-black-64.png')
self.img.pos = (400,400)
#creating btn and adding press handler
self.change_img_btn = Button(text="Change Image ")
self.change_img_btn.bind(on_press = self.changeImageSource)
#adding widget to Widget instance
parent.add_widget(self.img)
parent.add_widget(self.change_img_btn)
return parent;
def changeImageSource(self,*args):
self.img.source = "https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a"
time.sleep(4)
# run the App
MyApp().run()
One: The "return parent;" should be "return parent"
Two: Why do you need time.sleep()?
You can also try flipping line 29 and 30.

How to add background image in Kivy without kv language

I'm creating an Kivy App for Desktop. I've created most of the app but I want to add a background image to the app. I've not use the KV language but created all the widgets using Python code only.
Can anybody please help me adding a background image in the kivy app using Python.
You can use with canvas: to draw a background image. Here is a simple example:
from kivy.app import App
from kivy.clock import Clock
from kivy.graphics.vertex_instructions import Rectangle
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
class TestApp(App):
def build(self):
theRoot = FloatLayout()
# draw the background
with theRoot.canvas:
self.rect = Rectangle(source='background.png')
# use binding to insure that the background stay matched to theRoot
theRoot.bind(on_size=self.update)
theRoot.add_widget(Label(text="Hi", size_hint=(None, None), size=(100, 50), pos=(100,100)))
# need to call update() to get background sized correctly at start
Clock.schedule_once(self.update, -1)
return theRoot
def update(self, *args):
# set the size and position of the background image
self.rect.size = self.root.size
self.rect.pos = self.root.pos
TestApp().run()

Python - Modules not working

So i downloaded the "kivy" module (using the 'command prompt') as shown during this video "https://www.youtube.com/watch?v=B79miUFD_ss&list=PLGLfVvz_LVvTAZ-OcNIXe05srJRXaJRd9"
But whenever i try to run this code:
import kivy
kivy.require('1.9.0')
from kivy.app import App
from kivy.uix.button import Label
# Inherit Kivy's App class which represents the window
# for our widgets
# HelloKivy inherits all the fields and methods
# from Kivy
class HelloKivy(App):
# This returns the content we want in the window
def build(self):
# Return a label widget with Hello Kivy
return Label(text="Hello Kivy")
helloKivy = HelloKivy()
helloKivy.run()
It says the module doesn't exist?
How do i find the path of this module (and other modules)?

Kivy TextInput background problems

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")

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