我在kivy使用Screen Manager制作应用程序,我被困在这里。我是kivy和python的新手。据我所知,我正在做的一切正确。
错误说 -
kivy.uix.screenmanager.ScreenManagerException:没有名称的屏幕 “显示”
注意:为了简单起见,这只是我代码的一部分我只显示发生错误的行。
import kivy
kivy.require('1.10.0')
from kivy.uix.stacklayout import StackLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.textinput import TextInput
from kivy.properties import StringProperty
import json
Builder.load_file('VocabularyJournal.kv')
class MenuPage(Screen, StackLayout):
pass
class DisplayPage(Screen, BoxLayout):
def __init__(self, *args):# the error occured after I wrote these line before that every other screens were working properly.
BoxLayout.__init__(self,*args)
with open('vocab_words.json') as rfile:
data=json.load(rfile)
#some codes here
class WordInsertPage(Screen,FloatLayout):
pass
class NewWordPage(Screen,StackLayout):
pass
class FlashCard(Screen):
pass
class WordGroups(Screen):
pass
sm=ScreenManager()
sm.add_widget(MenuPage(name='menu'))
sm.add_widget(WordInsertPage(name='insertword'))
sm.add_widget(NewWordPage(name='newword'))
sm.add_widget(FlashCard(name='flashcard'))
sm.add_widget(WordGroups(name='wordgroup'))
sm.add_widget(DisplayPage(name='display'))
class VocabularyJournalApp(App):
def build(self):
return sm
object = VocabularyJournalApp()
object.run()
这是发生错误的kv文件的一部分:
# the part of the code where the error is occuring
<WordInsertPage>:
FloatLayout:
Button:
text: "New Word"
on_press: root.manager.current='newword'# this is working very well
font_size: 30
color: 0,0,0,1
size_hint: .2, .1
pos_hint: {"center_x": .5, "center_y": 0.3}
background_down: 'darkgrey.png'
Button:
text: "search word"
on_press: root.manager.current='display' # here the error says kivy.uix.screenmanager.ScreenManagerException: No Screen with name "display".
font_size: 30
color: 0,0,0,1
size_hint: .2, .1
pos_hint: {"center_x": .5, "center_y": 0.5}
background_down: 'darkgrey.png'
Button:
text: 'Flash Cards'
on_press: root.manager.current="flashcard"
font_size: 30
color: 0,0,0,1
size_hint: .2, .1
pos_hint: {"center_x": .5, "center_y": 0.7}
background_down: 'darkgrey.png'
这里是追溯:
[INFO ] [Logger ] Record log in C:\Users\HP\.kivy\logs\kivy_18-03-01_31.txt
[INFO ] [Kivy ] v1.10.0
[INFO ] [Python ] v3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
[INFO ] [Factory ] 194 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[INFO ] [OSC ] using <thread> for socket
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] GLEW initialization succeeded
[INFO ] [GL ] Backend used <glew>
[INFO ] [GL ] OpenGL version <b'4.4.0 - Build 21.20.16.4627'>
[INFO ] [GL ] OpenGL vendor <b'Intel'>
[INFO ] [GL ] OpenGL renderer <b'Intel(R) HD Graphics 520'>
[INFO ] [GL ] OpenGL parsed version: 4, 4
[INFO ] [GL ] Shading version <b'4.40 - Build 21.20.16.4627'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <32>
[INFO ] [Shader ] fragment shader: <b"WARNING: 0:7: '' : #version directive missing">
[INFO ] [Shader ] vertex shader: <b"WARNING: 0:7: '' : #version directive missing">
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [GL ] NPOT texture support is available
[INFO ] [Base ] Start application main loop
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[WARNING] [Accordion ] not enough space for displaying all children
[WARNING] [Accordion ] need 176px, got 100px
[WARNING] [Accordion ] layout aborted.
[INFO ] [Base ] Leaving application in progress...
Traceback (most recent call last):
File "E:\SharanyaPy\desktop vocabulary journal\vocabJournal.py", line 92, in <module>
object.run()
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\app.py", line 828, in run
runTouchApp()
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\base.py", line 504, in runTouchApp
EventLoop.window.mainloop()
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\window_sdl2.py", line 663, in mainloop
self._mainloop()
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\window_sdl2.py", line 405, in _mainloop
EventLoop.idle()
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\base.py", line 342, in idle
self.dispatch_input()
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\base.py", line 327, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\base.py", line 233, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\__init__.py", line 1188, in on_motion
self.dispatch('on_touch_down', me)
File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\__init__.py", line 1204, in on_touch_down
if w.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\screenmanager.py", line 1189, in on_touch_down
return super(ScreenManager, self).on_touch_down(touch)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\widget.py", line 457, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\relativelayout.py", line 288, in on_touch_down
ret = super(RelativeLayout, self).on_touch_down(touch)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\widget.py", line 457, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\widget.py", line 457, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
self.dispatch('on_press')
File "kivy\_event.pyx", line 714, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8146)
File "kivy\_event.pyx", line 1225, in kivy._event.EventObservers.dispatch (kivy\_event.c:14035)
File "kivy\_event.pyx", line 1109, in kivy._event.EventObservers._dispatch (kivy\_event.c:12816)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\lang\builder.py", line 64, in custom_callback
exec(__kvlang__.co_value, idmap)
File "E:\SharanyaPy\desktop vocabulary journal\VocabularyJournal.kv", line 51, in <module>
on_press: root.manager.current='display'
File "kivy\properties.pyx", line 478, in kivy.properties.Property.__set__ (kivy\properties.c:5572)
File "kivy\properties.pyx", line 516, in kivy.properties.Property.set (kivy\properties.c:6405)
File "kivy\properties.pyx", line 571, in kivy.properties.Property.dispatch (kivy\properties.c:7105)
File "kivy\_event.pyx", line 1225, in kivy._event.EventObservers.dispatch (kivy\_event.c:14035)
File "kivy\_event.pyx", line 1131, in kivy._event.EventObservers._dispatch (kivy\_event.c:13193)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\screenmanager.py", line 1037, in on_current
screen = self.get_screen(value)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\screenmanager.py", line 1063, in get_screen
raise ScreenManagerException('No Screen with name "%s".' % name)
kivy.uix.screenmanager.ScreenManagerException: No Screen with name "display".
答案 0 :(得分:1)
在DisplayPage
课程中,您正在覆盖__init__
方法,因此您需要调用父__init__
类的Screen
方法。使用BoxLayout.__ init __ (self, * args)
,您只需调用BoxLayout
的构造函数,而不是Screen
的构造函数。我不明白为什么你尝试在这里做一个多子类,你可以简单地从Screen
继承并调用它的构造函数(最好使用super
)。
Screen
是RelativeLayout
,如果您需要在屏幕中使用其他布局,只需将其添加为任何小部件(小心某些peculiarities):
<MenuPage>:
BoxLayout:
# Rest of screen's content here
您可以执行以下操作:
<强> main.py:强>
import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
import json
Builder.load_file('VocabularyJournal.kv')
class MenuPage(Screen):
pass
class DisplayPage(Screen):
def __init__(self, **kwargs):
# Call __init__ method of parent class (Screen)
super(DisplayPage, self).__init__(**kwargs)
with open('vocab_words.json') as rfile:
data = json.load(rfile)
class WordInsertPage(Screen):
pass
class NewWordPage(Screen):
pass
class FlashCard(Screen):
pass
class WordGroups(Screen):
pass
class Manager(ScreenManager):
pass
class VocabularyJournalApp(App):
def build(self):
return Manager()
if __name__ == "__main__":
object = VocabularyJournalApp()
object.run()
VocabularyJournal.kv:
<Manager>:
MenuPage:
name: 'menu'
WordInsertPage:
name: 'insertword'
NewWordPage:
name: 'newword'
FlashCard:
name: 'flashcard'
WordGroups:
name: 'wordgroup'
DisplayPage:
name: 'display'
<MenuPage>:
# Your code here
<WordInsertPage>:
FloatLayout:
Button:
text: "New Word"
on_press: root.manager.current='newword'
font_size: 30
color: 0,0,0,1
size_hint: .2, .1
pos_hint: {"center_x": .5, "center_y": 0.3}
Button:
text: "search word"
on_press: root.manager.current='display'
font_size: 30
color: 0,0,0,1
size_hint: .2, .1
pos_hint: {"center_x": .5, "center_y": 0.5}
Button:
text: 'Flash Cards'
on_press: root.manager.current="flashcard"
font_size: 30
color: 0,0,0,1
size_hint: .2, .1
pos_hint: {"center_x": .5, "center_y": 0.7}
<NewWordPage>:
# Your code here
<FlashCard>:
# Your code here
<WordGroups>:
# Your code here
<DisplayPage>:
# Your code here
注意:如果您将kv重命名为
vocabularyjournal.kv
,则无需使用Builder.load_file
:https://kivy.org/docs/guide/lang.html#how-to-load-kv