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.
File where the speech is saved
-
@Python567 try
import ui import datetime class MyView(ui.View): def __init__(self): self.frame = (0,0,400,400) self.background_color = 'white' b = ui.Button(name='b') b.border_width = 1 b.border_color = 'blue' b.frame = (10,10,250,20) self.add_subview(b) self.update_interval = 1 def update(self): self['b'].title = str(datetime.datetime.now()) v = MyView() v.present('sheet')
-
@Python567 said:
is it normal that my Pythonista crash sometimes?
It can be normal if you script uses a lot of memory and you re-run it several times without clearing it.
It happens frequently that I have to kill Pythonista and restart it when I use a big script or a script using a lot of images, by example. -
@Python567 other example, try it
import ui import datetime from math import pi,cos,sin class ClockButton(ui.View): def __init__(self, seconds=True, *args, **kwargs): super().__init__(*args, **kwargs) self.seconds = seconds self.background_color = 'white' b = ui.Button(name='clock') b.frame = (0,0,self.width,self.height) b.corner_radius = self.width/2 b.border_color = 'black' b.border_width = 1 b.bg_color = 'lightgray' b.title = '' b.tint_color = 'red' b.background_image = None self.add_subview(b) self.update_interval = 1 if self.seconds else 60 def update(self): t = datetime.datetime.utcnow() tick = -2 * pi / 60.0 seconds = t.second + t.microsecond/1000000.0 minutes = t.minute + seconds/60.0 hours = (t.hour % 12) + minutes/60.0 r = self.width/2 with ui.ImageContext(r * 2, r * 2) as ctx: pthc = ui.Path.oval(r*0.9,r*0.9,r*0.2,r*0.2) ui.set_color('black') pthc.fill() pth = ui.Path() ui.set_color('black') pth.line_width = 2 # hours lh = 0.6 # length of hours hand ah = -pi/2 + hours*(2*pi/12) # angle of hours hand pth.move_to(r,r) pth.line_to(r*(1+lh*cos(ah)),r*(1+lh*sin(ah))) # minutes lm = 0.9 # length of minutes hand am = -pi/2 + minutes*(2*pi/60) # angle of minutes hand pth.move_to(r,r) pth.line_to(r*(1+lm*cos(am)),r*(1+lm*sin(am))) pth.stroke() # seconds if self.seconds: lt = 0.9 # length of seconds hand at = -pi/2 + seconds*(2*pi/60) # angle of seconds hand pths = ui.Path() ui.set_color('red') pths.move_to(r,r) pths.line_to(r*(1+lt*cos(at)),r*(1+lt*sin(at))) pths.stroke() ui_image = ctx.get_image() self['clock'].background_image = ui_image v = ui.View() v.background_color = 'white' v.frame = (0,0,400,400) clock = ClockButton(frame=(10,10,32,32)) v.add_subview(clock) v.present('sheet')
-
@cvp wow, the second example is better ๐. And if I want the numbers, where can I add them?
-
-
@cvp yes
-
@Python567 wait....
-
@Python567 try
import ui import datetime from math import pi,cos,sin class ClockButton(ui.View): def __init__(self, r, seconds=True, *args, **kwargs): super().__init__(*args, **kwargs) self.r = r self.seconds = seconds self.background_color = 'white' self.frame = (0,0,self.r*2,self.r*2) b = ui.Button(name='clock') b.frame = (0,0,self.width,self.height) b.corner_radius = self.width/2 b.border_color = 'black' b.border_width = 1 b.bg_color = 'lightgray' b.title = '' b.tint_color = 'red' b.background_image = None self.add_subview(b) self.update_interval = 1 if self.seconds else 60 with ui.ImageContext(self.r * 2, self.r * 2) as ctx: pthc = ui.Path.oval(self.r*0.9,self.r*0.9,self.r*0.2,self.r*0.2) ui.set_color('black') pthc.fill() h = 10 for i in range(12): a = pi/2 - 2 * pi * i/12.0 x,y = self.r+cos(a)*(self.r*0.85)-h/2, self.r-sin(a)*(self.r*0.85)-h/2 ui.draw_string(str(i),rect=(x,y,20,h),font=('Menlo',h)) self.back = ctx.get_image() def update(self): t = datetime.datetime.utcnow() tick = -2 * pi / 60.0 seconds = t.second + t.microsecond/1000000.0 minutes = t.minute + seconds/60.0 hours = (t.hour % 12) + minutes/60.0 with ui.ImageContext(self.r * 2, self.r * 2) as ctx: self.back.draw(0,0,self.r * 2, self.r * 2) pth = ui.Path() ui.set_color('black') pth.line_width = 2 # hours lh = 0.6 # length of hours hand ah = -pi/2 + hours*(2*pi/12) # angle of hours hand pth.move_to(self.r,self.r) pth.line_to(self.r*(1+lh*cos(ah)),self.r*(1+lh*sin(ah))) # minutes lm = 0.9 # length of minutes hand am = -pi/2 + minutes*(2*pi/60) # angle of minutes hand pth.move_to(self.r,self.r) pth.line_to(self.r*(1+lm*cos(am)),self.r*(1+lm*sin(am))) pth.stroke() # seconds if self.seconds: lt = 0.9 # length of seconds hand at = -pi/2 + seconds*(2*pi/60) # angle of seconds hand pths = ui.Path() ui.set_color('red') pths.move_to(self.r,self.r) pths.line_to(self.r*(1+lt*cos(at)),self.r*(1+lt*sin(at))) pths.stroke() ui_image = ctx.get_image() self['clock'].background_image = ui_image v = ui.View() v.background_color = 'white' v.frame = (0,0,400,400) clock = ClockButton(64,seconds=True) v.add_subview(clock) v.present('sheet')
-
@Python567 I've just updated the last script above to create fixed part of the clock (circle, digits) in the init def.
-
@cvp ok, but is it possible that itโs in your country 7:50?
-
@Python567 no, 08:50
-
Ok, because on my clock, the little cursor is on the 8
-
@Python567 sorry, replace utcnow by now
-
@cvp what must I replace to what?
Edit: I have it, thanks -
@Python567 if you want the date in your clock, replace the graphic part of the init by
with ui.ImageContext(self.r * 2, self.r * 2) as ctx: pthc = ui.Path.oval(self.r*0.9,self.r*0.9,self.r*0.2,self.r*0.2) pthc.fill() h = 12 w = 5*h t = f"{datetime.datetime.now():%m/%d/%y}" x = self.r - w/2 y = self.r - r/2 pthr = ui.Path.rect(x,y,w,h) ui.set_color('white') pthr.fill() ui.set_color('black') ui.draw_string(t,rect=(x,y,w,h),font=('Menlo',h)) for i in range(12): a = pi/2 - 2 * pi * i/12.0 x,y = self.r+cos(a)*(self.r*0.85)-h/2, self.r-sin(a)*(self.r*0.85)-h/2 ui.draw_string(str(i),rect=(x,y,2*h,h),font=('Menlo',h)) self.back = ctx.get_image()
-
And now, use your creativity to invent your own clock, path allow all what you imagine.
Try with a personal photo as background of the clock... -
@cvp yes I try, thanks for the start code
-
@Python567 and don't forget, this clock stays a button, you can set it's own action with this kind of lines added at end of init def.
b.action = self.action def action(self,sender): print('clock has been pressed')
-
@Python567 Nice, isn' it? I put the picture of my wife, only as example ๐
self.update_interval = 1 if self.seconds else 60 with ui.ImageContext(self.r * 2, self.r * 2) as ctx: ui.Image.named('test:Lenna').draw(0,0,self.r * 2, self.r * 2) pthc = ui.Path.oval(self.r*0.9,self.r*0.9,self.r*0.2,self.r*0.2)
-
@cvp yes really nice, only one thing, why do
tint_color = 'white'
don't work? (I would to change the color from the numbers and the from coursers)