2016-04-12 8 views
0

私はカスタムボタン(Label + Image)でScrollViewを作成したいと思っていますが、Buttonオブジェクトの適切な位置付けLabelとImageに問題があります。ScrollViewとCustomButton

コード:

class TestScreen(Screen): 
    def change(self, btn, pos): 
     print pos 
    def populate(self): 
     scroll = self.ids.myscroll 
     grid = self.ids.scrollgrid 
     btn = Button(size_hint_y=None, width='29sp', text='') 
     box = BoxLayout(size=btn.size, pos=btn.pos,orientation='horizontal') 
     image = Image(source='image.png', size_hint_x=None, width=74) 
     label = Label(size_hint_x=None, width=100, text='test') 

     box.add_widget(image) 
     box.add_widget(label) 

     btn.add_widget(box) 
     grid.add_widget(btn) 

     btn.bind(pos=partial(self.change)) 

と.kvファイル:

<TestScreen>: 
    Button: 
     text: 'populate' 
     size_hint: None, None 
     size: 100,100 
     pos: 0,0 
     on_press: root.populate() 
    ScrollView: 
     id: myscroll 
     size_hint: None, None 
     size: 300, 500 
     pos: 100, 100 
     scroll_x: 0.5 
     GridLayout: 
      spacing: 20 
      padding: 20 
      id: scrollgrid 
      size_hint: None, None 
      cols: 1 
      size_hint_y: None 
     children 
     height: self.minimum_height 
     width: self.parent.width 

最大の問題は、その移入にBoxLayoutを作成する時に()で、btn.posである[0,0]とレンダリング後btn.posが適切な座標に変更されました(posに対してbind()を使ってチェックしました)。上記のBoxLayoutを作成するときに、どのようにして正しいcooridnateを得ることができますか?

答えて

0

レイアウトではない別のウィジェットにウィジェットを追加すると、デフォルトのpos(0,0)とサイズ(100,100)が割り当てられます。グリッドレイアウトを使用して、イメージとボタンをまとめてパックします。例:

main.py:

#!/usr/bin/env python2 
# -*- coding: utf-8 -*- 
from kivy.app import App 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.screenmanager import Screen 
from kivy.properties import StringProperty, ObjectProperty 
from random import choice 


class ImageButton(GridLayout): 

    images = [ 
     'http://kivy.org/logos/kivy-logo-black-256.png', 
     'http://img.interia.pl/rozrywka/nimg/2/7/roz4286600.jpg', 
     'http://www.i-programmer.info/images/stories/News/2014/Apr/A/kivycont2.jpg', 
     'http://static.giantbomb.com/uploads/scale_small/9/90155/2472244-abathur.png', 
     'https://www.gravatar.com/avatar/a07de5a89d18964a22775deae84d9ba6?s=328&d=identicon&r=PG', 
     'https://www.gravatar.com/avatar/578e323e1c4dda99b24bf047e0cb2e3e?s=328&d=identicon&r=PG&f=1' 
    ] 

    source = StringProperty(images[0]) 

    def change_image(self): 
     while True: 
      new_img = choice(self.images) 
      if new_img != self.source: 
       self.source = new_img 
       break 


class MainScreen(Screen): 

    grid = ObjectProperty() 

    def populate(self): 
     for i in xrange(10): 
      self.grid.add_widget(ImageButton()) 


class Test(App): 

    def on_start(self): 
     self.root.populate() 


Test().run() 

test.kv:

MainScreen: 
    grid: grid 

    ScrollView: 

     GridLayout: 
      id: grid 
      cols: 1 
      spacing: '10dp' 
      padding: '10dp' 
      size_hint_y: None 
      height: self.minimum_height 


<ImageButton>: 
    cols: 2 
    size_hint_y: None 
    height: '200dp' 

    AsyncImage: 
     source: root.source 
     size_hint_x: 0.3 

    Button: 
     text: 'click me' 
     on_press: root.change_image() 
0

Buttonにウィジェットを追加することはとても良い解決策ではありません。ボタンは実際には振舞いが設定されたLabelであるため、正当な理由なしに複製するだけです。このようなウィジェット(イメージ付きのボタン)を作成する方法はhereで説明されていますが、Imageの位置を選択し、必要に応じてウィジェット全体を配置するだけで済みます。最初の例では、AppクラスもBoxLayoutのように動作します。したがって、それをレイアウトのみ、すなわちルートウィジェットとして想像してください。そこから簡単になるでしょう。

また、ウィジェットが初期化されると、デフォルト値はsizeposとなり、ウィジェット自体に固有の値が変更されます。基本的には、btn = Button()のウィジェットを作成しましたが、posまたはsizeを設定していないので、後でboxの位置にアクセスすると、デフォルトとなります。... sizeposの別のウィジェットデフォルト値を同じ値に置き換えただけです。正しい値を取得するには、最初にウィジェット内の希望の位置&のサイズに設定する必要があります。