【问题标题】:Kivy: Absolute position and size of an imageKivy:图像的绝对位置和大小
【发布时间】:2023-03-25 00:16:01
【问题描述】:

使用 Kivy,我如何找到图像的位置和大小?

下面的代码将位置打印为 [0,72],将图像的大小打印为 [800, 480]。我怀疑这分别是 BoxLayout 的位置和大小。如果我们使用下面的 Mels 图像,很明显位置和大小都不正确。

from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen

Builder.load_string("""

<MyScreenManager>:
    ThirdScreen:
        id: third_screen

<ThirdScreen>:
    name: '_third_screen_'
    id: third_screen
    BoxLayout:
        orientation: "vertical"
        id: third_screen_boxlayout
        canvas:
        Label:
            id: main_title
            text: "Title"
            size_hint: (1, 0.1)
        Image:
            id: main_image
            source: "C:/Users/OneDrive/0. T2/6. Kivy/4/claymore.jpg"
        BoxLayout:
            id: button_boxlayout
            orientation: "horizontal"
            padding: 10
            size_hint: (1, 0.15)
            Button:
                id: accept_button
                text: "Okay"
                size_hint: (0.33, 1)
                on_press: root.image_accepted_by_user(root.image_address)
            Button:
                id: crop_button
                text: "Crop"
                size_hint: (0.33, 1)
                on_press: root.enable_cropping()
            Button:
                id: cancel_button
                text: "Cancel"
                size_hint: (0.33, 1) 
                on_press: root.manager.current = '_first_screen_'
""")


class ThirdScreen(Screen, BoxLayout):
    def enable_cropping(self):
        print(self.ids.main_image.pos)
        print(self.ids.main_image.size)

# Create the screen manager
sm = ScreenManager()
sm.add_widget(ThirdScreen(name='_third_screen_'))

class MyApp(App):
    def build(self):
        return sm


if __name__ == '__main__':
    MyApp().run()

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    绝对位置和大小

    • 绝对大小,使用self.ids.main_image.norm_image_size
    • 绝对位置 x,使用 self.ids.main_image.center_x - self.ids.main_image.norm_image_size[0] / 2.
    • 绝对位置,使用self.ids.main_image.center_y - self.ids.main_image.norm_image_size[1] / 2.

    片段

    class ThirdScreen(Screen):
        def enable_cropping(self):
            print("\nThirdScreen:")
            print(self.ids.main_image.pos)
            print(self.ids.main_image.size)
            print("\tAbsolute size=", self.ids.main_image.norm_image_size)
            print("\tAbsolute pos_x=", self.ids.main_image.center_x - self.ids.main_image.norm_image_size[0] / 2.)
            print("\tAbsolute pos_y=", self.ids.main_image.center_y - self.ids.main_image.norm_image_size[1] / 2.)
    

    示例

    main.py

    ​​>
    from kivy.uix.boxlayout import BoxLayout
    from kivy.lang import Builder
    from kivy.app import App
    from kivy.uix.screenmanager import ScreenManager, Screen
    
    Builder.load_string("""
    
    <MyScreenManager>:
        ThirdScreen:
            id: third_screen
    
    <ThirdScreen>:
        name: '_third_screen_'
        id: third_screen
        BoxLayout:
            orientation: "vertical"
            id: third_screen_boxlayout
            canvas:
            Label:
                id: main_title
                text: "Title"
                size_hint: (1, 0.1)
                canvas.before:
                    Color:
                        rgba: 1, 0, 0, 1    # red
                    Rectangle:
                        pos: self.pos
                        size: self.size
            Image:
                id: main_image
                # source: "C:/Users/OneDrive/0. T2/6. Kivy/4/claymore.jpg"
                source: "./claymore.jpeg"
                canvas.before:
                    Color:
                        rgba: 0, 0, 1, 0.5    # blue
                    Rectangle:
                        pos: self.pos
                        size: self.size
            BoxLayout:
                id: button_boxlayout
                orientation: "horizontal"
                padding: 10
                size_hint: (1, 0.15)
                Button:
                    id: accept_button
                    text: "Okay"
                    size_hint: (0.33, 1)
                    on_press: root.image_accepted_by_user(root.image_address)
                Button:
                    id: crop_button
                    text: "Crop"
                    size_hint: (0.33, 1)
                    on_press: root.enable_cropping()
                Button:
                    id: cancel_button
                    text: "Cancel"
                    size_hint: (0.33, 1) 
                    on_press: root.manager.current = '_first_screen_'
    """)
    
    
    class MyScreenManager(ScreenManager):
        pass
    
    
    class ThirdScreen(Screen):
        def enable_cropping(self):
            print("\nThirdScreen:")
            print(self.ids.main_image.pos)
            print(self.ids.main_image.size)
            print("\tAbsolute size=", self.ids.main_image.norm_image_size)
            print("\tAbsolute pos_x=", self.ids.main_image.center_x - self.ids.main_image.norm_image_size[0] / 2.)
            print("\tAbsolute pos_y=", self.ids.main_image.center_y - self.ids.main_image.norm_image_size[1] / 2.)
    
    
    class MyApp(App):
        def build(self):
            return MyScreenManager()
    
    
    if __name__ == '__main__':
        MyApp().run()
    

    输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      相关资源
      最近更新 更多