无法移动写入.py文件的窗口小部件位置

时间:2019-07-18 17:26:53

标签: python widget kivy

您好,感谢您的阅读。我无法在屏幕上移动小部件位置。实际上,我检查了小部件的位置,可以根据需要进行更改,但是在屏幕上看不到任何更改。我也在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()

感谢您已经回答...

1 个答案:

答案 0 :(得分:0)

  1. 实施自定义窗口小部件类,例如 # # # # # # # # 具有更新MyWidget()pos的方法
  2. size替换为Widget(...)
  3. 删除MyWidget(...),因为它是在新类with self.list2[len(self.list1)-1].canvas: ...中完成的

摘要

MyWidget()