简体   繁体   English

屏幕尺寸更改后的奇异图像更新

[英]Kivy image update on screen size change

So my problem is this. 所以我的问题是这个。 I have a background image that works great, when i rescale the window everything works out and the image moves like designed but in my login class my 'check-icon.png' does not show up at all. 我有一个效果很好的背景图片,当我重新调整窗口大小时,所有内容都可以正常工作,并且图片按设计方式移动,但是在我的登录类中,我的“ check-icon.png”根本没有显示。 The log says it was loaded but it is not on the window anywhere. 日志说它已经被加载了,但是它不在窗口的任何地方。 Also by changing the login class statement to say: 也可以通过更改登录类语句来说:

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

instead of 代替

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

(root changed to self) I can get the check-icon.png to appear but it still does not realign when the window size changes like the background image does at the bottom. (将根更改为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()

Also, sorry I posted this super long thing. 另外,对不起,我张贴了这条超长的东西。 I know I should only post the really relevant code but since I'm new at this I wanted to be sure the mistake isn't somewhere else in the code. 我知道我应该只发布真正相关的代码,但是由于我是新手,所以我想确保错误不在代码中其他地方。

the new code is this: 新的代码是这样的:

    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

You've bound your size update function to self.root, but this is an instance of login that isn't ever added to the widget tree and never does anything - in particular, it never changes size and so the update never happens. 您已经将大小更新功能绑定到self.root,但这是一个login实例,从未添加到小部件树中,并且从不执​​行任何操作-特别是,它从未更改大小,因此更新从未发生。

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

Also, you should make your widgets names starting with capital letters, both because it's a good python convention worth following and because kv language relies on this to distinguish widgets from properties...and you'll probably want to use kv language as much as possible! 另外,您应该使小部件名称以大写字母开头,这既是值得遵循的良好python约定,又是因为kv语言依赖于此来区分小部件和属性...而且您可能会想尽可能多地使用kv语言可能!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM