python - 如何以另一种方法将所选图像的路径显示为变量?
问题描述
我有两种方法,get_image_one() 打开一个 Tkinter 文件选择器并返回一个显示在 kv 文件中的图像。我希望将所选图像设置为方法 check_for_image() 中的源,以便可以通过 check_for_image() 对所选图像进行进一步处理,例如检查用户选择的图像是否有任何文本等。逻辑此测试将由 check_for_image() 提供
请让我知道这是否是解决此问题的最佳方法,即有两种单独的方法,一种用于选择文件,第二种用于验证文件。感谢您的时间和关注。
下面是我的完整示例。
主文件
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from tkinter.filedialog import askopenfilename
from tkinter import Tk
class SampBoxLayout(BoxLayout):
def get_image_one(self):
'''This method is called by button FileChooser, it opens a FileChooser selects the desired image'''
Tk().withdraw()
img1 = askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
self.first_image.source = img1
def check_for_image(self):
'''This method is called by button TextIdentifier
1. It needs to get the image selected by the above FileChooser
2. The selected Image can be analysed further Ex: to check the if the selected image contains any text etc.
'''
pass
class SampleApp(App):
def build(self):
# Set the background color for the window
Window.clearcolor = (1, 1, 1, 1)
return SampBoxLayout()
sample_app = SampleApp()
sample_app.run()
样本.kv
#:kivy 1.10.0
<ColoredBackground@Image>:
font_size: '48sp'
color: (.9, .5, .4, 1)
canvas.before:
Color:
rgb: (.2, .9, .9)
Rectangle:
pos: self.x + sp(2), self.y + sp(2)
size: self.width - sp(5), self.height - sp(4)
<SampBoxLayout>:
first_image: first_image # <----
orientation: "vertical"
padding: 10
spacing: 10
# ---------- Button FileChooser ----------
BoxLayout:
orientation: "horizontal"
height: 30
Button:
text: "FileChooser"
on_press: root.get_image_one()
Button:
text: "TextIdentifier"
on_press: root.check_for_image()
# ---------- Display Images----------
BoxLayout:
orientation: "vertical"
height: 30
ColoredBackground:
id: first_image # <----
size: self.parent.width, self.parent.height
allow_stretch: True
keep_ratio: False
解决方案
解决方案
- 替换为
img1
以self.img1
其他方法访问图像。 - 在
get_image_one()
方法中,当前没有选择任何内容时 Kivy App 会崩溃。因此,检查用户是否选择了任何图像。如果没有选择怎么办?例如显示弹出消息。 - 在
check_for_image()
方法中,如果所有检查都通过,则将图像文件分配给图像的源。
建议
如果有很多支票,请将其分开。始终保持模块小,以便于理解和维护。
片段
class SampBoxLayout(BoxLayout):
def get_image_one(self):
'''This method is called by button FileChooser, it opens a FileChooser selects the desired image'''
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
self.img1 = askopenfilename(initialdir="/", title="Select file",
filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*")))
if not self.img1:
# TODO
# e.g. Kivy Popup message
print("No image file selected!")
def check_for_image(self):
'''This method is called by button TextIdentifier
1. It needs to get the image selected by the above FileChooser
2. The selected Image can be analysed further Ex: to check the if the selected image contains any text etc.
'''
print(self.img1)
self.first_image.source = self.img1
self.first_image.reload()
输出
推荐阅读
- linq - 处理来自相关实体的空值
- mysql - 如何编写一个 MySQL 查询在条件下搜索两个表中的数据?
- c++ - 在c ++中从带有参数的函数返回数组
- python - 控制转移到班级的下一行
- java - Collectors.toSet() 中的哪个 Set 实现?
- php - 我可以将简单的表单帖子转换为 ajax 表单帖子吗
- php - 如果使用标头('Content-Type: application/json')但不使用图像,PHP REST API 不接受图像
- spring - 如何使用spring boot从excel中读取数据并将其存储在mongodb中
- ms-access - 根据使用 VBA 或宏生成器的条件格式颜色过滤计算文本框中的报表值
- cucumber - 如何在 multiple-cucumber-html-reporter 上为功能设置自定义元数据?