“Kivy – self not defined”翻译为”Kivy – self未定义”。

huangapple go评论62阅读模式
英文:

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 &#39;self&#39; 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(&#39;{} button clicked!&#39;.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__ == &quot;__main__&quot;:
    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 &quot;/home/chavez/Documents/Python/kivy_venv/lib/python3.5/site-packages/kivy/__init__.py&quot;
    [INFO   ] [Python      ] v3.5.2 (default, Oct  8 2019, 13:06:37) 
    [GCC 5.4.0 20160609]
    [INFO   ] [Python      ] Interpreter at &quot;/home/chavez/Documents/Python/kivy_venv/bin/python&quot;
    [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([&#39;window_egl_rpi&#39;] ignored)
    [INFO   ] [GL          ] Using the &quot;OpenGL&quot; graphics system
    [INFO   ] [GL          ] Backend used &lt;sdl2&gt;
    [INFO   ] [GL          ] OpenGL version &lt;b&#39;3.0 Mesa 18.0.5&#39;&gt;
    [INFO   ] [GL          ] OpenGL vendor &lt;b&#39;Intel Open Source Technology Center&#39;&gt;
    [INFO   ] [GL          ] OpenGL renderer &lt;b&#39;Mesa DRI Intel(R) HD Graphics 520 (Skylake GT2) &#39;&gt;
    [INFO   ] [GL          ] OpenGL parsed version: 3, 0
    [INFO   ] [GL          ] Shading version &lt;b&#39;1.30&#39;&gt;
    [INFO   ] [GL          ] Texture max size &lt;16384&gt;
    [INFO   ] [GL          ] Texture max units &lt;32&gt;
    [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 &quot;./ticTacToe.py&quot;, line 23, in &lt;module&gt;
         class TicTacToeGrid(GridLayout):
       File &quot;./ticTacToe.py&quot;, line 35, in TicTacToeGrid
         grid_entry.bind(on_release=self.button_pressed)
     NameError: name &#39;self&#39; 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(&#39;{} button clicked!&#39;.format(instance.coords))

if __name__ == &quot;__main__&quot;:
    TicTacToeApp().run()

huangapple
  • 本文由 发表于 2020年1月6日 23:52:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定