import ui, sound
#sound.set_honors_silent_switch(False) #maybe for production
class ButtonWrapper(ui.View):
def touch_began(self, touch):
imageName = self.subviews[0].path
imageName = imageName[12:][:-7]
imageName = 'res/buttons/' + imageName +'on.png'
self.subviews[0].image = ui.Image.named(imageName).with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
self.subviews[0].path = imageName
sound.play_effect(self.subviews[0].name)
def touch_ended(self, touch):
imageName = self.subviews[0].path
imageName = imageName[12:][:-6]
imageName = 'res/buttons/' + imageName +'off.png'
self.subviews[0].image = ui.Image.named(imageName).with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
self.subviews[0].path = imageName
v = ui.load_view()
v.present(style = 'full_screen', orientations = ['portrait'],title_bar_color='#222222', hide_title_bar=True) #In order to exit swipe down with two fingers
So I have a Script, that is supposed to change a background picture of a button while it is pressed.
I have stored the pictures in "res/buttons/" where one is for example called "orangeoff.png" and one is called "orangeon.png". With the above code I can achieve what I want.
However when I add the Script to the homescreen to run it from there I get the following exception most of the time:
Traceback (most recent call last):
File "/private/var/mobile/Containers/Shared/AppGroup/C1A7CE9F-6A30-4CA8-9380-76FD1CACD07F/Pythonista3/Documents/main.py", line 19, in touch_ended
self.subviews[0].image = ui.Image.named(imageName).with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
AttributeError: 'NoneType' object has no attribute 'with_rendering_mode'
When I hit the back button on the left top, and basically reload the view, everything works as expected again. Does anybody have an idea, why this might happen, and how to prevent it?
For clarification, this is my .pyui file (the view above the button is only needed to prevent the touch effect):
[
{
"nodes" : [
{
"nodes" : [
{
"nodes" : [
],
"frame" : "{{0, 0}, {240, 247}}",
"class" : "View",
"attributes" : {
"frame" : "{{46, 2}, {100, 100}}",
"class" : "View",
"name" : "view1",
"uuid" : "02ECED66-9D85-4C76-94DC-0FBB760E6596"
},
"selected" : false
},
{
"nodes" : [
],
"frame" : "{{0, 0}, {240, 247}}",
"class" : "Button",
"attributes" : {
"action" : "",
"frame" : "{{10, 34}, {80, 32}}",
"title" : "",
"uuid" : "350C205C-A1B2-41CA-B6ED-EA120173BF4C",
"class" : "Button",
"custom_attributes" : "{'image':ui.Image.named('res\/buttons\/blueoff.png'). with_rendering_mode(ui.RENDERING_MODE_ORIGINAL), 'path': 'res\/buttons\/blueoff.png'}",
"font_size" : 15,
"name" : "res\/sounds\/intro.mp3"
},
"selected" : false
}
],
"frame" : "{{0, 0}, {240, 240}}",
"class" : "View",
"attributes" : {
"uuid" : "C5101DD7-0CBA-448A-8BC8-34F0CF0347B3",
"frame" : "{{110, 950}, {100, 100}}",
"class" : "View",
"name" : "view1",
"custom_class" : "ButtonWrapper"
},
"selected" : false
}
],
"frame" : "{{0, 0}, {240, 240}}",
"class" : "View",
"attributes" : {
"name" : "",
"enabled" : true,
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
},
"selected" : false
}
]