如何使用python语言在kivy中定位图像?

时间:2014-06-17 01:43:21

标签: image python-2.7 position kivy

如何使用python而不是kivy语言(.kv文件)定位图像?

我的工作如下。

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

我的目标是验证用户是否存在,然后根据用户名框选中复选标记(如果有的话),然后稍后(还没有代码)验证匹配的密码(也没有代码)。如果验证失败或密码匹配,我不想编码会发生什么,因为如果我无法更改图像位置则没有意义。

目前,图像显示在中央,非常大(我也需要缩小它)。我在kivy.uix.image.Image中搜索了Kivy API,我找不到解决方案(搜索位置更改或center_x类型的东西,但无法找到)。我试图在python中执行此操作,因为我将图像构造函数放在if语句中,我不确定.kv文件可以执行此操作,因为它们控制属性。

**我关注的一句话是:

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

以下是随附的kivy文件:

#: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

提前致谢!

1 个答案:

答案 0 :(得分:1)

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

图像是窗口小部件,而不是画布指令。您应该使用self.add_widget添加它,而不是使用画布。

我认为您还要用kv中的所有位置和大小说明重新发明轮子,您可能会发现使用kivy的布局类更容易为您管理大部分内容。

相关问题