Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I position an image in kivy using the python language?

How do I position the image using python as opposed to the kivy language (.kv files)?

My work is as follows.

import kivy
kivy.require('1.8.0') # current kivy version
import ConfigParser

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.image import Image





class login(Widget):
    #checking to see if the user logging in has privilage to access program
    def validate(self, *args):

        username = self.ids['user']
        user = username.text
        config = ConfigParser.ConfigParser()
        config.read('Privilage.cfg')
        if not config.has_section('users'):
            print 'the Privilage.cfg file has been tampered with'

        if not config.has_option('users',user):
            print 'user is not listed'
        else:
            userPriv = config.get('users',user)
            print 'user',user,'has privilage',userPriv
            with self.canvas:
                Image(source='/Users/Kevin/Desktop/Python-extras/SSS Assistant/Manager_images/check-icon.png',pos=self.pos,size=self.size)



class DataApp(App):
    def build(self):
        return login()


if __name__ == '__main__':
    DataApp().run()

My goal is to verify if the user exists and then put a check mark by the username box if it does and then later (no code yet) verify a matching password (no code yet either). I didn't want to code what happens if the verification fails or password matches yet because if I can't change the image position there is not point.

Currently the image appears right in the center and is very large (I need to shrink it too). I've searched the Kivy API in kivy.uix.image.Image and I can't find a solution (searched for position change or center_x kind of stuff and can't find). I am trying to do this in python because I'm putting the image constructor in the if statement and I'm not sure .kv files can do that since they control properties.

**The line I'm concerned with is:

with self.canvas:
    Image(source="path",pos=self.pos,size=self.size)

Here is the accompanying kivy file just in case:

#:kivy 1.8.0

<login>:
    canvas:
        Rectangle:
            source:"/Users/Kevin/Desktop/Python-extras/SSS Assistant/Manager_images/background.jpg"
            pos: self.pos
            size: self.size
    Label:
        center_x: root.width / 2
        top: (root.top/2) + 150
        text: 'Please login with your ID and password'
    Label:
        center_x: (root.width/2)-130
        top: (root.top/2)+110
        text: 'Username:'
    Label:
        center_x: (root.width/2)-130
        top: (root.top/2)+83
        text: 'Password:'
    TextInput:
        id: user
        center_x: (root.width/2) +10
        top:(root.top/2)+75
        size_hint: None, None
        size: 200, 30
        max_lines: 1
        valign: 'middle'
        halighn: 'center'
        font_size: 15
        multiline: False
        on_text_validate: root.validate()
    TextInput:
        id: password
        center_x: (root.width/2) +10
        top:(root.top/2)+45
        size_hint: None, None
        size: 200, 30
        max_lines: 1
        valign: 'middle'
        halighn: 'center'
        font_size: 15
        multiline: False
        password: True

Thanks in advance!

like image 673
Bystander001 Avatar asked Jun 01 '26 08:06

Bystander001


1 Answers

with self.canvas:
    Image(source="path",pos=self.pos,size=self.size)

Image is a Widget, not a canvas instruction. You should add it with self.add_widget and not use the canvas at all.

I think you're also reinventing the wheel a bit with all your position and size instructions in kv, you might find it easier to use kivy's layout classes to manage much of this for you.

like image 184
inclement Avatar answered Jun 04 '26 08:06

inclement



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!