英文:
KIVY: Button background not displayed despite source being specified
问题
我在Kivy中遇到了一个奇怪的问题,其中一个按钮的background_normal
和background_down
值被设置为一个目录路径,即'images/icons/software_icon.jpg'
,然而当我启动程序时,图片没有显示出来,而是显示了Kivy按钮默认的基本灰色框。我猜测Kivy无法在我给定的目录中找到这个图片,但我已经确保图片在正确的目录中。这个方法在我的代码中所有其他按钮中都有效,所以我不确定发生了什么。这是我的代码:
Button:
id: power_button
text: ''
background_normal: 'images/icons/power_icon.jpg'
background_down: 'images/icons/power_icon.jpg'
center_x: 1520
center_y: 120
texture: self.texture
height: 50
width: 50
opacity: 0
disabled: True
on_press:
root.powerOff()
Button:
id: settings_button
text: ''
background_normal: 'images/icons/settings_icon.jpg'
background_down: 'images/icons/settings_icon.jpg'
center_x: 1520
center_y: 190
texture: self.texture
height: 50
width: 50
opacity: 0
disabled: True
on_press:
pass
Button:
id: os_button
text: ''
background_normal: 'images/icons/software_icon.jpg'
background_down: 'images/icons/software_icon.jpg'
center_x: 1520
center_y: 260
texture: self.texture
height: 50
width: 50
opacity: 0
disabled: True
on_press:
pass
输出:
问题的图片
有什么建议吗?
英文:
I am having a strange issue in Kivy where a button's background_normal
and background_down
values are being set to a directory path which is 'images/icons/software_icon.jpg'
however when my program is started, the image is not displayed and I am given the basic grey box that Kivy buttons default to. My guess is that Kivy is not finding the image in the directory I've given it however I have ensured that the image is in the correct directory. This method has worked with all my other buttons in my code so I'm not sure what's going on. Here is my code:
Button:
id: power_button
text: ''
background_normal: 'images/icons/power_icon.jpg'
background_down: 'images/icons/power_icon.jpg'
center_x: 1520
center_y: 120
texture: self.texture
height: 50
width: 50
opacity: 0
disabled: True
on_press:
root.powerOff()
Button:
id: settings_button
text: ''
background_normal: 'images/icons/settings_icon.jpg'
background_down: 'images/icons/settings_icon.jpg'
center_x: 1520
center_y: 190
texture: self.texture
height: 50
width: 50
opacity: 0
disabled: True
on_press:
pass
Button:
id: os_button
text: ''
background_normal: 'images/icons/software_icon.jpg'
background_down: 'images/icons/software_icon.jpg'
center_x: 1520
center_y: 260
texture: self.texture
height: 50
width: 50
opacity: 0
disabled: True
on_press:
pass
The output:
image of the issue
Any ideas as to what I should do now?
答案1
得分: 0
我找到了解决方案! 我仍然不清楚为什么这会产生任何差异,但我不抱怨。 无论如何,这是“.kv
”文件中按钮的新更新代码:
Button:
id: os_button
text: ''
center_x: 1910
center_y: 260
texture: self.texture
height: 50
width: 50
opacity: 0
disabled: True
on_press:
pass
Image:
source: 'images/icons/soft_icon.jpg'
size: os_button.size
pos: os_button.pos
因此,只需添加Image
属性而不是background_normal
和background_down
属性,就可以清除此问题。 希望这对将来遇到相同问题的人有所帮助!
新结果:最终结果
英文:
I found the solution! I'm still unclear as to why this made any difference but I am not complaining. Anyways here is the newly updated code for the button in the .kv
file:
Button:
id: os_button
text: ''
center_x: 1910
center_y: 260
texture: self.texture
height: 50
width: 50
opacity: 0
disabled: True
on_press:
pass
Image:
source: 'images/icons/soft_icon.jpg'
size: os_button.size
pos: os_button.pos
So just adding the Image
attribute instead of the background_normal
and background_down
attributes cleared up the issue. Hopefully this helps anyone having the same issue in the future!
New result: FINAL RESULT
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论