Kivy从调用布局的函数外部更新小部件图像

问题描述:

我有一个函数范围问题。在评估函数被调用后,当按下“放弃”按钮时,我需要更新'cardTableLayout'。我该怎么做呢?我知道这是一个kv布局语言问题。我不知道如何从'buttonClick_callback'中引用'cardTableLayout'。 Python代码 Kivy从调用布局的函数外部更新小部件图像

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() 

千伏文件

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 

按钮已被添加到cardTableLayout。在按下时,它会执行一个名为buttonClick_callback的函数,将它自己作为第一个参数传递。

这允许通过调用self.parent来参考buttonClick_callback中的cardTableLayout

+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中的self.parent.children]''? – jligeza

问题在于导入的good_deal库。传入的值未正确更新。一旦纠正,画布正确更新。