Pyui button image options
cvp last edited by cvp
@Sabarblatoe example of custom attributes in UI designer:
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.
cvp last edited by cvp
@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.
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.
scarlett last edited by
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
ccc last edited by ccc
To write code in a post to this forum, please follow https://guides.github.com/features/mastering-markdown
My code goes here...
If not then __init__ becomes init.
If you are not inside a class definition then
super()is not defined.