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.
Making arcs and filling them with in ui.Path
-
@omz , thanks. Just gives different options for design. To inherit from a class and override draw or to point to a function or class that will draw
-
@omz , oh! Just found out I can not use another custom view to embed my draw method in, which makes sense. But back to the first question then. Is there a way to get/set the ImageContext object. I can see I can return a ImageContext but would be nicer if I can just explicitly set or use an existing ImageContext
Equivalent of
MyImageContext = v.xxx
With MyImageContext as current:
Drawing cmds going to this view -
oh! Just found out I can not use another custom view to embed my draw method in, which makes sense.
It should be possible to do that. What does your code look like?
Is there a way to get/set the ImageContext object.
Not really. An
ImageContext
is basically just a way to redirect drawing code into an image (for example, to save a drawing as a file). You don't really need anImageContext
for drawing in custom views. Views have a drawing context of their own, but it doesn't correspond to anImageContext
object, and you can't really access it (nor should you actually need to).When the
draw()
method (of a custom view) is called (by the system), a drawing context is already set up, and all drawing functions affect the current context. So it's perfectly fine to do something like this:# ... def draw(self): SomeOtherViewClass.draw(self)
to use the drawing functionality of a different view class.
-
@omz , thanks. You are right it does work. Not worth posting the code now, it's all over the place. Trying to work around many issues. But this will help simply a lot. Thanks again 😱
-
-
@omz The default
ui.View
doesn't seem to have adraw
method. I assume this is special-cased internally to not cause errors, but it would be nice to have a defaultdraw
method (even if empty) so we can always usesuper
in thedraw
method.For example, this code currently fails, because
ui.View
doesn't have adraw
method:import ui class CustomView(ui.View): def draw(self): super().draw() ui.Path.rect(5, 5, 45, 45).fill()
But if we'd write a subclass of
CustomView
, we would need to callsuper().draw()
, otherwise the custom drawing code inCustomView.draw
wouldn't be executed.This isn't a big issue - currently you just have to leave out the
super
call when the base class isui.View
- but I am a fan of consistency.:)
-
@dgelessus , I am not sure I follow you 100% here. But in this case wouldn't you just do something like
If v.superview:
Do something...I know your statement was directed at @omz , but just curious
-
@Phuket2 I'm not talking about superviews of a view, but about the superclass (base class) of a custom view class. What I mean is that currently when you subclass
ui.View
directly, you cannot callsuper().draw()
, but when you subclass another custom view class (that has a customdraw
method), you have to callsuper().draw()
. Ifui.View
had an emptydraw
method, you could always callsuper().draw()
no matter what. -
@dgelessus , I don't have a fraction of your knowledge. I knew you were not talking about superviews directly. I only mention that, because what I understand is if there is not superview it's the root view object.
But I have read some docs on Python objects. I can't remember exactly what it says, but I think it's something like there is always a base class. I think it said something like that.
So, I guess you are saying when super is called on the base class it should point to itself , just to be consistent and compatible will other Python patterns.
Hmmm, I am already regretting this post. I am in the deep end of the pool and need floaties 😱
Look if I am so wrong, it's ok. No need to waste your time to respond. -
Yes, if you write a class and don't set any superclass(es) yourself, its superclass is automatically
object
. (In Python 3 at least - the situation is more complicated in Python 2.)This isn't really important here though. I mean that
ui.View
should have adraw
method, so that subclasses can usesuper().draw()
in their owndraw
methods. (Currently this fails sometimes becauseui.View.draw
doesn't exist.) -
@dgelessus , ok I think I see. Actually draw also does not exist in a custom class unless you have a draw method. I was just doing some print dir(obj) to understand your post.
If you print dir(custom_class) without a draw method defined it does not show up. So I guess it's just added at runtime as required. Not sure that's normal or not. I would have thought the method would be there, but not the ImageContext etc...not sure if it's normal or not . But seems like maybe @omz took a logic short cut 😱😈😈😈Sure I will regret this post also, I am probably way off course. I will learn to keep my big mouth shut one day.