Python Kivy图像更新屏幕大小更改

Python Kivy图像更新屏幕大小更改,python,image,canvas,window,kivy,Python,Image,Canvas,Window,Kivy,所以我的问题是这个。我有一个很好的背景图像,当我重新缩放窗口时,一切都正常,图像像设计的那样移动,但在我的登录类中,我的“check icon.png”根本不显示。日志显示它已加载,但它不在任何窗口上。还可以将login class语句更改为: with self.canvas.before: self.image = Image(stuff) 而不是 with root.canvas.before: self.image = Image(stuff) (root改为self

所以我的问题是这个。我有一个很好的背景图像,当我重新缩放窗口时,一切都正常,图像像设计的那样移动,但在我的登录类中,我的“check icon.png”根本不显示。日志显示它已加载,但它不在任何窗口上。还可以将login class语句更改为:

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

您已经将大小更新函数绑定到self.root,但这是
login
的一个实例,它从未添加到小部件树中,也从未执行任何操作-特别是,它从未更改大小,因此更新从未发生

您应该简单地绑定到
self
,而不是
self.bind(pos=…)


此外,您应该让您的小部件名称以大写字母开头,因为这是一个值得遵循的好python约定,并且因为kv语言依赖于此来区分小部件和属性……而且您可能希望尽可能多地使用kv语言

图像是一个小部件,而不是画布指令。使用
self.Add_小部件(Image(…)
添加它。这会添加图像,但如果调整窗口大小,它仍然不会更改。现在,当我调整大小时,会出现此错误:“self.Image.pos=instance.pos AttributeError:'NoneType'对象没有属性'pos'”。请阅读底部的问题以查看修改后的代码。非常感谢。add_小部件返回None,因此返回错误。您应该绑定到小部件。e、 g.
self.image=image(…)
然后
self.add\u小部件(self.image)