【发布时间】: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