Python 把图像放在一个按钮的中心-Kivy
再次卡住并寻求帮助 这一次,我试图将一个按钮上的图像与下面的文本放在一起 以下是我迄今为止的代码片段Python 把图像放在一个按钮的中心-Kivy,python,kivy,Python,Kivy,再次卡住并寻求帮助 这一次,我试图将一个按钮上的图像与下面的文本放在一起 以下是我迄今为止的代码片段 Button: canvas: Rectangle: # set rects size, pos = size, pos of the button size:50,50 pos:self.
Button:
canvas:
Rectangle:
# set rects size, pos = size, pos of the button
size:50,50
pos:self.pos
source:'icons/home.png'
text:'NCERT\nSolutions'
background_normal: ''
background_color: rgba("#FFFFE0")
color:0,0,0,1
halign:'center'
on_release:
app.root.current='flamingowindow'
这就得到了下面的结果
<强> > <强>:图标应该在按钮的中间,下面是文本。
这是一个真正的黑客尝试去做你想做的事情。使用一点算法就可以很容易地将图标放在您想要的位置,但定位文本需要反复试验: #:set image_height 50
#:set text_height 30
Button:
canvas:
Rectangle:
# sets size, pos of the image
size: image_height, image_height
pos: self.pos[0] + (self.width - image_height)/2, self.pos[1] + self.height - image_height
source: 'icons/home.png'
font_size: text_height / 2.5
text: "\\n\\n\\nNCERT\\nSolutions"
halign: 'center'
size_hint_y: None
height: image_height + text_height
试错包括
文本高度
,计算字体大小
,以及在文本
开头添加多少新行,我认为这是一个更好的解决方案。此代码创建一个名为ImageLabel
的自定义小部件
,该小部件由一个图像
组成,图像下方有一个标签
。整个ImageLabel
充当按钮
from kivy.lang import Builder
from kivy.properties import ListProperty, StringProperty
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.boxlayout import BoxLayout
class ImageLabel(ButtonBehavior, BoxLayout):
image_source = StringProperty('')
image_size = ListProperty([50, 50])
text = StringProperty('')
# stuff used by ButtonBehavior
background_color = ListProperty([1, 1, 1, 1])
background_normal = StringProperty(
'atlas://data/images/defaulttheme/button')
background_down = StringProperty(
'atlas://data/images/defaulttheme/button_pressed')
background_disabled_normal = StringProperty(
'atlas://data/images/defaulttheme/button_disabled')
background_disabled_down = StringProperty(
'atlas://data/images/defaulttheme/button_disabled_pressed')
border = ListProperty([16, 16, 16, 16])
Builder.load_string('''
<ImageLabel>:
orientation: 'vertical'
size_hint: None, None
height: self.image_size[1] + label.texture_size[1]
width: max(self.image_size[0], label.texture_size[0])
state_image: self.background_normal if self.state == 'normal' else self.background_down
disabled_image: self.background_disabled_normal if self.state == 'normal' else self.background_disabled_down
canvas:
Color:
rgba: self.background_color
BorderImage:
border: self.border
pos: self.pos
size: self.size
source: self.disabled_image if self.disabled else self.state_image
Image:
id: image
source: root.image_source
size: root.image_size
Label:
id: label
halign: 'center'
text: root.text
size: self.texture_size
''')
if __name__ == '__main__':
from kivy.app import App
gl = Builder.load_string('''
FloatLayout:
ImageLabel:
pos_hint: {'center_x':0.5, 'center_y': 0.5}
text: 'NCERT\\nSolutions'
image_source: 'atlas://data/images/defaulttheme/filechooser_folder'
image_size: 100, 100
on_release: app.button_callback()
''')
class ImageLabelTestApp(App):
def build(self):
return gl
def button_callback(self):
print('button pressed')
ImageLabelTestApp().run()
来自kivy.lang导入生成器
从kivy.properties导入ListProperty、StringProperty
从kivy.uix.behaviors导入按钮行为
从kivy.uix.boxlayout导入boxlayout
类图像标签(按钮行为,框布局):
image\u source=StringProperty(“”)
图像大小=ListProperty([50,50])
text=StringProperty(“”)
#纽扣行为人使用的东西
背景颜色=ListProperty([1,1,1,1])
背景_normal=StringProperty(
'atlas://data/images/defaulttheme/button')
背景\u down=StringProperty(
'atlas://data/images/defaulttheme/button_pressed')
背景\禁用\正常=StringProperty(
'atlas://data/images/defaulttheme/button_disabled')
背景\u禁用\u向下=StringProperty(
'atlas://data/images/defaulttheme/button_disabled_pressed')
border=ListProperty([16,16,16,16])
Builder.load_字符串(“”)
:
方向:“垂直”
大小提示:无,无
高度:self.image\u size[1]+label.texture\u size[1]
宽度:最大值(self.image\u size[0],label.texture\u size[0])
状态\图像:self.background\如果self.state=='normal'否则self.background\关闭,则self.background\正常
禁用\u图像:self.background\u禁用\u正常如果self.state=='normal'否则self.background\u禁用\u关闭
画布:
颜色:
rgba:self.background\u颜色
边框图像:
边界:self.border
pos:self.pos
大小:self.size
来源:self.disabled\u image if self.disabled else self.state\u image
图片:
id:图像
来源:root.image\u来源
大小:root.image\u大小
标签:
id:标签
哈利恩:“中心”
text:root.text
大小:self.texture\u大小
''')
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
从kivy.app导入应用程序
gl=Builder.load\u字符串(“”)
浮动布局:
图像标签:
位置提示:{'center_x':0.5,'center_y':0.5}
文本:“NCERT\\n解决方案”
图片来源:'atlas://data/images/defaulttheme/filechooser_folder'
图像大小:100100
发布时:app.button\u callback()
''')
类ImageLabelTestApp(应用程序):
def生成(自):
返回总帐
def按钮_回调(自):
打印('按钮按下')
ImageLabelTestApp().run()
Short-precision-Spot在这方面做得非常好。但是,尽管如此,我尝试了所有的解决方案,所有的都非常好。谢谢你们的支持