Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Pyui button image options
-
How do you give a button a custom icon within the UI Designer? In the attribute menu, under “image”, the only options accessible are the icons within the ionicons Black folder. i have tried more things than I can remember to get this working but have been unsuccessful. Is there a way to give a button a custom Icon using an image that you have added to the project folder.
-
instead of "EAR" you can use "image000.jpg" or whatever
-
I’m not exactly sure what you mean, however, I will list a few of the many things I have tried in the attempt to get this to work.
Adding {image_name : image.png} to the buttons Custom Attributes with & without “”.Changing the file format of the .pyui file to .json & editing the image_name : to the custom image.png.
Editing the py file & adding button.background_image = ui.Image.named('filePath/image.PNG')\
With just these three things I have tried every variation I can think of along with MAAAAAAAANY others. I found that I was able to get the image to show up as a button by editing a .py file manually but my goal is to do this using the .pyui file & the UI Design tool. I would like to be able to create the layout of my ui with the design to because positioning every thing manually is just excruciating.
Anyway...Thanks in advance for any help that can be given.
-
@Sabarblatoe example of custom attributes in UI designer:
{'background_image':ui.Image.named('IMG_6066.PNG')}
Or
{'image':ui.Image.named('IMG_6066.PNG'). with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)}
But you only will see the effect when you run your script, not at design time
-
Hey thanks for taking the time to reply. Unfortunately both examples were included in the plethora of things I have tried and I figured there was a good chance any custom changes may not show up in the designer . I literally spent an entire day googling & reading and tried everything I could find along with slight variations and combinations.
-
Ok...so I decided to delete & reinstall the app & boom. It works now so something must have just been missing. You do have to add the last part included in the second example but it works perfectly. Thanks again for your help.
-
@Sabarblatoe Even so, I don't see the image in the UI designer, only when I run the script.
I did not reinstall the app. -
Yes, you are correct. It does not show up in the designer & only when you run the program. Mine, however, was not working as expected. This is likely due to something not being installed correctly during my initial download of the app. Perhaps a crappy internet connection or my phone just being an ass, we will never know. After reinstalling the app the problem was fixed & the app was working as expected. You’re app, unlike mine, was working properly in the first place & thus would not require a reinstall. It was your initial post that prompted me to think the problem may be on my end seeing as how, during my research of this topic, I came across the same method you discribed multiple times. So again, thanks.
-
@Sabarblatoe, not sure you are interested or not. Below is some code that reads a pyui/UIFile into a class and class behaves as if it is a Custom ui View. I think its handy for many reasons and it can make life a little easier (Maybe not in all cases). But in the demo below you can see you can code directly against the view rather than the pyui file being a subview of another view. This has been posted on the forum many times before.
Anyway, maybe its interesting to you.@omz do you have any plans to add this functionality directly into API? I know I have asked before. But I still believe this would be a great addition to the API. It would also then be documented, I think a lot of ppl would use this approach to loading UIFiles if it was documented and built in.
import ui class PYUILoader(ui.View): # this acts as a normal Custom ui.View class # the root view of the class is the pyui file read in # from @JonB def WrapInstance(obj): class Wrapper(obj.__class__): def __new__(cls): return obj return Wrapper def __init__(self, pyui_fn, *args, **kwargs): bindings = globals().copy() bindings[self.__class__.__name__] = self.WrapInstance() ui.load_view(pyui_fn, bindings) # call after so our kwargs modify attrs super().__init__(*args, **kwargs) class MyClass(PYUILoader): ''' This class acts the same as a ui Custom View would even though its been loaded in from a pyui file. *** NOTE *** You have to do 2 things for this to work correctly. 1. pass the file name of your .pyui file when creating the class 2. in the ui Designer, you need to set the 'Custom View Class' field in the Inspector to the name of this class's name. In this case 'MyClass' - Finding the 'Custom View Class' in the ui Designer Make sure you are at the root level, i.e not in a subview In the ui Designer, tap anywhere to make sure nothing is selected, Then you will see the 'Size Presets' in the Inspector. Directly below you will see the 'Custom View Class' field. ''' def __init__(self, pyui_fn, *args, **kwargs): super().__init__(pyui_fn, *args, **kwargs) self.update_interval = 1 def did_load(self): print('view loaded') def layout(self): print('in layout') def update(self): print('update fired') if __name__ == '__main__': f = (0, 0, 480, 600) v = MyClass('MyUIFile.pyui', frame=f) v.present(style='sheet')
-
In the example above, if you say want to set the action attr of a ui.Button in the ui Designer, you cant refer to self to access a method in your class. In the case of the example above, the class is named MyClass, so if you had a method in MyClass called hit, in the ui Designer in the action property of the btn you would enter MyClass.hit, not self.hit.
If your action is a func in the same source file you would do as normal, just enter the name of the func.I cant remember if it was always like this. It's possible that @omz made some changes to prevent injection attacks. I thought before that self.method worked. But maybe I am dreaming.
Regardless many things can be done with pyui files. It's worth searching the forum.
-
@Phuket2
Hey thanks. I will definitely check that out. I really appreciate it. If it works the way I think it does then yes, it could be VERY helpfull. Thanks again. -
@Sabarblatoe , no problems. There seems to be a few little quirks with this way that I dont remember from before. But I think its still really nice. But I think it will work as you expect. Once you have returned from the super() call, its the same as if you have created a Custom ui class by hand.
-
I am using Pythonista on my iPhone 11
And I am writing this
super().init( *args, **kwargs)
However it still tells me super is not defined -
To write code in a post to this forum, please follow https://guides.github.com/features/mastering-markdown
So,
```python
My code goes here...
```If not then __init__ becomes init.
If you are not inside a class definition then
super()
is not defined.