KIVY 的 super() 类中的类型错误

Posted

技术标签:

【中文标题】KIVY 的 super() 类中的类型错误【英文标题】:Type Error In The Class in The super() in KIVY 【发布时间】:2021-04-20 15:20:28 【问题描述】:

我试图创建一个弹出窗口,其中显示参数中给出的值。 我的代码是:

from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout

#Import widgets
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.popup import Popup

#Importing the clipboard
from kivy.core.clipboard import Clipboard

'''
The popup that shows the encoded or the decoded text, with a copy and paste button
'''
class Ans_pop(Popup):
    def __init__(self, text,**kwargs):
        self.a = super(Ans_pop, self).__init__(**kwargs)

        #The title of the popup
        self.a['title'] = 'Result'

        #The main layout that is shown on the popup
        #Note* Only one layout can be added in popup
        self.box = BoxLayout(orientation='vertical')

        #The text to be displayed
        self.result = text

        #The label that displays the text to be displayed
        self.res_lab = Label(text = self.result, font_size = 50)

        #The boxLayout that displayes the copy and paste button on the boxLayout
        self.a3 = BoxLayout(orientation='horizontal')
        def reposition_b3(a3, *args):
            self.btn_3.pos = self.a3.x, self.a3.y
            self.btn_3.size = self.a3.width, self.a3.height/2

        #The copy button:
        self.btn_3 = Copy_Btn(text, text = 'Copy', size = (self.a3.width/2, self.a3.height), pos=(self.a3.x, self.a3.y))

        #The resize of the button
        self.a3.bind(on_size=reposition_b3, on_pos=reposition_b3)

        #The container of the buttons:
        self.a3.add_widget(btn_3)

        #The label and the container of labels in the main boxcontainer
        self.box.add_widget(self.res_lib)
        self.box.add_widget(self.a3)


        ###  ADD THE MAIN BOX CONTAINER IN THE POPUP WINDOW  ##
        self.a.content = self.box

'''
The copy button feature, that appends the text in the instance to the clipboard
'''
class Copy_Btn(Button):
    def __init__(self, instance, **kwargs):
        self.a = super(Copy_Btn, self).__init__(**kwargs)
        self.instance = instance
        
    def on_release(self):
        Clipboard.copy(self.instance)

class APP(App):
    def build(self):
        a = Ans_pop('This is the text in popup')
        a.show()
        return a

出于某种原因,我不想显示您的某些代码。请原谅我! 但别担心,因为运行弹出窗口给定的代码就足够了! 当我运行这段代码时:

[INFO   ] [Logger      ] Record log in C:\Users\new\.kivy\logs\kivy_21-01-15_101.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.3.1
[INFO   ] [Kivy        ] v2.0.0
[INFO   ] [Kivy        ] Installed at "C:\Users\new\AppData\Local\Programs\Python\Python39-32\lib\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:23:07) [MSC v.1927 32 bit (Intel)]
[INFO   ] [Python      ] Interpreter at "C:\Users\new\AppData\Local\Programs\Python\Python39-32\pythonw.exe"
[INFO   ] [Factory     ] 186 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[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.5.13474 Compatibility Profile Context 22.19.162.4'>
[INFO   ] [GL          ] OpenGL vendor <b'ATI Technologies Inc.'>
[INFO   ] [GL          ] OpenGL renderer <b'AMD Radeon(TM) R4 Graphics'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 5
[INFO   ] [GL          ] Shading version <b'4.50'>
[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   ] [Clipboard   ] Provider: winctypes
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
Traceback (most recent call last):
   File "F:\Python tries#1\Simple Cryptography (Kivy)\Cryptography App.py", line 197, in <module>
     Crypto().run()
   File "C:\Users\new\AppData\Local\Programs\Python\Python39-32\lib\site-packages\kivy\app.py", line 950, in run
     runTouchApp()
   File "C:\Users\new\AppData\Local\Programs\Python\Python39-32\lib\site-packages\kivy\base.py", line 582, in runTouchApp
     EventLoop.mainloop()
   File "C:\Users\new\AppData\Local\Programs\Python\Python39-32\lib\site-packages\kivy\base.py", line 347, in mainloop
     self.idle()
   File "C:\Users\new\AppData\Local\Programs\Python\Python39-32\lib\site-packages\kivy\base.py", line 391, in idle
     self.dispatch_input()
   File "C:\Users\new\AppData\Local\Programs\Python\Python39-32\lib\site-packages\kivy\base.py", line 342, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Users\new\AppData\Local\Programs\Python\Python39-32\lib\site-packages\kivy\base.py", line 308, in post_dispatch_input
     wid.dispatch('on_touch_up', me)
   File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\new\AppData\Local\Programs\Python\Python39-32\lib\site-packages\kivy\uix\behaviors\button.py", line 179, in on_touch_up
     self.dispatch('on_release')
   File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "F:\Python tries#1\Simple Cryptography (Kivy)\Cryptography App.py", line 95, in on_release
     Ans_pop(encoded_text)
   File "F:\Python tries#1\Simple Cryptography (Kivy)\Cryptography App.py", line 38, in __init__
     self.a['title'] = 'Result'
 TypeError: 'NoneType' object does not support item assignment

在此突出显示错误是:

   File "F:\Python tries#1\Simple Cryptography (Kivy)\Cryptography App.py", line 38, in __init__
     self.a['title'] = 'Result'
 TypeError: 'NoneType' object does not support item assignment

如何修复错误?

【问题讨论】:

【参考方案1】:

代码:

class Ans_pop(Popup):
    def __init__(self, text,**kwargs):
        self.a = super(Ans_pop, self).__init__(**kwargs)

        #The title of the popup
        self.a['title'] = 'Result'

应改为:

class Ans_pop(Popup):
    def __init__(self, text,**kwargs):
        super(Ans_pop, self).__init__(**kwargs)

        #The title of the popup
        self.title = 'Result'

__init__() 方法返回None,导致您的错误。而title 只是Popup 的一个属性。由于您的Ans_pop 扩展了Popup,因此title 只是Ans_pop 的一个属性。

【讨论】:

以上是关于KIVY 的 super() 类中的类型错误的主要内容,如果未能解决你的问题,请参考以下文章

类型错误:没有名为“kivy._clock”的模块

Java——super关键字

java中的super()是啥

java super关键字

重写super关键字final关键字多态子类型转换抽象的初步了解

在 Kivy.kv 文件中,我如何引用另一个类中的方法