如何避免在Python/Kivy中调整my.png的大小时出现问题

2024-05-19 10:09:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Kivy是个新手,我试着给自己找一个模板,为以后的程序部分做准备。 现在我被卡住了,因为当我尝试调整按钮的大小时,我奇妙的round.png按钮都会出现“边/凸点”。你知道吗

按钮应该是这样的。。。 Img01

。。。这就是实际发生的情况(请忽略白色背景) Img02

我尝试了多个其他的.png,所以我非常确定问题不在.png本身。你知道吗

这是我的代码(我缩短了它,以便很容易找到相关的部分。如果您觉得需要更多的代码,我当然可以添加):

test.kv

#:kivy 1.0.9

<MainWindow>:
BoxLayout:
    #[shortened for readability reasons]
    BoxLayout:
        #[shortened for readability reasons]
    BoxLayout:
        #[shortened for readability reasons]
        AnchorLayout:
            #[shortened for readability reasons]
        FloatLayout:
            canvas:
                Rectangle:
                    pos: self.pos
                    size: self.size
            Button:
                id: leftButton
                background_normal: 'assets/graphics/buttons/dummy.png'
                background_down: 'assets/graphics/buttons/.png'
                size_hint: .15, .15
                pos_hint: {'x':root.size_hint_x/2, 'y':root.size_hint_y/2}

也许这里有人知道问题出在哪里,怎么解决? 请记住,我想在.kv文件中修复这个问题(如果可能的话),以便以后可以更轻松地重用它。你知道吗

另一个小问题是其他一些按钮的位置。我想让它们对齐到右边,但我找不到如何访问按钮本身的宽度,以便我可以从中减去它自身宽度(如果我做对了,应该是周围布局的宽度)

有什么建议吗?你知道吗


在此期间,我做了一些测试,以确保文件类型不是这里的问题。我尝试了.gif和.tif,据我所知,其他最常见的支持透明性的文件类型。 结果与.png相同


为了澄清设计的最终效果,我将添加另一张图片:

Img03

所以我想我必须用浮动布局来做。我想在pyqt中使用grid,但是在kivy中,不可能定义小部件应该放置的单元格,也不可能不给它一个colspan或rowspan。至少我读过。如果我错了,请纠正我。你知道吗


好吧,ikolim的解决方案到目前为止还有效,但是产生了另一个问题:我不能再使用background\u normal和background\u down了,但是我觉得我必须给用户一个视觉反馈,比如,点击按钮并按住它。我想因为我用了buttonbeavier我也可以用背景的东西。但事实似乎并非如此。有谁能给我一个解决问题的提示吗?你知道吗


更新:我现在得到了视觉反馈。解决方法很简单,直到上周五我才把它忽略掉(直到现在我还可以回到一台有互联网的电脑上)。 因此,对于有类似问题的读者,以下是我的解决方案: 我现在只需使用on d press和on d release侦听器,并从那里更改图像的来源我的图片.png到myPic_已单击.png反之亦然。你知道吗

所以问题解决了,我会写一个完整的答案,并在几天内结束这个问题。 但在此之前,我真的很想知道是否有人有想法,为什么我会有这样的坎坷。一方面是为了全面了解kivy,另一方面也是为了避免将来出现这个问题。你知道吗

提前谢谢!你知道吗


Tags: 代码posforsize宽度png按钮background
2条回答

好吧,在没有其他人知道最初的问题是什么之后,这里至少有一个很好的解决方案。你知道吗

ikolims的回答几乎是完全正确的,我想以此为荣。但它也遗漏了一个重要的部分,至少对我来说是这样。这是他的密码。之后,我将解释遗漏的内容:

from kivy.lang import Builder
from kivy.base import runTouchApp

runTouchApp(Builder.load_string('''
#:kivy 1.11.0

<CustomButton@ButtonBehavior+Image>:
GridLayout:
    rows: 1
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size

    CustomButton:
        id: Button0
        source: './dummy.png'
        size_hint: .15, .15
        pos_hint: {'x':root.size_hint_x/2, 'y':root.size_hint_y/2}
        on_release:
            print('CustomButton clicked')

    CustomButton:
        id: Button1
        source: './dummy.png'
        size_hint: .15, .15
        pos_hint: {'x':root.size_hint_x/2, 'y':root.size_hint_y/2}
        on_release:
            print('CustomButton clicked')

    CustomButton:
        id: Button2
        source: './dummy.png'
        size_hint: .15, .15
        pos_hint: {'x':root.size_hint_x/2, 'y':root.size_hint_y/2}
        on_release:
            print('CustomButton clicked')

    CustomButton:
        id: Button3
        source: './dummy.png'
        size_hint: .15, .15
        pos_hint: {'x':root.size_hint_x/2, 'y':root.size_hint_y/2}
        on_release:
            print('CustomButton clicked')
'''))

问题是,如果我们这样做的话,有突起的虫子就消失了。但也消失了是背景正常和背景下降不再工作。解决这个问题的方法是使用on\u pressed和on\u release代替,并更改其中的源代码。你知道吗

  • 创建一个继承ButtonBehavior和Image的自定义按钮
  • 使用GridLayout将自定义按钮放置在一行中

示例

你知道吗主.py你知道吗

from kivy.lang import Builder
from kivy.base import runTouchApp

runTouchApp(Builder.load_string('''
#:kivy 1.11.0

<CustomButton@ButtonBehavior+Image>:

GridLayout:
    rows: 1
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size

    CustomButton:
        id: Button0
        source: './dummy.png'
        size_hint: .15, .15
        pos_hint: {'x':root.size_hint_x/2, 'y':root.size_hint_y/2}
        on_release:
            print('CustomButton clicked')

    CustomButton:
        id: Button1
        source: './dummy.png'
        size_hint: .15, .15
        pos_hint: {'x':root.size_hint_x/2, 'y':root.size_hint_y/2}
        on_release:
            print('CustomButton clicked')

    CustomButton:
        id: Button2
        source: './dummy.png'
        size_hint: .15, .15
        pos_hint: {'x':root.size_hint_x/2, 'y':root.size_hint_y/2}
        on_release:
            print('CustomButton clicked')

    CustomButton:
        id: Button3
        source: './dummy.png'
        size_hint: .15, .15
        pos_hint: {'x':root.size_hint_x/2, 'y':root.size_hint_y/2}
        on_release:
            print('CustomButton clicked')
'''))

输出

Img01

相关问题 更多 >

    热门问题