大佬教程收集整理的这篇文章主要介绍了Kivy - 我不能用 id 更改 .source,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在一个盒子里改变我的照片,我试图在 ID 的帮助下做到这一点。我已经尝试了很多解决方案,并且已经搜索了 3 多个小时,但我无法得到任何正确的结果,因此我将不胜感激。我对 Kivy 也很陌生。
我尝试改变:
self.IDs.rec.source = 'download.jpg'
to(如python - Kivy: AttributeError: 'super' object has no attribute '__getattr__' when trying to get self.ids所建议的)
self.parent.parent.IDs
没用,只是把我的错误改成了:
AttributeError: 'nonetype' object has no attribute 'parent'
试图自己摆弄它,但除了将错误更改为另一个错误之外,也无能为力。
这是我的python文件:
from kivy.app import *
from kivy.uix.button import *
from kivy.graphics import *
from kivy.uix.Widget import *
from kivy.uix.label import *
from kivy.uix.floatlayout import *
from kivy.uix.boxlayout import *
from kivy.uix.relativelayout import *
from kivy.propertIEs import ListProperty,StringProperty,ObjectProperty
x = []
f = []
#---------------------------------------------------------------------------------
class Picture(floatLayout):
def __init__(self,**kwargs):
super(Picture,self).__init__(**kwargs)
def change(self,**kwargs):
y = len(X)
if y % 2 == 0:
self.IDs.rec.source = 'download.jpg'
tilesource = 'download.jpg'
print("aaaa")
else:
tilesource = 'images.jpg'
print("bbbbb")
#---------------------------------------------------------------------------------
class TileWidget(floatLayout):
def __init__(self,**kwargs):
super(TileWidget,self).__init__(**kwargs)
self.pos = (0,0)
self.size = (1,1)
d = Picture()
self.add_Widget(d)
def swap(self):
x.append("0")
Picture().change()
#---------------------------------------------------------------------------------
class langApp(App):
def build(self):
return TileWidget()
#---------------------------------------------------------------------------------
if __name__ == '__main__':
langApp().run()
and this is my .kv file:
#:kivy 1.11.1
<TileWidget>:
floatLayout:
pos: 0,0
size: root.wIDth,root.height
button:
text: 'Last'
pos_hint: {'x':.25,'y':.1}
size_hint: .1,.1
button:
text: 'Stop'
pos_hint: {'x':.45,.1
button:
on_press: root.swap()
text: 'Next'
pos_hint: {'x':.65,.1
<Picture>:
pos_hint: {'x':.25,'y':.4}
canvas:
Rectangle:
ID: rec
size: self.size[0]*0.5,self.size[1]*0.5
pos: self.pos[0],self.pos[1]
source: "images.jpg"
这是我的完整错误日志:
TraceBACk (most recent call last):
file "kivy\propertIEs.pyx",line 861,in kivy.propertIEs.ObservableDict.__getattr__
KeyError: 'rec'
During handling of the above exception,another exception occurred:
TraceBACk (most recent call last):
file "c:/Users/Leon/mobile/main.py",line 45,in <module>
langApp().run()
file "C:\Users\Leon\mobile\env\lib\site-packages\kivy\app.py",line 950,in run
runtouchApp()
file "C:\Users\Leon\mobile\env\lib\site-packages\kivy\base.py",line 582,in runtouchApp
EventLoop.mainloop()
file "C:\Users\Leon\mobile\env\lib\site-packages\kivy\base.py",line 347,in mainloop
self.IDle()
file "C:\Users\Leon\mobile\env\lib\site-packages\kivy\base.py",line 391,in IDle
self.dispatch_input()
file "C:\Users\Leon\mobile\env\lib\site-packages\kivy\base.py",line 342,in dispatch_input
post_dispatch_input(*pop(0))
file "C:\Users\Leon\mobile\env\lib\site-packages\kivy\base.py",@R_935_5450@8,in post_dispatch_input
Listener.dispatch('on_motion',etype,mE)
file "kivy\_event.pyx",line 709,in kivy._event.Eventdispatcher.dispatch
file "C:\Users\Leon\mobile\env\lib\site-packages\kivy\core\window\__init__.py",line 1412,in on_motion
self.dispatch('on_touch_down',line 1428,in on_touch_down
if w.dispatch('on_touch_down',touch):
file "kivy\_event.pyx",in kivy._event.Eventdispatcher.dispatch
file "C:\Users\Leon\mobile\env\lib\site-packages\kivy\uix\Widget.py",line 545,in on_touch_down
if child.dispatch('on_touch_down',in kivy._event.Eventdispatcher.dispatch
file "C:\Users\Leon\mobile\env\lib\site-packages\kivy\uix\behaviors\button.py",line 151,in on_touch_down
self.dispatch('on_press')
file "kivy\_event.pyx",line 705,in kivy._event.Eventdispatcher.dispatch
file "kivy\_event.pyx",line 1248,in kivy._event.EventObservers.dispatch
file "kivy\_event.pyx",line 1132,in kivy._event.EventObservers._dispatch
file "C:\Users\Leon\mobile\env\lib\site-packages\kivy\lang\builder.py",line 57,in custom_callBACk
exec(__kvlang__.co_value,IDmap)
file "c:\Users\Leon\mobile\lang.kv",line 15,in <module>
on_press: root.swap()
file "c:/Users/Leon/mobile/main.py",line 37,in swap
Picture().change()
file "c:/Users/Leon/mobile/main.py",line 19,in change
self.IDs.rec.source = 'download.jpg'
file "kivy\propertIEs.pyx",line 864,in kivy.propertIEs.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
您的代码有两个问题:
swap()
方法:
def swap(self):
x.append("0")
Picture().change()
正在创建 Picture
的新实例并在该新实例上调用 change()
。这对实际在您的 GUI 中的 Picture
实例没有影响。解决此问题的一种简单方法是保留对实际位于 GUI 中的 Picture
实例的引用,并在 swap()
方法中使用它:
class TileWidget(FloatLayout):
def __init__(self,**kwargs):
super(TileWidget,self).__init__(**kwargs)
self.pos = (0,0)
self.size = (1,1)
self.d = Picture()
self.add_widget(self.d)
def swap(self):
x.append("0")
self.d.change()
第二个问题是画布指令不能使用ids
,但可以使用groups
。因此,您可以将 Rectangle
指令放在一个单独的组中:
<Picture>:
pos_hint: {'x':.25,'y':.4}
canvas:
Rectangle:
group: 'rec' # place this instruction in its own group
size: self.size[0]*0.5,self.size[1]*0.5
pos: self.pos[0],self.pos[1]
source: "images.jpg"
然后您可以使用该 group
来修改 Rectangle
:
def change(self,**kwargs):
y = len(X)
if y % 2 == 0:
rec = self.canvas.get_group('rec')[0]
rec.source = 'download.jpg'
tilesource = 'download.jpg'
print("aaaa")
else:
tilesource = 'images.jpg'
print("bbbbb")
以上是大佬教程为你收集整理的Kivy - 我不能用 id 更改 .source全部内容,希望文章能够帮你解决Kivy - 我不能用 id 更改 .source所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。