回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我的项目是纸牌游戏。每张卡片也是一个按钮。当你点击游戏中的一张牌时,弹出窗口会显示一张牌的图像、一个可滚动的牌文本标签和各种按钮。你知道吗</p>
<p><img src="https://i.imgur.com/bUEtKXE.jpg" alt="PopUp"/></p>
<p>我目前的问题是试图改变弹出窗口中的图像和文本,以匹配实际的卡按钮信息。我目前有能力改变显示的信息,但只是作为它的最新定义。据我所知,这是因为我重新定义了class属性,而不是class属性的实例。你知道吗</p>
<p>我尝试过在Card(Button Image)文件中执行def_uuuinit_uuuu(self,parameters):但是,每当我尝试执行PlayMatWidget的draw方法时,它都会崩溃。你知道吗</p>
<p>我认为这可能与Kivy对象属性有关,但我不完全确定如何准确地使用它们(主要是因为按钮不是在运行时创建的)</p>
<p>谢谢你的阅读!你知道吗</p>
<p>Python文件:</p>
<pre><code>from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
from kivy.lang import Builder
import random
Builder.load_string('''
<ScrollableLabel>:
Label:
size_hint_y: None
height: self.texture_size[1]
text_size: self.width, None
text: root.text
''')
class ScrollableLabel(ScrollView):
text = StringProperty('')
# Visual Representation of a Card or Deck
class ImageButton(ButtonBehavior, Image):
pass
class CardBackend:
def __init__(self, name, card_art, effect_one):
self.name = name
self.card_art = card_art
self.effect = effect_one
class Card(ImageButton):
# Defining the Popup Window
main_pop = Popup()
main_pop.title = 'Inspect Card'
main_pop.title_align = 'center'
main_pop.size_hint = None, None
main_pop.size = 400, 400
main_pop.auto_dismiss = False
main_pop.opacity = 0.8
# Variables or Variables to Be
card_text = 'ABCDEFGHIJK1234567890' * 100
card_art = Image(source='test.png', size_hint=(.9, .85), pos_hint={'x': -0.18, 'y': .125})
# Primary Child Widget
main_box = FloatLayout()
# Buttons
play_button = Button(text='Play', size_hint=(0.32, 0.1), pos_hint={'x': 0.01, 'y': 0.0})
discard_button = Button(text='Discard', size_hint=(0.32, 0.1), pos_hint={'x': 0.34, 'y': 0.0})
close_button = Button(text='Close', size_hint=(0.32, 0.1), pos_hint={'x': 0.67, 'y': 0.0})
close_button.bind(on_press=main_pop.dismiss)
# Scrolling Card Text Viewer
card_info = ScrollableLabel(text=card_text, size_hint=(0.45, .8), pos_hint={'x': .55, 'y': .15})
# Building main_pop.content
main_box.add_widget(play_button)
main_box.add_widget(discard_button)
main_box.add_widget(close_button)
main_box.add_widget(card_info)
main_box.add_widget(card_art)
main_pop.content = main_box
class PlayMatWidget(FloatLayout):
def draw(self):
# Create Backend
# Draw Backend
# Create Card() using Backend
# Defining Card Data (Removed for imported Decks)
backend_bewd = CardBackend('Blue Eyes White Dragon', 'bewd.png', 'One')
backend_dragon = CardBackend('Dragon Egg', 'test.png', 'Two')
backend_lava = CardBackend('Lava Golem', 'lavagolem.png', 'Three')
# Creating Card Button Objects
card_zero = Card()
card_one = Card()
card_two = Card()
# Defining Card Title
card_zero.main_pop.title = backend_lava.name
card_one.main_pop.title = backend_dragon.name
card_two.main_pop.title = backend_bewd.name
# Defining Card Text for Scrollview
card_zero.card_info.text = backend_bewd.effect
# Defining Card Art
card_zero.source = backend_bewd.card_art
card_one.source = backend_dragon.card_art
card_two.source = backend_lava.card_art
deck_dictionary = {
0: card_zero,
1: card_one,
2: card_two,
}
# Prototype Shuffle for top card
# drawing = random.choice(deck_list)
# Creating a Physical Card Object
# drawn_card = Card()
# OLD
# Applying Card Data from backend to front end Card Object
# drawn_card.main_pop.title = drawing.name
# drawn_card.source = drawing.card_art
# drawn_card.card_art.source = drawing.card_art
# drawn_card.card_info.text = drawing.effect
# Used to check somethings
for x in range(0, 3):
print(deck_dictionary[x].source)
print(deck_dictionary[x].main_pop.title)
self.ids.PlayerField.add_widget(random.choice(deck_dictionary), index=int(len(self.ids.PlayerField.children)/2))
class CardGameApp(App):
def build(self):
return PlayMatWidget()
if __name__ == "__main__":
CardGameApp().run()
</code></pre>
<p>Kivy文件:</p>
<pre><code><Card@ImageButton>:
size_hint: 0.8,0.8
pos_hint: {'x':0, 'y':0.1}
on_press: print('confirming press')
on_press: root.main_pop.open()
<PlayMatWidget>:
id: PlayMat
canvas:
Rectangle:
size: self.size
pos: self.pos
source: 'tron1.png'
StackLayout:
id: OppHand
size_hint: 0.6, 0.1
pos_hint: {'x':0.2,'y': 0.9}
canvas:
Rectangle:
size: self.size
pos: self.pos
source: 'background.png'
StackLayout:
id: OppField
size_hint: 0.6, 0.4
pos_hint: {'x':0.2, 'y':0.5}
canvas:
Rectangle:
size: self.size
pos: self.pos
source: 'background.png'
StackLayout:
id: cards
size_hint: 0.6,0.4
pos_hint: {'x':0.2, 'y':0.1}
canvas:
Rectangle:
size: self.size
pos: self.pos
source: 'background.png'
AnchorLayout:
id: PlayerFieldAnchor
anchor_x: 'center'
anchor_y: 'center'
size_hint: 0.6,0.4
pos_hint: {'x':0.2, 'y':0.1}
canvas.before:
Rectangle:
size: self.size
pos: self.pos
source: 'background.png'
BoxLayout:
id: PlayerField
orientation: 'horizontal'
StackLayout:
id: PlayerHand
orientation: 'lr-tb'
size_hint: 0.6,0.1
pos_hint: {'x': 0.2, 'y':0}
canvas:
Rectangle:
size: self.size
pos: self.pos
source: 'background.png'
ImageButton:
id: PlayerDeck
source: 'cardback.png'
size_hint: 0.15, 0.15
pos_hint: {'x':0.025,'y':0.05}
on_press: root.draw()
</code></pre>