Below is the whole (test) app written with Kivy. This is something like app preview application: user enters a text of kv markup (see variable self.kv) and a text of classes (see variable self.text). Then he clicks the "preview" button and sees the result on the right side of the application. Loading kv is implemented using kivy Builder.load_string(). Class loading is implemented using exec(, globals()).
The main problem is that for some reason I get the following error when I click on the preview button for the third time (the first 2 clicks work without errors):
TypeError: super(type, obj): obj must be an instance or subtype of type
The error can be because of exec(), (without exec I don’t get this error).
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
KV = '''
BoxLayout:
BoxLayout:
orientation: 'vertical'
CodeEd
id: code_editor
Button:
text: 'Preview'
on_release: app.preview()
Preview:
id: preview_area
<CodeEd@TextInput>
text: app.text
<Preview@RelativeLayout>
'''
class MyApp(App):
def build(self):
self._kv_filename = 'KvEditor_internal.' + str(self.uid)
self.text = '''
class MyButton(Button):
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
print (333)
super(MyButton, self).on_touch_down(touch)
'''
self.kv = 'MyButton'
self.root = Builder.load_string(KV)
def preview(self):
preview_area = self.root.ids.preview_area
#if 'MyButton' in globals():
# del globals()['MyButton']
#print ('===================')
#print ([i for i in dict(globals())])
try:
exec(self.text, globals())
except:
print ('some error when exec class ')
Builder.unload_file(self._kv_filename)
try:
preview_area.add_widget(Builder.load_string(self.kv, filename=self._kv_filename))
except Exception as e:
print (e.message if getattr(e, r"message", None) else str(e))
MyApp().run()
How to solve this problem?
from TypeError: super(type, obj) when using exec()
No comments:
Post a Comment