您好,感谢您的阅读。我无法在屏幕上移动小部件位置。实际上,我检查了小部件的位置,可以根据需要进行更改,但是在屏幕上看不到任何更改。我也在for循环self.remove_widget(i)
和self.add_widget(i)
中尝试过。但是也没有任何改变。
.py文件:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.uix.widget import Widget
from kivy.graphics import Color,Rectangle
from random import randint
class Page(Screen):
def on_enter(self, *args):
self.list1 = []
self.list2 = []
self.currentball = 0
def add(self):
self.currentball += 1
self.list1.append('Ball[{}]'.format(self.currentball))
self.list2.append(self.list1[len(self.list1)-1])
print('List1 => ',self.list1)
print('List2 => ',self.list2)
#Widget oluşturalım:
self.list2[len(self.list1)-1] = Widget(size_hint=(.1,.1),pos=(randint(100,200),randint(250,500)))
with self.list2[len(self.list1)-1].canvas:
Color(1,1,1,1)
Rectangle(source='worker.png',size=self.list2[len(self.list1)-1].size,pos=self.list2[len(self.list1)-1].pos)
self.add_widget(self.list2[len(self.list1)-1])
def move(self):
for i in self.list2:
i.pos[0] += randint(0,20)
print(i.pos)
def remove(self):
self.update()
class ScMn(ScreenManager):
pass
class test2(App):
def build(self):
return ScMn()
if __name__ == '__main__':
test2().run()
.kv文件:
<ScMn>:
Page:
name: 'page'
id: pageid
<Page>:
RelativeLayout:
Button:
size_hint: .1,.1
pos: 0,0
text: 'Add'
on_press: root.add()
Button:
size_hint: .1,.1
pos_hint:{'x':.1,'y':0}
text: 'Move'
on_press: root.move()
Button:
size_hint: .1,.1
pos_hint:{'x':.2,'y':0}
text: 'RemoveLast'
on_press: root.remove()
感谢您已经回答...
答案 0 :(得分:0)
#
# #
# #
# #
#
具有更新MyWidget()
和pos
的方法size
替换为Widget(...)
MyWidget(...)
,因为它是在新类with self.list2[len(self.list1)-1].canvas: ...
中完成的MyWidget()