【问题标题】:Kivy image update on screen size change屏幕尺寸变化的 Kivy 图像更新
【发布时间】:2014-09-03 22:40:01
【问题描述】:

所以我的问题是这样的。我有一个效果很好的背景图像,当我重新缩放窗口时,一切正常,图像按照设计的方式移动,但在我的登录类中,我的“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

【问题讨论】:

  • 图像是一个小部件,而不是画布指令。用self.add_widget(Image(...))添加它。
  • 这会添加图像,但如果调整窗口大小,它仍然不会改变。

标签: python image canvas window kivy


【解决方案1】:

您已将大小更新函数绑定到 self.root,但这是 login 的一个实例,它从未添加到小部件树中并且从不做任何事情 - 特别是,它从不改变大小,因此更新永远不会发生。

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

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

【讨论】:

  • 现在我在调整大小时收到此错误:“self.image.pos = instance.pos AttributeError: 'NoneType' object has no attribute 'pos'”阅读底部的问题以查看修改后的代码。非常感谢。
  • add_widget 返回 None,因此出现错误。您应该绑定到小部件。例如self.image = Image(...) 然后 self.add_widget(self.image).
猜你喜欢
  • 2011-06-15
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
相关资源
最近更新 更多