我在类(class)上遇到问题,无论我做什么,总是会出现错误,请告诉我哪里错了。 我一直在尝试在第一个屏幕上绑定(bind)一个按钮,以便在按下该按钮时打开弹出窗口,但出现此错误:
AttributeError: 'str' object has no attribute 'bind'
File "C:\Users\Lara\Downloads\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\lang.py", line 2012, in _apply_rule
setattr(widget_set, key, value)
File "kivy\weakproxy.pyx", line 22, in kivy.weakproxy.WeakProxy.__setattr__ (kivy\weakproxy.c:1189)
File "kivy\properties.pyx", line 397, in kivy.properties.Property.__set__ (kivy\properties.c:4680)
File "kivy\properties.pyx", line 429, in kivy.properties.Property.set (kivy\properties.c:5203)
File "kivy\properties.pyx", line 484, in kivy.properties.Property.dispatch (kivy\properties.c:5852)
File "kivy\_event.pyx", line 1168, in kivy._event.EventObservers.dispatch (kivy\_event.c:12154)
File "kivy\_event.pyx", line 1074, in kivy._event.EventObservers._dispatch (kivy\_event.c:11451)
File "C:\Users\Lara\Downloads\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\uix\popup.py", line 190, in on_content
self._container.add_widget(value)
File "C:\Users\Lara\Downloads\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\uix\boxlayout.py", line 210, in add_widget
widget.bind(
.py文件的相关部分:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.image import Image
from kivy.graphics import Color, Rectangle
from kivy.uix.popup import Popup
from kivy.properties import NumericProperty, ReferenceListProperty
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.uix.popup import Popup
class InstructionsPopup(Popup):
pass
class StartScreen(Screen):
def show_instructions(self):
p = InstructionsPopup()
p.open()
class GameScreen(Screen):
pass
class RootScreen(ScreenManager):
pass
class Main2App(App):
def build(self):
self.load_kv("main2.kv")
return RootScreen()
if __name__ == "__main__":
Main2App().run()
和.kv文件:
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
<StartScreen>:
name: "start"
FloatLayout:
Button:
id: play
text: "Play!"
on_release: root.manager.current = 'game'
Button:
id: how
text: "How to play"
on_press: self.parent.parent.show_instructions()
<GameScreen>:
name: "game"
FloatLayout:
SnakeWidget:
InfoWidget:
Button:
id: menu
text: "Menu"
<InstructionsPopup>:
size_hint: 0.5, 0.5
content: "You start the game by ... "
title: "How to play"
<RootScreen>:
id: screen_manager
transition: FadeTransition()
StartScreen:
name: "start"
GameScreen:
name: "game"
我还尝试在 StartScreen
类中绑定(bind)按钮,如下所示:
btn = self.ids.how
btn.bind(on_press=ShowInstructions())
但它一直告诉我self
未定义。
有什么想法我还可以尝试或者我可能错在哪里?
最佳答案
您的 kivy 文件中有两个编码错误。
<StartScreen>:
name: "start"
BoxLayout:
#Change your layout here
# OR
#give pos or pos_hint or size_hint if you want to give floatlayout
Button:
id: play
text: "Play!"
on_release: root.manager.current = 'game'
Button:
id: how
text: "How to play"
on_press: root.show_instructions()
#First error was here
<InstructionsPopup>:
size_hint: 0.5, 0.5
title: "How to play"
#You add the content in the popup, this way
Button:
text: "dismiss"
on_release: root.dismiss()
关于python - 将按钮绑定(bind)到 .kv 文件中 .py 中的类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31241186/