python - 屏幕尺寸变化时 Kivy 图像更新

标签 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

最佳答案

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

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

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

关于python - 屏幕尺寸变化时 Kivy 图像更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24729332/

相关文章:

android - React native,无法解析静态图像的模块

ios - 通过单击第一个按钮 IOS7 更改第二个按钮图像

javascript - 从 charts.js 中删除工具提示

python - tkinter( python ): assign class method to a key

Python/Django 测试运行程序导入错误

Python:BaseHTTPRequestHandler 实例在 GET 请求后重置

python - 如何遍历lxml对象的节点来查看属性是否存在?

php - 如何从需要在 PHP 页面上以 root 身份运行的 Python 脚本返回输出?

c - ppm 文件的图像卷积

html - SVG 与 HTML5 Canvas 中的图表