我有一个功能范围问题。我需要更新“cardTableLayout”'什么时候丢弃'调用evaluate函数后按下按钮。我该怎么做呢?我知道这是一个kv布局语言问题。我不确定如何参与&card; CardableLayout'来自内部' buttonClick_callback。'
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()
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 :(得分:0)
该按钮已添加到cardTableLayout
。在按下时,它执行一个名为buttonClick_callback
的函数,将自身作为第一个参数传递。
这样可以通过调用cardTableLayout
来引用buttonClick_callback
中的self.parent
。
答案 1 :(得分:0)
问题出在导入的good_deal库中。传入的值未正确更新。一旦纠正,画布正在更新。