【发布时间】: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