python - Kivy - 如何将 ImageButton 单击空间限制为仅在图像本身上?(而不是空白的“占用”空间)?
问题描述
我连续有四个 ImageButtons,全部由(似乎是)空格分隔。然而,这个空白也可以作为一个按钮(直到为图像分配的两个按钮的整个部分之间的中点)
'''
FloatLayout:
canvas:
Color:
rgb: utils.get_color_from_hex("#0a5a97")
Rectangle:
size: self.size
pos: self.pos
pos_hint: {"top":0.125,"right":1}
size_hint: 1,.125
ImageButton:
source: "Icons5/040-user.png"
pos_hint: {"top":0.95,"right":1}
size_hint: .3,.7
on_release:
print("Account")
ImageButton:
source: "Icons5/002-settings.png"
pos_hint: {"top":0.95,"right":.75}
size_hint: .3,.7
on_release:
print("Settings")
app.change_screen("settings_screen", direction='right', mode='push')
ImageButton:
source: "Icons5/015-idea.png"
pos_hint: {"top":0.95,"right":.5}
size_hint: .3,.7
on_release:
print("Info")
app.change_screen("settings_screen", direction='right', mode='push')
ImageButton:
source: "Icons5/003-home.png"
pos_hint: {"top":0.95,"right":.25}
size_hint: .3,.7
on_release:
print("Home")
app.change_screen("home_screen", direction='right', mode='push')
Label:
pos_hint: {"top":0.2,"right":1}
size_hint: .3, .15
font_color:
utils.get_color_from_hex("#425FFF")
font_size: 16
text: "Account"
markup: True
Label:
pos_hint: {"top":0.2,"right":.75}
size_hint: .3, .15
font_color:
utils.get_color_from_hex("#425FFF")
font_size: 16
text: "Settings"
markup: True
Label:
pos_hint: {"top":0.2,"right":.5}
size_hint: .3, .15
font_color:
utils.get_color_from_hex("#425FFF")
font_size: 16
text: "Info"
markup: True
Label:
pos_hint: {"top":0.2,"right":.25}
size_hint: .3, .15
font_color:
utils.get_color_from_hex("#425FFF")
font_size: 16
text: "Home"
markup: True
'''
我如何使它只有图像本身充当按钮,而不是包括它周围的空间?
解决方案
通过使用 size_hint,我设法将“可点击”区域限制为图像本身(可点击区域现在是方形 - 图像是圆形的,角落仍然是可点击的)。
我首先减小了图像的水平尺寸,直到最右边的图像基本上接触到窗口的一侧。然后这个值(对我来说是 0.08)表示我的特定图像的水平宽度。然后我为剩余的图像填充了 0.08,并将它们与我的标签对齐。
然而,这并不排除我的圆形图像的角落,因为它们仍然是可点击的。但就我的目的而言,这个简单的解决方案就足够了。
更新代码:
'''
FloatLayout:
canvas:
Color:
rgb: utils.get_color_from_hex("#0a5a97")
Rectangle:
size: self.size
pos: self.pos
pos_hint: {"top":0.125,"right":1}
size_hint: 1,.125
ImageButton:
source: "Icons5/040-user.png"
pos_hint: {"top":0.95,"right":.80}
size_hint: .07,.7
on_press:
print("Account")
ImageButton:
source: "Icons5/002-settings.png"
pos_hint: {"top":0.95,"right":.60}
size_hint: .07,.7
on_press:
print("Settings")
ImageButton:
source: "Icons5/015-idea.png"
pos_hint: {"top":0.95,"right":.40}
size_hint: .07,.7
on_press:
print("Info")
ImageButton:
source: "Icons5/003-home.png"
pos_hint: {"top":0.95,"right":.20}
size_hint: .07,.7
on_press:
print("Home")
app.change_screen("home_screen", direction='right', mode='push')
Label:
pos_hint: {"top":0.2,"right":.8}
size_hint: .07,.15
font_color:
utils.get_color_from_hex("#425FFF")
font_size: 16
text: "Account"
markup: True
Label:
pos_hint: {"top":0.2,"right":.6}
size_hint: .07,.15
font_color:
utils.get_color_from_hex("#425FFF")
font_size: 16
text: "Settings"
markup: True
Label:
pos_hint: {"top":0.2,"right":.4}
size_hint: .07,.15
font_color:
utils.get_color_from_hex("#425FFF")
font_size: 16
text: "Info"
markup: True
Label:
pos_hint: {"top":0.2,"right":.20}
size_hint: .07,.15
font_color:
utils.get_color_from_hex("#425FFF")
font_size: 16
text: "Home"
markup: True
'''
推荐阅读
- javascript - RecipeId 未定义
- spring-boot - 如何在apache生成的日志中隐藏敏感数据
- swift - 关闭 MainScreen 视图控制器
- visual-studio-code - VSCode:我想更改文本颜色(不是背景颜色,只是文字颜色),我应该编辑哪个配置文件?
- scala - 如何区分参数类型?
- c - 你可以给宏设置一个字符串吗
- react-native - 在 feathersjs 中上传文件时出现错误 500(一般错误)
- android - SYSTEM_UI_FLAG_LAYOUT_STABLE 标志在 Android R 中已弃用
- mysql - mysql - 当精度为 24 或 25 时,FLOAT 存储大小从 4 字节切换到 8 字节?
- django - 空的python请求正文