【问题标题】:Kivy updating a widget image from outside the function that calls the layoutKivy 从调用布局的函数外部更新小部件图像
【发布时间】:2016-12-06 01:02:47
【问题描述】:

我有一个函数范围问题。在调用评估函数后,按下“放弃”按钮时,我需要更新“cardTableLayout”。我该怎么做呢?我知道这是一个 kv 布局语言问题。我不确定如何从“buttonClick_callback”中引用“cardTableLayout”。 蟒蛇代码

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

【问题讨论】:

    标签: python image layout scope kivy


    【解决方案1】:

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

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

    【讨论】:

    • 我知道 self.parent 应该允许我访问父函数中的小部件,但是,我仍然无法通过 kv 文件中的 ids 或 name 键访问小部件树。 print(self.parent.ids) 返回“[]”。此外, self.parent.ask_update() 什么也不做。 self.parent.do_layout() 也没有。我需要做的是更新布局中每个小部件的画布。
    • @SteveHostetler 当您在buttonClick_callback 中执行print(self.parent)print(self) 时,控制台中会写什么?结果正确吗?如果是,您能否遍历子列表并显示他们的名字,例如names = [c.name for c in self.parent.children]
    【解决方案2】:

    问题出在导入的 good_deal 库中。传入的值没有正确更新。纠正后,画布正在正确更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多