KIVY: 按钮背景尽管指定了源但未显示

huangapple go评论94阅读模式
英文:

KIVY: Button background not displayed despite source being specified

问题

我在Kivy中遇到了一个奇怪的问题,其中一个按钮的background_normalbackground_down值被设置为一个目录路径,即'images/icons/software_icon.jpg',然而当我启动程序时,图片没有显示出来,而是显示了Kivy按钮默认的基本灰色框。我猜测Kivy无法在我给定的目录中找到这个图片,但我已经确保图片在正确的目录中。这个方法在我的代码中所有其他按钮中都有效,所以我不确定发生了什么。这是我的代码:

  1. Button:
  2. id: power_button
  3. text: ''
  4. background_normal: 'images/icons/power_icon.jpg'
  5. background_down: 'images/icons/power_icon.jpg'
  6. center_x: 1520
  7. center_y: 120
  8. texture: self.texture
  9. height: 50
  10. width: 50
  11. opacity: 0
  12. disabled: True
  13. on_press:
  14. root.powerOff()
  15. Button:
  16. id: settings_button
  17. text: ''
  18. background_normal: 'images/icons/settings_icon.jpg'
  19. background_down: 'images/icons/settings_icon.jpg'
  20. center_x: 1520
  21. center_y: 190
  22. texture: self.texture
  23. height: 50
  24. width: 50
  25. opacity: 0
  26. disabled: True
  27. on_press:
  28. pass
  29. Button:
  30. id: os_button
  31. text: ''
  32. background_normal: 'images/icons/software_icon.jpg'
  33. background_down: 'images/icons/software_icon.jpg'
  34. center_x: 1520
  35. center_y: 260
  36. texture: self.texture
  37. height: 50
  38. width: 50
  39. opacity: 0
  40. disabled: True
  41. on_press:
  42. 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:

  1. Button:
  2. id: power_button
  3. text: ''
  4. background_normal: 'images/icons/power_icon.jpg'
  5. background_down: 'images/icons/power_icon.jpg'
  6. center_x: 1520
  7. center_y: 120
  8. texture: self.texture
  9. height: 50
  10. width: 50
  11. opacity: 0
  12. disabled: True
  13. on_press:
  14. root.powerOff()
  15. Button:
  16. id: settings_button
  17. text: ''
  18. background_normal: 'images/icons/settings_icon.jpg'
  19. background_down: 'images/icons/settings_icon.jpg'
  20. center_x: 1520
  21. center_y: 190
  22. texture: self.texture
  23. height: 50
  24. width: 50
  25. opacity: 0
  26. disabled: True
  27. on_press:
  28. pass
  29. Button:
  30. id: os_button
  31. text: ''
  32. background_normal: 'images/icons/software_icon.jpg'
  33. background_down: 'images/icons/software_icon.jpg'
  34. center_x: 1520
  35. center_y: 260
  36. texture: self.texture
  37. height: 50
  38. width: 50
  39. opacity: 0
  40. disabled: True
  41. on_press:
  42. pass

The output:
image of the issue

Any ideas as to what I should do now?

答案1

得分: 0

我找到了解决方案! 我仍然不清楚为什么这会产生任何差异,但我不抱怨。 无论如何,这是“.kv”文件中按钮的新更新代码:

  1. Button:
  2. id: os_button
  3. text: ''
  4. center_x: 1910
  5. center_y: 260
  6. texture: self.texture
  7. height: 50
  8. width: 50
  9. opacity: 0
  10. disabled: True
  11. on_press:
  12. pass
  13. Image:
  14. source: 'images/icons/soft_icon.jpg'
  15. size: os_button.size
  16. pos: os_button.pos

因此,只需添加Image属性而不是background_normalbackground_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:

  1. Button:
  2. id: os_button
  3. text: ''
  4. center_x: 1910
  5. center_y: 260
  6. texture: self.texture
  7. height: 50
  8. width: 50
  9. opacity: 0
  10. disabled: True
  11. on_press:
  12. pass
  13. Image:
  14. source: 'images/icons/soft_icon.jpg'
  15. size: os_button.size
  16. 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

huangapple
  • 本文由 发表于 2023年2月9日 01:35:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定