簡體   English   中英

在kivy中,為什么size_hint的圖像行為不同於按鈕的行為?

[英]In kivy, why does size_hint behave differently for images than for buttons?

我正在使用Kivy 1.9.1構建紙牌游戲。 我已經通過size_hint相對於根窗口的大小正確顯示了卡片。 卡類從圖像類繼承而來,並且效果很好。 我意識到我需要使卡片可點擊,因此我修改了該類以從按鈕類繼承。 由於某種原因,它的大小與圖像的大小不同。 背景.png文件變形。 請幫忙。 這讓我發瘋。 我通常會關閉size_hint以避免發生此問題,但是我需要根據根窗口大小對所有內容進行縮放。

ScreenManagement:
    CardTableScreen:
<Card>:
    size_hint: (.25, .25)
    pos_hint: ({'left': .05})
<CardTableScreen>:
    name: 'cardTable'

    Card:
        name: 'card0'
        id: card0
        pos: (self.width *.20 , root.height / 2)
    Card:
        name: 'card1'
        id: card1
        pos: (self.width * .75, root.height / 2)
    Card:
        name: 'card2'
        id: card2
        pos: (self.width * 1.30 , root.height / 2)
    Card:
        name: 'card3'
        id: card3
        pos: (self.width * 1.85, root.height / 2)
    Card:
        name: 'card4'
        id: card4
        pos: (self.width * 2.40, root.height / 2)
    Label:
        name: 'handType'
        id: handType
        pos: (-(card0.width *.125), root.height * .30)
        font_size: '18sp'

<Layout>:
    orientation: 'vertical'
    canvas.before:
        Color:
            rgba: 0,.25,0,1
        Rectangle:
            pos: self.pos
            size: self.size

蟒蛇:

from kivy.uix.button import Button
class(Button): pass

事實證明,解決方案是從圖像類和按鈕行為類繼承,如下所示:

從kivy.uix.behaviors導入ButtonBehavior
從kivy.uix.image導入圖像

類Cards(ButtonBehavior,Image):通過

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM