Python Kivy将图像放置在动态生成的按钮的某个点上

Python Kivy将图像放置在动态生成的按钮的某个点上,python,python-2.7,kivy,kivy-language,Python,Python 2.7,Kivy,Kivy Language,我想在创建的每个按钮的左侧放置圆形图像,就像在消息传递应用程序上一样。图像将通过服务器发送。我已经有代码使用PIL使图像成为一个圆圈以及。我只是不知道如何把它们放在每个按钮的角落 应用程序的Python文件: from kivy.app import App from kivy.uix.screenmanager import ScreenManager, Screen from kivy.properties import ObjectProperty from kivy.uix.boxlay

我想在创建的每个按钮的左侧放置圆形图像,就像在消息传递应用程序上一样。图像将通过服务器发送。我已经有代码使用PIL使图像成为一个圆圈以及。我只是不知道如何把它们放在每个按钮的角落

应用程序的Python文件:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.clock import mainthread
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.properties import BooleanProperty, StringProperty
from kivy.uix.image import AsyncImage


Builder.load_file('main.kv')

login_plz = Popup(title='Please login',
              content=Label(text='You need to login first'),
              size_hint=(None, None), size=(400, 400),
              auto_dismiss=True)


class Menu(BoxLayout):
    access_denied = BooleanProperty(True)


class ScreenLogIn(Screen):

    @mainthread
    def verify_credentials(self):
        popup = Popup(title='Try again',
                  content=Label(text='Wrong Email/Password'),
                  size_hint=(None, None), size=(400, 400),
                  auto_dismiss=True)

        try:
            if len(self.ids.login.text) > 20 or   len(self.ids.passw.text) > 32:
                popup.open()
            elif self.ids.login.text == "a" and self.ids.passw.text == "a":
                App.get_running_app().root.access_denied = False
                self.manager.current = "match"
            else:
                App.get_running_app().root.access_denied = True
                popup.open()
        except Exception as e:
            pass


class ScreenNearUsers(Screen):

    @mainthread
    def on_enter(self):
        if App.get_running_app().root.access_denied is True:
            self.manager.current = 'login'
            login_plz.open()
        else:
            for i in xrange(101):
                button = Button(text="B_" + str(i))
                self.ids.grid.add_widget(button)


class ScreenMatch(Screen):

    def on_enter(self, *args):
        if App.get_running_app().root.access_denied is True:
            self.manager.current = 'login'
            login_plz.open()
        else:
            for i in range(10):
                src = "http://placehold.it/480x270.png&text=slide-%d&.png" % i
                image = AsyncImage(source=src, allow_stretch=True, keep_ratio=False,opacity=1,size_hint=(1, 1.3),
                               pos_hint={'center_x': 0.5, 'center_y': 0.75}, border=True)
                self.ids.carousel.add_widget(image)


class ScreenChats(Screen):

    def on_enter(self, *args):
        if App.get_running_app().root.access_denied is True:
            self.manager.current = 'login'
            login_plz.open()
        else:
            for i in range(50):
                button = Button(text='Some_name')
                self.ids.chat_layout.add_widget(button)


class ScreenUserProfile(Screen):

    def on_enter(self, *args):
        if App.get_running_app().root.access_denied is True:
            self.manager.current = 'login'
            login_plz.open()
        else:
            pass


class Manager(ScreenManager):
    screen_log_in = ObjectProperty(None)
    screen_near_user = ObjectProperty(None)
    screen_match = ObjectProperty(None)
    screen_chats = ObjectProperty(None)
    screen_user_profile = ObjectProperty(None)


class MenuApp(App):

    def build(self):
        return Menu()

    def on_start(self):
        self.current_screen = 'login'


if __name__ == '__main__':
    MenuApp().run()
这是主Kv:

<Menu>:
    manager: screen_manager
    orientation: "vertical"
    id: action

    ActionBar:
        size_hint_y: 0.1
        background_color: 0, 0, 1000, 10
        background_normal: ""
        ActionView:
            ActionPrevious:
                app_icon:""
                with_previous: False

                text:"    [b]Dhobiwala.com[/b]"

                markup:True
                font_size:"16dp"
            ActionButton:
                id: near_users
                size_hint: 1, 1
                height: self.texture_size[ 1 ]
                width: self.texture_size[ 0 ] + 40
                minimum_width: self.texture_size[ 0 ] + 60

               halign: "center"
               icon: 'icons/internet.png'
               disabled: True if root.access_denied else False
               on_press: root.manager.current = 'near_users'
           ActionButton:
               id: matching_bar
               size_hint: 1, 1

               height: self.texture_size[ 1 ]
               width: self.texture_size[ 0 ] + 40
               minimum_width: -self.texture_size[ 0 ] + 60
               halign: "center"
               text: "Matching"
               disabled: True if root.access_denied else False
               on_press: root.manager.current= 'match'
           ActionButton:
               id: chat
               size_hint: 1, 1

               height: self.texture_size[ 1 ]
               width: self.texture_size[ 0 ] + 40
               minimum_width: self.texture_size[ 0 ] + 60

               halign: "center"
               text: "chat"
               disabled: True if root.access_denied else False
               on_press: root.manager.current = 'chats'
           ActionButton:
               id: profile
               size_hint: 1, 1

               height: self.texture_size[ 1 ]
               width: self.texture_size[ 0 ] + 40
               minimum_width: self.texture_size[ 0 ] + 60

               halign: "center"
               text: "Profile"
               disabled: True if root.access_denied else False
               on_press: root.manager.current = 'profile'
    Manager:
        id: screen_manager

<ScreenLogIn>:
    id: login_screen
    BoxLayout:
        orientation: "vertical"
        padding: 20, 20
        spacing: 50
        TextInput:
            id: login
            size_hint_y: None

            multiline: False
        TextInput:
            id: passw
            size_hint_y: None
            multiline: False
            password: True # hide password
        Button:
            text: "Log In"
            on_release: root.verify_credentials()

<ScreenNearUsers>:
    ScrollView:
        GridLayout:
            id: grid
            size_hint_y: None
            height: self.minimum_height
            cols: 2
            row_default_height: '20dp'
            row_force_default: True
            spacing: 0, 0
            padding: 0, 0


<ScreenMatch>:
    name: 'Carousel'
    fullscreen: True

    BoxLayout:
       size_hint_y: None
       height: '48dp'

       Button:
           text: 'last user'
           id: last_user

       Button:
            text: 'like'

       Button:
           text: 'super like'
           on_release: carousel.load_previous()

       Button:
           text: 'Dislike'
           on_release: carousel.load_next()

    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'center'

    Carousel:
        id: carousel
        loop: last_user.state == 'down'


<ScreenChats>:

    ScrollView:        
        GridLayout:
            id: chat_layout
            size_hint_y: None
            height: self.minimum_height
            cols: 1
            row_default_height: '125dp'
            row_force_default: True
            spacing: 0, 0
            padding: 0, 0


<ScreenUserProfile>:

    Button:
        text: "stuff4"

<Manager>:
    id: screen_manager
    screen_log_in: screen_log_in
    screen_near_users: screen_near_users
    screen_match: screen_match
    screen_chats: screen_chats
    screen_user_profile: screen_user_profile

    ScreenLogIn:
        id: screen_log_in
        name: 'login'
        manager: screen_manager

    ScreenNearUsers:
        id: screen_near_users
        name: 'near_users'
        manager: screen_manager

    ScreenMatch:
        id: screen_match
        name: 'match'
        manager: screen_manager

    ScreenChats:
        id: screen_chats
        name: 'chats'
        manager: screen_manager

    ScreenUserProfile:
        id: screen_user_profile
        name: 'profile'
        manger: screen_manager
