Python Kivy,TypeError:';非类型';对象没有属性'__获取项目';
在尝试运行示例kivy应用程序时,我遇到了上述错误 以下是python代码: main.pyPython Kivy,TypeError:';非类型';对象没有属性'__获取项目';,python,kivy,Python,Kivy,在尝试运行示例kivy应用程序时,我遇到了上述错误 以下是python代码: main.py from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition class MyScreenManager(ScreenManager): pass class MainScree
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition
class MyScreenManager(ScreenManager):
pass
class MainScreen(Screen):
pass
class MailScreen(Screen):
pass
class ProjectScreen(Screen):
pass
class IntroScreen(Screen):
pass
class GsaMain(BoxLayout):
pass
class GsamApp(App):
def build(self):
return MyScreenManager()
GsamApp().run()
名为gsam.kv的kivy文件
MyScreenManager:
IntroScreen:
MainScreen:
PrjcScreen:
MailScreen:
ChatScreen:
<IntroScreen>:
name: 'introscreen'
BoxLayout:
Label:
text: 'Welcome to GSAM'
font_size: 35
BoxLayout:
Label:
text: "Login"
TextInput:
id: login
text: "Login"
Button:
text: "Connect"
on_release: root.current = 'mainpage'
BoxLayout:
Button:
text: 'Project Page'
font_size: 25
on_release: app.root.current = 'main'
<MainScreen>:
name: 'main'
BoxLayout:
Label:
text: 'Your GSAM Portal'
font_size: 35
BoxLayout:
Button:
text: 'Check Mails'
font_size: 25
on_release: app.root.current = 'mailscreen'
canvas:
color: rgba: 1,0,0,1
Rectangle:
size: 50, 50
Label: "Post Projects"
id: projct
Rectangle:
size: 50, 50
Label: "Messages"
id: msgs
<MailScreen>:
name: 'mailscreen'
BoxLayout:
Label:
text: 'GSAM Mail'
font_size: 35
BoxLayout:
Button:
text: 'Chat'
font_size: 25
on_release: app.root.current = 'chat'
<ChatScreen>:
name: 'chatscreen'
BoxLayout:
Label:
text: 'Chat with your Colleagues'
font_size: 35
BoxLayout:
Button:
text: 'Home'
font_size: 25
on_release: app.root.current = 'main'
<PrjcScreen>:
name: 'prjcscreen'
BoxLayout:
Label:
text: 'Community Projects'
font_size: 35
BoxLayout:
Button:
text: 'community Projects'
font_size: 25
on_release: app.root.current = 'main'
GridLayout:
cols: 2
rows: 4
padding: 5
spacing: 5
Label:
text: "Project Title"
TextInput:
id: ptitle
Label:
text: "Project Description"
TextInput:
id: pdescr
MyScreenManager:
介绍屏幕:
主屏幕:
PRJC屏幕:
邮件屏幕:
聊天室屏幕:
:
名称:“introscreen”
盒子布局:
标签:
文字:“欢迎来到GSAM”
字体大小:35
盒子布局:
标签:
文本:“登录”
文本输入:
id:登录
文本:“登录”
按钮:
文本:“连接”
发布时:root.current='mainpage'
盒子布局:
按钮:
文本:“项目页面”
字体大小:25
发布时:app.root.current='main'
:
名称:'main'
盒子布局:
标签:
文本:“您的GSAM门户”
字体大小:35
盒子布局:
按钮:
文本:“检查邮件”
字体大小:25
发布时:app.root.current='mailscreen'
画布:
颜色:rgba:1,0,0,1
矩形:
尺码:50,50
标签:“发布项目”
id:projct
矩形:
尺码:50,50
标签:“信息”
id:msgs
:
名称:'mailscreen'
盒子布局:
标签:
文本:“GSAM邮件”
字体大小:35
盒子布局:
按钮:
文字:“聊天”
字体大小:25
发布时:app.root.current='chat'
:
名称:“聊天屏幕”
盒子布局:
标签:
文字:“与同事聊天”
字体大小:35
盒子布局:
按钮:
文字:“家”
字体大小:25
发布时:app.root.current='main'
:
名称:“prjcscreen”
盒子布局:
标签:
正文:“社区项目”
字体大小:35
盒子布局:
按钮:
正文:“社区项目”
字体大小:25
发布时:app.root.current='main'
网格布局:
科尔斯:2
行数:4
填充:5
间距:5
标签:
正文:“项目名称”
文本输入:
id:ptitle
标签:
正文:“项目说明”
文本输入:
id:pdescr
我搞不清楚追踪指向的是什么
Traceback (most recent call last):
File "main.py", line 28, in <module>
GsamApp().run()
File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/app.py", line 797, in run
self.load_kv(filename=self.kv_file)
File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/app.py", line 594, in load_kv
root = Builder.load_file(rfilename)
File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py", line 1749, in load_file
return self.load_string(data, **kwargs)
File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py", line 1796, in load_string
parser = Parser(content=string, filename=fn)
File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py", line 1185, in __init__
self.parse(content)
File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py", line 1287, in parse
objects, remaining_lines = self.parse_level(0, lines)
File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py", line 1384, in parse_level
level + 1, lines[i:], spaces)
File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py", line 1384, in parse_level
level + 1, lines[i:], spaces)
File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py", line 1437, in parse_level
if current_property[:3] == 'on_':
TypeError: 'NoneType' object has no attribute '__getitem__'
回溯(最近一次呼叫最后一次):
文件“main.py”,第28行,在
GsamApp().run()
文件“/home/afidegnum/gsam/local/lib/python2.7/site packages/kivy/app.py”,第797行,正在运行
self.load_-kv(文件名=self.kv_文件)
文件“/home/afidegnum/gsam/local/lib/python2.7/site packages/kivy/app.py”,第594行,负载为kv
root=Builder.load\u文件(rfilename)
文件“/home/afidegnum/gsam/local/lib/python2.7/site packages/kivy/lang.py”,第1749行,在load_文件中
返回self.load_字符串(数据,**kwargs)
文件“/home/afidegnum/gsam/local/lib/python2.7/site packages/kivy/lang.py”,第1796行,装入字符串
解析器=解析器(内容=字符串,文件名=fn)
文件“/home/afidegnum/gsam/local/lib/python2.7/site packages/kivy/lang.py”,第1185行,在__
self.parse(内容)
解析中的文件“/home/afidegnum/gsam/local/lib/python2.7/site packages/kivy/lang.py”,第1287行
对象,剩余的_行=self.parse_级别(0,行)
文件“/home/afidegnum/gsam/local/lib/python2.7/site packages/kivy/lang.py”,第1384行,解析级
标高+1,行[i:],空格)
文件“/home/afidegnum/gsam/local/lib/python2.7/site packages/kivy/lang.py”,第1384行,解析级
标高+1,行[i:],空格)
文件“/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py”,第1437行,解析级
如果当前_属性[:3]=“on_u”:
TypeError:“非类型”对象没有属性“\uuuu getitem\uuuu”
我需要在python代码中定义一个函数吗 main.py
- 没有
。定义一个:聊天屏幕
类聊天屏幕(屏幕): 通过
应重命名为ProjectScreen
,以匹配PrjcScreen
中的名称。(或在gsam.kv
中更改gsam.kv
)PrjcScreen
->: ->颜色
- 没有
参数:标签
Rectangle: size: 50, 50 # Label: "Post Projects" <--- id: projct Rectangle: size: 50, 50 # Label: "Messages" <--- id: msgs
矩形: 尺码:50,50
#标签:“发布项目”我已经编写了您的代码并生成了一个可用的代码。除了上面给出的建议之外,我还注意到在你的.kv文件中,有很多缩进问题。我在必要的时候发表评论,我做出了这些改变。请在下面查找工作代码: main.py
gsma.kvfrom kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition class MyScreenManager(ScreenManager): pass class MainScreen(Screen): pass class MailScreen(Screen): pass class ChatScreen(Screen): pass class ProjectScreen(Screen): pass class IntroScreen(Screen): pass class GsaMain(BoxLayout): pass class GsamApp(App): def build(self): sm = MyScreenManager() sm.add_widget(IntroScreen(name="introscreen")) sm.add_widget(MainScreen(name="main")) sm.add_widget(MailScreen(name="mailscreen")) sm.add_widget(ProjectScreen(name="prjcscreen")) sm.add_widget(ChatScreen(name='chat')) return sm GsamApp().run()
<IntroScreen>: name: 'introscreen' BoxLayout: orientation: 'vertical' padding: 100,100 spacing: 50 Label: text: 'Welcome to GSAM' font_size: 35 Label: text: "Login" TextInput: id: login text: "Login" # BoxLayout: # this makes the button widget covers the whole page. remove it Button: text: "Connect" on_release: app.root.current = 'main' Button: text: 'Project Page' font_size: 15 on_release: app.root.current = 'prjcscreen' <MainScreen>: name: 'main' BoxLayout: orientation:'vertical' padding:100,100 spacing:50 Label: text: 'Your GSAM Portal' font_size: 35 # BoxLayout: # remove this. Dont repeat BoxLayout for every widget u want show on the screen except for a good reason. Or better still, ident the BoxLayout as a child of the First Boxlayout Button: text: 'Check Mails' # text, font_size,on_realease must not be in the same line with button font_size: 25 on_release: app.root.current = 'mailscreen' canvas.before: Color: # I have reidented Color rgba: 1,0,0,1 # this line must be here Rectangle: # Rectangle must ident with Color size: 50, 50 # Label: "Post Projects" # remove the label here id: projct # and place it either before of after canvas class Rectangle: size: 50, 50 # Label: "Messages" id: msgs <MailScreen>: name: 'mailscreen' BoxLayout: Label: text: 'GSAM Mail' font_size: 35 Button: text: 'Chat' font_size: 25 on_release: app.root.current = 'chat' <ChatScreen>: name: 'chatscreen' BoxLayout: Label: text: 'Chat with \nyour Colleagues' font_size: 35 Button: text: 'Home' font_size: 25 on_release: app.root.current = 'main' <ProjectScreen>: name: 'prjcscreen' BoxLayout: Label: text: 'Community Projects' font_size: 35 Button: text: 'community Projects' font_size: 25 on_release: app.root.current = 'main' GridLayout: cols: 2 rows: 4 padding: 5 spacing: 5 Label: text: "Project Title" TextInput: id: ptitle Label: text: "Project Description" TextInput: id: pdescr
: 名称:“introscreen” 盒子布局: 方向:“垂直” 填充:100100 间距:50 标签: 文字:“欢迎来到GSAM” 字体大小:35 标签: 文本:“登录” 文本输入: id:登录 文本:“登录” #BoxLayout:#这使得按钮小部件覆盖整个页面。移除它 按钮: 文本:“连接” 发布时:app.root.current='main' 按钮: 文本:“项目页面” 字体大小:15 发布时:app.root.current='prjcscreen' : 名称:'main' 盒子布局: 方向:'垂直' 填充:100100 间距:50 标签: 文本:“您的GSAM门户” 字体大小:35 #BoxLayout:#将其移除。除非有充分的理由,否则不要对你想在屏幕上显示的每个小部件重复BoxLayout。或者更好的是,将BoxLayout识别为第一个BoxLayout的子项
from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition class MyScreenManager(ScreenManager): pass class MainScreen(Screen): pass class MailScreen(Screen): pass class ChatScreen(Screen): pass class ProjectScreen(Screen): pass class IntroScreen(Screen): pass class GsaMain(BoxLayout): pass class GsamApp(App): def build(self): sm = MyScreenManager() sm.add_widget(IntroScreen(name="introscreen")) sm.add_widget(MainScreen(name="main")) sm.add_widget(MailScreen(name="mailscreen")) sm.add_widget(ProjectScreen(name="prjcscreen")) sm.add_widget(ChatScreen(name='chat')) return sm GsamApp().run()
<IntroScreen>: name: 'introscreen' BoxLayout: orientation: 'vertical' padding: 100,100 spacing: 50 Label: text: 'Welcome to GSAM' font_size: 35 Label: text: "Login" TextInput: id: login text: "Login" # BoxLayout: # this makes the button widget covers the whole page. remove it Button: text: "Connect" on_release: app.root.current = 'main' Button: text: 'Project Page' font_size: 15 on_release: app.root.current = 'prjcscreen' <MainScreen>: name: 'main' BoxLayout: orientation:'vertical' padding:100,100 spacing:50 Label: text: 'Your GSAM Portal' font_size: 35 # BoxLayout: # remove this. Dont repeat BoxLayout for every widget u want show on the screen except for a good reason. Or better still, ident the BoxLayout as a child of the First Boxlayout Button: text: 'Check Mails' # text, font_size,on_realease must not be in the same line with button font_size: 25 on_release: app.root.current = 'mailscreen' canvas.before: Color: # I have reidented Color rgba: 1,0,0,1 # this line must be here Rectangle: # Rectangle must ident with Color size: 50, 50 # Label: "Post Projects" # remove the label here id: projct # and place it either before of after canvas class Rectangle: size: 50, 50 # Label: "Messages" id: msgs <MailScreen>: name: 'mailscreen' BoxLayout: Label: text: 'GSAM Mail' font_size: 35 Button: text: 'Chat' font_size: 25 on_release: app.root.current = 'chat' <ChatScreen>: name: 'chatscreen' BoxLayout: Label: text: 'Chat with \nyour Colleagues' font_size: 35 Button: text: 'Home' font_size: 25 on_release: app.root.current = 'main' <ProjectScreen>: name: 'prjcscreen' BoxLayout: Label: text: 'Community Projects' font_size: 35 Button: text: 'community Projects' font_size: 25 on_release: app.root.current = 'main' GridLayout: cols: 2 rows: 4 padding: 5 spacing: 5 Label: text: "Project Title" TextInput: id: ptitle Label: text: "Project Description" TextInput: id: pdescr