英文:
kivy - self not defined
问题
这是你提供的Python程序的翻译:
我正试图运行来自一本名为《Python完全手册》的Python书籍中的这个Python程序,然而说明是针对Python 2.7编写的,而我正在运行Python 3.5。
这个程序在`kivy`环境中运行,因为我正在使用它来创建一个应用程序。我当前遇到的问题是,当我运行程序时,会收到一个错误消息,上面写着`'self'未定义`。我已经尝试更改语法,移动函数(因为我认为它必须按特定顺序读取函数),甚至完全删除整行。无论我如何尝试,我仍然得到相同的`self`未定义错误。
这是代码以及错误消息:
#!/usr/bin/env python
import kivy
from kivy.core.window import Window
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.properties import ListProperty
class GridEntry(Button):
coords = ListProperty([0, 0])
class TicTacToeApp(App):
pass
def build(self):
return TicTacToeGrid()
class TicTacToeGrid(GridLayout):
def __init__(self, *args, **kwargs):
super(TicTacToeGrid, self).__init__(*args, **kwargs)
def button_pressed(self, instance):
print('{} 按钮被点击!'.format(instance.coords))
for row in range(3):
for column in range(3):
grid_entry = GridEntry(coords=(row, column))
grid_entry.bind(on_release=self.button_pressed)
self.add_widget(grid_entry)
if __name__ == "__main__":
TicTacToeApp().run()
输出:
(kivy_venv)root@c0mputer:/home/chavez/Documents/Python# ./ticTacToe.py
[INFO] [Logger] 在/root/.kivy/logs/kivy_20-01-06_1.txt中记录日志
[INFO] [Kivy] v1.11.1
[INFO] [Kivy] 安装在"/home/chavez/Documents/Python/kivy_venv/lib/python3.5/site-packages/kivy/__init__.py"
[INFO] [Python] v3.5.2 (默认值,2019年10月8日,13:06:37)
[INFO] [Python] 解释器位于"/home/chavez/Documents/Python/kivy_venv/bin/python"
[INFO] [Logger] 清除日志已触发。分析中...
[INFO] [Logger] 清除完成!
[INFO] [Factory] 加载了184个符号
[INFO] [Image] 提供者:img_tex、img_dds、img_sdl2、img_gif(img_pil、img_ffpyplayer被忽略)
[INFO] [Window] 提供者:sdl2(忽略了'window_egl_rpi')
[INFO] [GL] 使用"OpenGL"图形系统
[INFO] [GL] 使用的后端是<sdl2>
[INFO] [GL] OpenGL版本<b'3.0 Mesa 18.0.5'>
[INFO] [GL] OpenGL供应商<b'Intel开源技术中心'>
[INFO] [GL] OpenGL渲染器<b'Mesa DRI Intel(R) HD Graphics 520(Skylake GT2)'>
[INFO] [GL] 解析的OpenGL版本:3, 0
[INFO] [GL] 着色版本<b'1.30'>
[INFO] [GL] 纹理最大尺寸<16384>
[INFO] [GL] 纹理最大单元<32>
[INFO] [Window] 自动添加sdl2输入提供者
[INFO] [Window] 不允许虚拟键盘,单一模式,未停靠
[INFO] [Text] 提供者:sdl2
回溯(最近的调用最后一个):
File "./ticTacToe.py", line 23, in <module>
class TicTacToeGrid(GridLayout):
File "./ticTacToe.py", line 35, in TicTacToeGrid
grid_entry.bind(on_release=self.button_pressed)
NameError: name 'self' is not defined
希望这有助于解决问题。如果需要进一步的帮助,请告诉我。
英文:
I am trying to run this Python program from a Python book that I've picked up named "Python The Complete Manual", however the instructions read for Python 2.7 and I am running Python 3.5.
This program runs in the kivy
environment, as I am using it to create an app. The issue that I'm currently having is that when I run the program, I get an error message that says 'self' is not defined
. I've already tried changing the syntax, moving the functions(because I thought that it would have to read the functions in a specified order), and even removed the whole line completely. I still get the same self
not defined error no matter how I try.
Here is the code along with the error message:
#!/usr/bin/env python
import kivy
from kivy.core.window import Window
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.properties import ListProperty
class GridEntry(Button):
coords = ListProperty([0, 0])
class TicTacToeApp(App):
pass
def build(self):
return TicTacToeGrid()
class TicTacToeGrid(GridLayout):
def __init__(self, *args, **kwargs):
super(TicTacToeGrid, self).__init__(*args, **kwargs)
def button_pressed(self, instance):
print('{} button clicked!'.format(instance.coords))
for row in range(3):
for column in range(3):
grid_entry = GridEntry(coords=(row, column))
grid_entry.bind(on_release=self.button_pressed)
self.add_widget(grid_entry)
if __name__ == "__main__":
TicTacToeApp().run()
Output:
(kivy_venv) root@c0mputer:/home/chavez/Documents/Python# ./ticTacToe.py
[INFO ] [Logger ] Record log in /root/.kivy/logs/kivy_20-01-06_1.txt
[INFO ] [Kivy ] v1.11.1
[INFO ] [Kivy ] Installed at "/home/chavez/Documents/Python/kivy_venv/lib/python3.5/site-packages/kivy/__init__.py"
[INFO ] [Python ] v3.5.2 (default, Oct 8 2019, 13:06:37)
[GCC 5.4.0 20160609]
[INFO ] [Python ] Interpreter at "/home/chavez/Documents/Python/kivy_venv/bin/python"
[INFO ] [Logger ] Purge log fired. Analysing...
[INFO ] [Logger ] Purge finished!
[INFO ] [Factory ] 184 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
[INFO ] [Window ] Provider: sdl2(['window_egl_rpi'] ignored)
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] Backend used <sdl2>
[INFO ] [GL ] OpenGL version <b'3.0 Mesa 18.0.5'>
[INFO ] [GL ] OpenGL vendor <b'Intel Open Source Technology Center'>
[INFO ] [GL ] OpenGL renderer <b'Mesa DRI Intel(R) HD Graphics 520 (Skylake GT2) '>
[INFO ] [GL ] OpenGL parsed version: 3, 0
[INFO ] [GL ] Shading version <b'1.30'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <32>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [Text ] Provider: sdl2
Traceback (most recent call last):
File "./ticTacToe.py", line 23, in <module>
class TicTacToeGrid(GridLayout):
File "./ticTacToe.py", line 35, in TicTacToeGrid
grid_entry.bind(on_release=self.button_pressed)
NameError: name 'self' is not defined
答案1
得分: 1
#!/usr/bin/env python
import kivy
from kivy.core.window import Window
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.properties import ListProperty
class GridEntry(Button):
coords = ListProperty([0, 0])
class TicTacToeApp(App):
def build(self):
return TicTacToeGrid()
class TicTacToeGrid(GridLayout):
def __init__(self, *args, **kwargs):
super(TicTacToeGrid, self).__init__(*args, **kwargs)
for row in range(3):
for column in range(3):
grid_entry = GridEntry(coords=(row, column))
grid_entry.bind(on_release=self.button_pressed)
self.add_widget(grid_entry)
def button_pressed(self, instance):
print('{} button clicked!'.format(instance.coords))
if __name__ == "__main__":
TicTacToeApp().run()
英文:
I am new to this forum(only having viewed questions and answers and not actually signing up to the site). Thank you @Error -Syntactical Remorse, that was the correct answer I was looking for. Be it simple but I am fairly new to Python and haven't even heard of kivy
until I started reading this book. Also, another thing that I do not like about getting the right answer is that the person that posts the question and gets the right answer almost never posts his/her/their code the way it was answered correctly. I just usually see, "Oh! that solved it!"... so, I will post where the correction is here:
#!/usr/bin/env python
import kivy
from kivy.core.window import Window
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.properties import ListProperty
class GridEntry(Button):
coords = ListProperty([0, 0])
class TicTacToeApp(App):
def build(self):
return TicTacToeGrid()
class TicTacToeGrid(GridLayout):
def __init__(self, *args, **kwargs):
super(TicTacToeGrid, self).__init__(*args, **kwargs)
for row in range(3):
for column in range(3):
grid_entry = GridEntry(coords=(row, column))
grid_entry.bind(on_release=self.button_pressed)
self.add_widget(grid_entry)
def button_pressed(self, instance):
print('{} button clicked!'.format(instance.coords))
if __name__ == "__main__":
TicTacToeApp().run()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论