:
经理:屏幕管理器
方向:“垂直”
id:行动
操作栏:
尺寸:0.1
背景颜色:0,0,1000,10
背景_正常:“
ActionView:
以前的行动:
应用程序图标:“
与_previous一起:False
正文:“[b]Dhobiwala.com[/b]”
标记:正确
字体大小:“16dp”
操作按钮:
id:near_用户
大小提示:1,1
高度:自身纹理大小[1]
宽度:自纹理大小[0]+40
最小宽度:自纹理大小[0]+60
哈利恩:“中心”
图标:“icons/internet.png”
禁用:如果root.access\u被拒绝,则为True;否则为False
按:root.manager.current='near\u users'
操作按钮:
id:匹配的_条
大小提示:1,1
高度:自身纹理大小[1]
宽度:自纹理大小[0]+40
最小宽度:-自身纹理大小[0]+60
哈利恩:“中心”
文本:“匹配”
禁用:如果root.access\u被拒绝,则为True;否则为False
按:root.manager.current='match'
操作按钮:
id:聊天室
大小提示:1,1
高度:自身纹理大小[1]
宽度:自纹理大小[0]+40
最小宽度:自纹理大小[0]+60
哈利恩:“中心”
文字:“聊天”
禁用:如果root.access\u被拒绝,则为True;否则为False
按:root.manager.current='chats'
操作按钮:
id:个人资料
大小提示:1,1
高度:自身纹理大小[1]
宽度:自纹理大小[0]+40
最小宽度:自纹理大小[0]+60
哈利恩:“中心”
正文:“简介”
禁用:如果root.access\u被拒绝,则为True;否则为False
按:root.manager.current='profile'
经理:
id:屏幕管理器
:
id:登录屏幕
盒子布局:
方向:“垂直”
填充:20,20
间距:50
文本输入:
id:登录
尺寸提示:无
多行:False
文本输入:
身份证号码:passw
尺寸提示:无
多行:False
密码:True#隐藏密码
按钮:
文本:“登录”
发布时:root.verify\u凭证()
:
滚动视图:
网格布局:
id:网格
尺寸提示:无
高度:自身最小高度
科尔斯:2
行\默认\高度:“20dp”
行\强制\默认值:True
间距:0,0
填充:0,0
:
名称:“旋转木马”
全屏:正确
盒子布局:
尺寸提示:无
高度:“48dp”
按钮:
文本:“最后一个用户”
id:最后一个用户
按钮:
文字:“喜欢”
按钮:
文字:“超级喜欢”
释放时:carousel.load_previous()
按钮:
文字:“不喜欢”
释放时:carousel.load\u next()
主持人安排:
主播:中锋
主播:“中心”
旋转木马:
id:旋转木马
循环:last_user.state=='down'
:
滚动视图:
网格布局:
id:聊天室布局
尺寸提示:无
高度:自身最小高度
科尔斯:1
行\默认\高度:“125dp”
行\强制\默认值:True
间距:0,0
填充:0,0
:
按钮:
文字:“stuff4”
:
id:屏幕管理器
屏幕登录:屏幕登录
screen_near_用户:screen_near_用户
屏幕匹配:屏幕匹配
屏幕聊天:屏幕聊天
屏幕用户配置文件:屏幕用户配置文件
屏幕登录:
id:屏幕\登录\登录
名称:“登录”
经理:屏幕管理器
ScreenNear用户:
id:screen\u靠近用户
名称:'near_用户'
经理:屏幕管理器
屏幕匹配:
id:屏幕匹配
姓名:'匹配'
经理:屏幕管理器
屏幕聊天:
id:screen_聊天室
姓名:'聊天'
经理:屏幕管理器
ScreenUserProfile:
id:屏幕\用户\配置文件
名称:“个人资料”
经理:屏幕管理器
我将使按钮透明,所以我不担心按钮的角显示圆圈外的图像

带通缉职位示例的图片


您是否尝试过使用
按钮的
背景
属性?您可能需要执行一些更复杂的图像操作,例如使图像具有与
按钮相同的纵横比,因为按钮将拉伸/收缩图像以适合
按钮。您可能还需要使图像中的所有内容都透明,除了圆圈内的区域。我如何更改纵横比?我的意思是,你的圆形图像实际上可能需要是一个矩形图像,其宽度/长度的比例与你的
按钮的宽度/长度的比例相同。好的,谢谢你,我现在明白你的意思了。我可以将透明图像和圆形图像组合在一起,但我不能让它适应按钮外伸展非常严重?