如何使用 Kivy 将选中的图像放置在 BoxLayout 中
How to place the image selected in a BoxLayout with Kivy
我正在尝试 select 一张图片并使用 KivyMD FileManager 将其放置在盒子布局中,但它会出现很多错误,例如(AttributeError: 'super' object has no attribute 'getattr') 当我使用 self.root.ids.image.source = path
时,另一次尝试 self.root.ids["image"].source = path
给了我 (KeyError: 'image') 和 self.ids["image"].source = path
给了 AttributeError: ( 'MyMainApp' 对象没有属性 'ids')。这是 python 代码:
from kivymd.app import MDApp
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.filemanager import MDFileManager
from kivy.uix.image import Image
from kivymd.toast import toast
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("file.kv")
class MyMainApp(MDApp):
def __init__(self, **kwargs):
super(MyMainApp, self).__init__(**kwargs)
self.manager_open = False
self.file_manager = MDFileManager(
exit_manager=self.exit_manager, # function called when the user reaches directory tree root
select_path=self.select_path, # function called when selecting a file/directory
#preview=True,
)
def file_manager_open(self):
self.file_manager.show("/") # output manager to the screen
self.manager_open = True
def select_path(self, path):
"""It will be called when you click on the file name
or the catalog selection button.
:type path: str;
:param path: path to the selected directory or file;
"""
self.exit_manager()
toast(path)
def exit_manager(self, *args):
"""Called when the user reaches the root of the directory tree."""
self.manager_open = False
self.file_manager.close()
def build(self):
return kv
if __name__ == "__main__":
MyMainApp(title="Noyse Remove").run()
.kv 文件
WindowManager:
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
id: pri
canvas:
Rectangle:
source: 'img/entrada.jpg'
size: self.width, self.height
Button:
text: "Open Archives"
size_hint: 0.166, 0.075
pos_hint: {"center_x": .5, "center_y": .3}
on_release:
app.root.current = "second"
root.manager.transition.direction = "left"
app.file_manager_open()
<SecondWindow>:
name: "second"
id: sec
canvas.before:
Color:
rgba: .01, .01, .01, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
id: box
orientation: "horizontal"
size: root.width, root.height
padding: 50
spacing: 50
Image:
id: image
source: "" # I want to place here the path of image selected
size:root.width, root.height
pos_hint: {'x': .1, 'y': .1}
Slider:
size_hint: 0.6, 0.1
min: 0
max: 100
on_value: label.text = str(self.value)
pos_hint:{'center_x': .5, 'center_y': .1}
Label:
id: label
text: "0.0"
pos_hint:{'center_x': .5, 'center_y': .05}
image
id
在 <SecondWindow>
规则中定义,因此 id
可从 SecondWindow
中的 ids
获得实例。因此,您必须获取 MyMainApp
中的 SecondWindow
实例。您可以将 Image
source
设置为:
self.root.get_screen('second').ids.image.source = path
假设这段代码是在MyMainApp
的一个方法中执行的。 self.root
是 App
的根小部件,即 WindowManager
。 get_screen('second')
方法获取对 SecondWindow
实例的引用,ids.image
获取对 Image
实例的引用。
我正在尝试 select 一张图片并使用 KivyMD FileManager 将其放置在盒子布局中,但它会出现很多错误,例如(AttributeError: 'super' object has no attribute 'getattr') 当我使用 self.root.ids.image.source = path
时,另一次尝试 self.root.ids["image"].source = path
给了我 (KeyError: 'image') 和 self.ids["image"].source = path
给了 AttributeError: ( 'MyMainApp' 对象没有属性 'ids')。这是 python 代码:
from kivymd.app import MDApp
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.filemanager import MDFileManager
from kivy.uix.image import Image
from kivymd.toast import toast
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("file.kv")
class MyMainApp(MDApp):
def __init__(self, **kwargs):
super(MyMainApp, self).__init__(**kwargs)
self.manager_open = False
self.file_manager = MDFileManager(
exit_manager=self.exit_manager, # function called when the user reaches directory tree root
select_path=self.select_path, # function called when selecting a file/directory
#preview=True,
)
def file_manager_open(self):
self.file_manager.show("/") # output manager to the screen
self.manager_open = True
def select_path(self, path):
"""It will be called when you click on the file name
or the catalog selection button.
:type path: str;
:param path: path to the selected directory or file;
"""
self.exit_manager()
toast(path)
def exit_manager(self, *args):
"""Called when the user reaches the root of the directory tree."""
self.manager_open = False
self.file_manager.close()
def build(self):
return kv
if __name__ == "__main__":
MyMainApp(title="Noyse Remove").run()
.kv 文件
WindowManager:
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
id: pri
canvas:
Rectangle:
source: 'img/entrada.jpg'
size: self.width, self.height
Button:
text: "Open Archives"
size_hint: 0.166, 0.075
pos_hint: {"center_x": .5, "center_y": .3}
on_release:
app.root.current = "second"
root.manager.transition.direction = "left"
app.file_manager_open()
<SecondWindow>:
name: "second"
id: sec
canvas.before:
Color:
rgba: .01, .01, .01, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
id: box
orientation: "horizontal"
size: root.width, root.height
padding: 50
spacing: 50
Image:
id: image
source: "" # I want to place here the path of image selected
size:root.width, root.height
pos_hint: {'x': .1, 'y': .1}
Slider:
size_hint: 0.6, 0.1
min: 0
max: 100
on_value: label.text = str(self.value)
pos_hint:{'center_x': .5, 'center_y': .1}
Label:
id: label
text: "0.0"
pos_hint:{'center_x': .5, 'center_y': .05}
image
id
在 <SecondWindow>
规则中定义,因此 id
可从 SecondWindow
中的 ids
获得实例。因此,您必须获取 MyMainApp
中的 SecondWindow
实例。您可以将 Image
source
设置为:
self.root.get_screen('second').ids.image.source = path
假设这段代码是在MyMainApp
的一个方法中执行的。 self.root
是 App
的根小部件,即 WindowManager
。 get_screen('second')
方法获取对 SecondWindow
实例的引用,ids.image
获取对 Image
实例的引用。