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

问题描述

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

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",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",in __init__
     self.a['title'] = 'Result'
 TypeError: 'nonetype' object does not support item assignment

如何修复错误

解决方法

代码:

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,**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 的一个属性。