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

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

KIVY: Button background not displayed despite source being specified

问题

我在Kivy中遇到了一个奇怪的问题,其中一个按钮的background_normalbackground_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_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:

	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

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:

确定