Android 如何解决;无效的属性名称";使用kivy创建文件选择器时出错?
我是kivy的新手,正在尝试构建一个应用程序,其中我有三个选项卡:一个用于加载图像,另一个用于并排显示图像和灰度版本,最后一个选项卡仅用于显示灰度图像 我从中获得了以下内容,并希望将其用于Python3,但我多次收到以下错误 无效的属性名 这是我当前使用的代码:Android 如何解决;无效的属性名称";使用kivy创建文件选择器时出错?,android,python,python-3.x,image,kivy,Android,Python,Python 3.x,Image,Kivy,我是kivy的新手,正在尝试构建一个应用程序,其中我有三个选项卡:一个用于加载图像,另一个用于并排显示图像和灰度版本,最后一个选项卡仅用于显示灰度图像 我从中获得了以下内容,并希望将其用于Python3,但我多次收到以下错误 无效的属性名 这是我当前使用的代码: import kivy; from kivy.app import App from kivy.uix.tabbedpanel import TabbedPanel from kivy.properties import Object
import kivy;
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from PIL import*
Builder.load_string("""
<RootWidget>:
manager:manager
img:img
img3:img3
img4:img4
lab:lab
do_default_tab:False
ScreenManager:
id:manager
Screen:
id:sc1
name:'Load img'
FileChooserIconView
canvas.before:
Color:
rgb:0.5, 0.4, 0.5
Rectangle:
pos:self.pos
size: self.size
on_selection:root.select_to(*args)
Screen:
id:sc2
name:'Image'
FloatLayout:
Button:
id:lab
pos_hint:{"right": 0.55, 'top': 1}
size_hint:.15,0.1
RelativeLayout:
Image:
id:img
on_touch_down:str('Relative:{}'.format(args[1].pos))
post_hint:{"left": 1, 'bottom': 1}
size_hint:0.5, 1
allow_strech:True
RelativeLayout:
Image:
id:img3
post_hint:{"right": 1, 'bottom': 1}
size_hint:0.5, 1
allow_strech = True
Screen:
id:sc3
name:'Image_'
FloatLayout:
Image:
id:img4
keep_data:True
post:self.pos
size: self.size
TabbedPanelHeader:
text.sc1.name
background_color: 1,0,0,1
screen:sc1.name
TabbedPanelHeader:
text:sc2.name
background_color: 1,1,0,1
screen:sc2.name
TabbedPanelHeader:
text:sc3.name
background_color: 1,0,1,1
screen:sc3.name
""")
class RootWidget(TabbedPanel):
manager = ObjectProperty(None)
def on_touch_up(self, touch):
if not self.img3.collide_point(*touch.pos):
return True
else:
self.lab.text ='Pos: (%d,%d)' %(touch.x, touch.y)
return True
def switch_to(self, header):
# set the Screen manager to load the appropriate screen
# linked to the tab head instead of loading content
self.manager.current = header.screen
# we have to replace the functionality of the original switch_to
self.current_tab.state = "normal"
header.state = 'down'
self._current_tab = header
def select_to(self, *args):
try:
print(args[1][0])
iw = Image.open(args[1][0])
iw.save('./phase.jpg')
gray = iw.convert('1')
gray.save('.gray_im.jpg')
self.img3.source = './gray_im.jpg'
self.img4.source = './gray_im.jpg'
self.img.source = './phase.jpg'
self.img.reload()
self.img3.reload()
self.img4.reload()
except:
pass
def update_touch_lavel(self, label, touch):
label.text = 'Pos:(%d, %d)' % (touch.x, touch.y)
label.texture_update()
label.pos = touch.pos
label.size = label.texture_size[0] + 20, label.texture_size[1] + 20
class MainApp(App):
title = 'Screen Widget'
def build(self):
return RootWidget()
def on_pause(self):
return True
if __name__ == '__main___':
MainApp().run()
进口kivy;
从kivy.app导入应用程序
从kivy.uix.tabbedpanel导入tabbedpanel
从kivy.properties导入ObjectProperty
从kivy.lang导入生成器
从PIL进口*
生成器。加载\u字符串(“”)
:
经理:经理
img:img
img3:img3
img4:img4
实验室:实验室
do_default_选项卡:False
屏幕管理器:
id:经理
屏幕:
编号:sc1
名称:'Load img'
FileChooseConView
在以下情况之前:
颜色:
rgb:0.5,0.4,0.5
矩形:
pos:self.pos
大小:self.size
在_选择上:根。选择_到(*args)
屏幕:
编号:sc2
名称:'Image'
浮动布局:
按钮:
id:实验室
pos_提示:{“right”:0.55,“top”:1}
尺寸提示:.15,0.1
相对长度:
图片:
id:img
按下按钮:str('Relative:{}'。格式(args[1].pos))
post_提示:{“左”:1,“下”:1}
尺寸提示:0.5,1
允许:对
相对长度:
图片:
id:img3
post_提示:{“right”:1,“bottom”:1}
尺寸提示:0.5,1
允许\u strech=True
屏幕:
编号:sc3
名称:'Image\uu'
浮动布局:
图片:
id:img4
保留数据:True
post:self.pos
大小:self.size
选项卡面板标题:
text.sc1.name
背景颜色:1,0,0,1
屏幕:sc1.name
选项卡面板标题:
文本:sc2.name
背景颜色:1,1,0,1
屏幕:sc2.name
选项卡面板标题:
文本:sc3.name
背景颜色:1,0,1,1
屏幕:sc3.name
""")
类RootWidget(选项卡面板):
manager=ObjectProperty(无)
def on_touch_up(自我,触摸):
如果不是自身img3碰撞点(*触摸位置):
返回真值
其他:
self.lab.text='Pos:(%d,%d)'(touch.x,touch.y)
返回真值
def开关_至(自身、收割台):
#设置屏幕管理器以加载适当的屏幕
#链接到选项卡标题,而不是加载内容
self.manager.current=header.screen
#我们必须将原来开关的功能替换为
self.current\u tab.state=“正常”
header.state='down'
self.\u current\u tab=标题
def select_to(自身,*参数):
尝试:
打印(参数[1][0])
iw=Image.open(args[1][0])
保存('./phase.jpg')
灰色=iw.convert('1')
gray.save('.gray\u im.jpg'))
self.img3.source='./gray_im.jpg'
self.img4.source='./gray_im.jpg'
self.img.source='./phase.jpg'
self.img.reload()
self.img3.reload()
self.img4.reload()
除:
通过
def更新触摸屏(自我、标签、触摸屏):
label.text='位置:(%d,%d)'(touch.x,touch.y)
label.texture_update()
label.pos=touch.pos
label.size=label.texture\u size[0]+20,label.texture\u size[1]+20
类主应用程序(应用程序):
标题='屏幕小部件'
def生成(自):
返回RootWidget()
def on_暂停(自我):
返回真值
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuuuuu':
MainApp().run()
我不确定为什么我会收到这个错误。如果能提供一些帮助,使代码按预期工作,我将不胜感激。有关详细信息,请参考示例 例子 main.py 试验电压(千伏)
#:kivy 1.10.1
:
经理:经理
img:img
img3:img3
img4:img4
实验室:实验室
do_default_选项卡:False
屏幕管理器:
id:经理
屏幕:
编号:sc1
名称:'Load img'
FileChooseConView:
在以下情况之前:
颜色:
rgb:0.5,0.4,0.5
矩形:
pos:self.pos
大小:self.size
关于选择:
根目录。选择要(*args)
屏幕:
编号:sc2
名称:“图像”
浮动布局:
按钮:
id:实验室
pos_提示:{“right”:0.55,“top”:1}
尺寸提示:.15,0.1
相对长度:
图片:
id:img
按下按钮时:
str('Relative:{}'。格式(args[1].pos))
位置提示:{“左”:1,“下”:1}
尺寸提示:0.5,1
允许拉伸:真
相对长度:
图片:
id:img3
pos_提示:{“right”:1,“bottom”:1}
尺寸提示:0.5,1
允许拉伸:真
屏幕:
编号:sc3
名称:“图像”
浮动布局:
图片:
id:img4
保留数据:True
post:self.pos
大小:self.size
选项卡面板标题:
文本:sc1.name
背景颜色:1,0,0,1
屏幕:sc1.name
选项卡面板标题:
文本:sc2.name
背景颜色:1,1,0,1
屏幕:sc2.name
选项卡面板标题:
文本:sc3.name
背景颜色:1,0,1,1
屏幕:sc3.name
输出
您可以改进代码的识别,这太可怕了。@eyllanesc抱歉,复制时代码的格式会弄乱
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.properties import ObjectProperty
from PIL import Image
class RootWidget(TabbedPanel):
manager = ObjectProperty(None)
img = ObjectProperty(None)
img3 = ObjectProperty(None)
img4 = ObjectProperty(None)
lab = ObjectProperty(None)
def on_touch_up(self, touch):
if not self.img3.collide_point(*touch.pos):
return True
else:
self.lab.text = 'Pos: (%d,%d)' % (touch.x, touch.y)
return True
def switch_to(self, header):
# set the Screen manager to load the appropriate screen
# linked to the tab head instead of loading content
self.manager.current = header.screen
# we have to replace the functionality of the original switch_to
self.current_tab.state = "normal"
header.state = 'down'
self._current_tab = header
def select_to(self, *args):
try:
print(args[1][0])
iw = Image.open(args[1][0])
iw.save('./phase.jpg')
gray = iw.convert('1')
gray.save('./gray_im.jpg')
self.img3.source = './gray_im.jpg'
self.img4.source = './gray_im.jpg'
self.img.source = './phase.jpg'
self.img.reload()
self.img3.reload()
self.img4.reload()
except:
pass
def update_touch_label(self, label, touch):
label.text = 'Pos:(%d, %d)' % (touch.x, touch.y)
label.texture_update()
label.pos = touch.pos
label.size = label.texture_size[0] + 20, label.texture_size[1] + 20
class TestApp(App):
title = 'Screen Widget'
def build(self):
return RootWidget()
def on_pause(self):
return True
if __name__ == '__main__':
TestApp().run()
#:kivy 1.10.1
<RootWidget>:
manager: manager
img: img
img3: img3
img4: img4
lab: lab
do_default_tab: False
ScreenManager:
id: manager
Screen:
id: sc1
name:'Load img'
FileChooserIconView:
canvas.before:
Color:
rgb: 0.5, 0.4, 0.5
Rectangle:
pos: self.pos
size: self.size
on_selection:
root.select_to(*args)
Screen:
id: sc2
name: 'Image'
FloatLayout:
Button:
id: lab
pos_hint: {"right": 0.55, 'top': 1}
size_hint: .15,0.1
RelativeLayout:
Image:
id: img
on_touch_down:
str('Relative:{}'.format(args[1].pos))
pos_hint: {"left": 1, 'bottom': 1}
size_hint: 0.5, 1
allow_stretch: True
RelativeLayout:
Image:
id: img3
pos_hint: {"right": 1, 'bottom': 1}
size_hint: 0.5, 1
allow_stretch: True
Screen:
id: sc3
name: 'Image_'
FloatLayout:
Image:
id: img4
keep_data: True
post: self.pos
size: self.size
TabbedPanelHeader:
text: sc1.name
background_color: 1, 0, 0, 1
screen: sc1.name
TabbedPanelHeader:
text: sc2.name
background_color: 1, 1, 0, 1
screen: sc2.name
TabbedPanelHeader:
text: sc3.name
background_color: 1, 0, 1, 1
screen: sc3.name