2016-08-01 10 views
0

私は関数スコープの問題があります。 evaluate関数が呼び出された後、 'discard'ボタンが押されたときに 'cardTableLayout'を更新する必要があります。これはどうすればいいですか?私はこれがkvレイアウト言語の問題であることを知っています。私は、 'buttonClick_callback'の内側から 'cardTableLayout'をどのように参照するかわかりません。 のpythonコード レイアウトを呼び出す関数の外からウィジェット画像を更新する

import sys 
from os import path, listdir 
from random import choice 
sys.path.append('libs') 
from good_deal import * 
from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.floatlayout import FloatLayout 
from kivy.core.audio import SoundLoader 
from kivy.uix.button import Button 
from kivy.uix.image import Image 
from kivy.uix.behaviors import ToggleButtonBehavior 
from kivy.clock import mainthread 
randomSong = choice(listdir('music')) 
backgroundMusic = SoundLoader.load(path.join('music', randomSong)) 
backgroundMusic.play() 
backgroundMusic.loop = True 
cardImagesGlobal = [] 
hand, deck = deal() 
class ScreenManagement(ScreenManager): pass 
class Card(ToggleButtonBehavior, Image): pass 
class GameButton(Button): pass 
def buttonClick_callback(self): 
    buttonClickSound = SoundLoader.load(path.join('sounds', 'buttonClick.ogg')) 
    buttonClickSound.play() 
    index = 0 
    for items in cardImagesGlobal: 
     if items.state == 'down': 
      hand.marked.append(index) 
      index += 1 
    discard(hand, deck) 
    evaluate(hand) 
class CardTableScreen(Screen): 
    @mainthread 
    def on_enter(self): 
     global cardImagesGlobal 
     cardImages = [] 
     self.ids['handType'].text = hand.type 
     index = 0 
     for items in hand.ordered: 
      cardImages.append(Card(source = hand.filenames[index])) 
      self.ids['handLayout'].add_widget(cardImages[index]) 
      index += 1 
     cardImagesGlobal = cardImages 
     discardButton = GameButton(text = 'DISCARD') 
     discardButton.bind(on_press = buttonClick_callback) 
     self.ids['cardTableLayout'].add_widget(discardButton) 
layoutFile = Builder.load_file('main.kv') 
class Main(App): 
    def build(self): 
     self.title = 'Good Deal Poker' 
     return layoutFile 
if __name__ == "__main__": 
    Main().run() 

KVファイルは

ScreenManagement: 
    CardTableScreen: 
<Card>: 
    size_hint: (.95, .95) 
<GameButton>: 
    size_hint: (.20, .10) 
<CardTableScreen>: 
    name: 'cardTableScreen' 
    FloatLayout: 
     name: 'cardTableLayout' 
     id: cardTableLayout 
     canvas.before: 
      Color: 
       rgba: 0,.25,0,1 
      Rectangle: 
       pos: self.pos 
       size: self.size 
     Label: 
      name: 'handType' 
      id: handType 
      font_size: '20sp' 
      pos_hint: {'center_x':.5, 'center_y':.95} 
     BoxLayout: 
      size_hint: (1, .30) 
      pos_hint: {'center_x':.5, 'center_y':.75} 
      name: 'handLayout' 
      id: handLayout 
      orientation: 'horizontal' 
      canvas.before: 
       Color: 
        rgba: 0,.25,0,1 
       Rectangle: 
        pos: self.pos 
        size: self.size 

答えて

0

ボタンがcardTableLayoutに追加されました。押すと、buttonClick_callbackという関数が実行され、最初のパラメータとして渡されます。

これにより、buttonClick_callbackの中のcardTableLayoutself.parentと呼ぶことができます。

+0

私はself.parentが親関数のウィジェットにアクセスできるはずですが、kvファイルのIDまたは名前キーを使ってウィジェットツリーにアクセスすることはできません。 print(self.parent.ids)は '[]'を返します。さらに、self.parent.ask_update()は何もしません。どちらもself.parent.do_layout()を実行しません。私がする必要があるのは、レイアウト内の各ウィジェットのキャンバスを更新することです。 –

+0

@SteveHostetler 'buttonClick_callback'で' print(self.parent) 'または' print(self) 'を実行すると、コンソールには何が書かれていますか?結果は正しいですか?そうであれば、子リストを繰り返し処理し、 'names = [c.name of c for self.parent.children]'のような名前を表示できますか? – jligeza

0

問題は、インポートされたgood_dealライブラリにありました。渡された値が正しく更新されていませんでした。一度修正されると、キャンバスは正しく更新されます。

関連する問題