屏幕尺寸变化的Kivy图像更新

时间:2014-07-14 03:59:41

标签: python image canvas window kivy

所以我的问题是这个。我有一个很好的背景图像,当我重新缩放窗口时,一切都工作,图像移动设计,但在我的登录类中,我的'check-icon.png'根本没有出现。日志说它已加载但它不在窗口的任何地方。另外通过更改登录类语句来说:

with self.canvas.before:
    self.image = Image(stuff)

而不是

with root.canvas.before:
    self.image = Image(stuff)

(root更改为self)我可以显示check-icon.png,但是当窗口大小改变时,它仍然不会重新对齐,就像底部的背景图像一样。

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
from kivy.graphics import Rectangle


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
            valid = '/Users/Kevin/Desktop/Python-extras/SSS Assistant/Manager_images/check-icon.png'

        #Put a check or x next to username based on if its in the system
        self.root = root = login()
        root.bind(size=self._update_image,pos=self._update_image)
        with root.canvas.before:
            self.image = Image(source=valid, pos=((self.width / 2)+130,(self.top / 2)), size=(25,25))


    def _update_image(self,instance,value):
        self.image.pos = instance.pos
        self.image.size = instance.size

class DataApp(App):
    def build(self):
        #login is the root Widget here
        self.root = root = login()
        root.bind(size=self._update_rect,pos=self._update_rect)
        with root.canvas.before:
            self.rect = Rectangle(source="/Users/Kevin/Desktop/Python-extras/SSS Assistant/Manager_images/background.jpg",size=root.size,pos=root.pos)
        return root

    def _update_rect(self,instance,value):
        self.rect.pos = instance.pos
        self.rect.size = instance.size



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

另外,抱歉,我发布了这个超长的东西。我知道我应该只发布真正相关的代码,但因为我是新手,所以我想确保错误不在代码的其他地方。

新代码是这样的:

    self.bind(size=self._update_image,pos=self._update_image)
    #Put a check or x next to username based on if its in the system
    self.image = self.add_widget(Image(source=valid, pos=((self.width / 2)+115,(self.top / 2)+50), size=(20,20)))



def _update_image(self,instance,value):
    self.image.pos = instance.pos
    self.image.size = instance.size

1 个答案:

答案 0 :(得分:0)

您已将大小更新功能绑定到self.root,但这是login的一个实例,它不会添加到小部件树中,也从不执行任何操作 - 特别是,它永远不会更改大小和所以更新永远不会发生。

您应该简单地绑定到selfself.bind(pos=...)

另外,你应该把你的小部件名称以大写字母开头,因为它是一个值得关注的好的python约定,因为kv语言依赖于它来区分小部件和属性......你可能想要使用kv语言尽可能多!