class IncrediblyCrudeClock(Label):
def update(self, *args):
self.text = time.asctime()
class TimeApp(App):
def build(self):
crudeclock = IncrediblyCrudeClock()
Clock.schedule_interval(crudeclock.update, 1)
return crudeclock
if __name__ == "__main__":
TimeApp().run()
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
# Create both screens. Please note the root.manager.current: this is how
# you can control the ScreenManager from kv. Each screen has by default a
# property manager that gives you the instance of the ScreenManager used.
Builder.load_string("""
#:import os os
<MenuScreen>:
BoxLayout:
Button:
text: 'Goto HVAC Controller'
on_press: root.manager.current = 'hvac'
Button:
text: 'Switch to Phone'
<HvacScreen>:
temperature: temperature
BoxLayout:
orientation: 'vertical'
Slider:
min: 0
max: 16
step: 1
orientation: 'horizontal'
Label:
text: 'temperature='+str(int(temperature.value))
Slider:
id: power
min: 0
max: 6
step: 1
orientation: 'horizontal'
Label:
text: 'Power='+str(int(power.value))
FloatLayout:
Button:
text: 'Back to menu'
on_press: app.stop()
size_hint: .2, .1
pos_hint: {'x':.8, 'y':0}
""")
# on_press: root.manager.current = 'menu'
# Declare both screens
class MenuScreen(Screen):
pass
class HvacScreen(Screen):
pass
# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(HvacScreen(name='hvac'))
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
Traceback (most recent call last):
File "C:/Users/Martin/PycharmProjects/Hvac/mart_layout.py", line 84, in <module>
TestApp().run()
File "C:\Python37\lib\site-packages\kivy\app.py", line 800, in run
root = self.build()
File "C:/Users/Martin/PycharmProjects/Hvac/mart_layout.py", line 80, in build
return Builder.load_string(kv)
File "C:\Python37\lib\site-packages\kivy\lang\builder.py", line 368, in load_string
parser = Parser(content=string, filename=fn)
File "C:\Python37\lib\site-packages\kivy\lang\parser.py", line 392, in __init__
self.parse(content)
File "C:\Python37\lib\site-packages\kivy\lang\parser.py", line 498, in parse
self.execute_directives()
File "C:\Python37\lib\site-packages\kivy\lang\parser.py", line 462, in execute_directives
mod = __import__(package)
File "C:\Users\Martin\PycharmProjects\Hvac\mart_clock.py", line 29, in <module>
class TimeApp(App):
NameError: name 'App' is not defined
Did you edit something? Are both files located in the same directory?
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5447b4a5-e97d-410c-ab95-5b046274c6f9%40googlegroups.com.
#:import IncrediblyCrudeClock mart_clock.IncrediblyCrudeClock
The problem seems to be:
File "C:\Users\Martin\PycharmProjects\Hvac\mart_clock.py", line 29, in <module>
class TimeApp(App):
This line of code should not be executed, if being imported. It is under the test for __name__
These files work for me. Put a print statement under this if statement. This code should not be getting executed if you are not running from mart_clock.py.
How are you invoking the code?
if __name__ == "__main__":
from kivy.app import App
from kivy.lang import Builder
kv="""
BoxLayout:
orientation: 'vertical'
Label:
text: 'Clock Test'
IncrediblyCrudeClock:
font_size: '50sp'
"""
class TimeApp(App):
def build(self):
return Builder.load_string(kv)
TimeApp().run()
From: Mart1
Sent: Sunday, November 24, 2019 7:59 AM
To: Kivy users support
Subject: [kivy-users] Re: Put a Clock in a BoxLayout
The problem seems to be with the import of this in my kivy file which raise this issue :
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/bb8909b6-cf0a-4224-a703-ef0f05df28e6%40googlegroups.com.
just run:
python mart_layout.py
From: Elliot Garbus
Sent: Sunday, November 24, 2019 8:04 AM
To: kivy-...@googlegroups.com
Subject: RE: [kivy-users] Re: Put a Clock in a BoxLayout
The problem seems to be:
File "C:\Users\Martin\PycharmProjects\Hvac\mart_clock.py", line 29, in <module>
class TimeApp(App):
This line of code should not be executed, if being imported. It is under the test for __name__
These files work for me. Put a print statement under this if statement. This code should not be getting executed if you are not running from mart_clock.py.
How are you invoking the code?
if __name__ == "__main__":
from kivy.app import App
from kivy.lang import Builder
kv="""
BoxLayout:
orientation: 'vertical'
Label:
text: 'Clock Test'
IncrediblyCrudeClock:
font_size: '50sp'
"""
class TimeApp(App):
def build(self):
return Builder.load_string(kv)
TimeApp().run()
From: Mart1
Sent: Sunday, November 24, 2019 7:59 AM
To: Kivy users support
Subject: [kivy-users] Re: Put a Clock in a BoxLayout
The problem seems to be with the import of this in my kivy file which raise this issue :
--
from kivy.app import App
from kivy.lang import Builder
When I run mart_layout.py:
When I run clock.py
From: Mart1
Sent: Sunday, November 24, 2019 8:19 AM
To: Kivy users support
Subject: [kivy-users] Re: Put a Clock in a BoxLayout
Yeah I just did "python mart_layout" and it raised me this error...I just download them and it raised me this.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d06d87bb-aeef-45e4-b5a8-6a8fd2b3258f%40googlegroups.com.
Check the formatting at the bottom of mart_clock.py
Perhaps something reformatted the code? It should look just like below. The code must all be indented under the if..
if __name__ == "__main__":
from kivy.app import App
from kivy.lang import Builder
kv="""
BoxLayout:
orientation: 'vertical'
Label:
text: 'Clock Test'
IncrediblyCrudeClock:
font_size: '50sp'
"""
class TimeApp(App):
def build(self):
return Builder.load_string(kv)
TimeApp().run()
From: Elliot Garbus
Sent: Sunday, November 24, 2019 8:42 AM
To: kivy-...@googlegroups.com
Subject: RE: [kivy-users] Re: Put a Clock in a BoxLayout
When I run mart_layout.py:
When I run clock.py
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5ddaa4f4.1c69fb81.d4a5a.2737SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Line
from kivy.uix.floatlayout import FloatLayout
from math import cos, sin, pi
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import NumericProperty
import datetime
kv = '''
#:import math math
[ClockNumber@Label]:
text: str(ctx.i)
pos_hint: {"center_x": 0.5+0.42*math.sin(math.pi/6*(ctx.i-12)), "center_y": 0.5+0.42*math.cos(math.pi/6*(ctx.i-12))}
font_size: self.height/16
<MyClockWidget>:
face: face
ticks: ticks
FloatLayout:
id: face
size_hint: None, None
pos_hint: {"center_x":0.5, "center_y":0.5}
size: 0.9*min(root.size), 0.9*min(root.size)
canvas:
Color:
rgb: 0.1, 0.1, 0.1
Ellipse:
size: self.size
pos: self.pos
ClockNumber:
i: 1
ClockNumber:
i: 2
ClockNumber:
i: 3
ClockNumber:
i: 4
ClockNumber:
i: 5
ClockNumber:
i: 6
ClockNumber:
i: 7
ClockNumber:
i: 8
ClockNumber:
i: 9
ClockNumber:
i: 10
ClockNumber:
i: 11
ClockNumber:
i: 12
Ticks:
id: ticks
r: min(root.size)*0.9/2
'''
Builder.load_string(kv)
class MyClockWidget(FloatLayout):
pass
class Ticks(Widget):
def __init__(self, **kwargs):
super(Ticks, self).__init__(**kwargs)
self.bind(pos=self.update_clock)
self.bind(size=self.update_clock)
def update_clock(self, *args):
self.canvas.clear()
with self.canvas:
time = datetime.datetime.now()
Color(0.2, 0.5, 0.2)
Line(points=[self.center_x, self.center_y, self.center_x+0.8*self.r*sin(pi/30*time.second), self.center_y+0.8*self.r*cos(pi/30*time.second)], width=1, cap="round")
Color(0.3, 0.6, 0.3)
Line(points=[self.center_x, self.center_y, self.center_x+0.7*self.r*sin(pi/30*time.minute), self.center_y+0.7*self.r*cos(pi/30*time.minute)], width=2, cap="round")
Color(0.4, 0.7, 0.4)
th = time.hour*60 + time.minute
Line(points=[self.center_x, self.center_y, self.center_x+0.5*self.r*sin(pi/360*th), self.center_y+0.5*self.r*cos(pi/360*th)], width=3, cap="round")
class MyClockApp(App):
def build(self):
clock = MyClockWidget()
Clock.schedule_interval(clock.ticks.update_clock, 1)
return clock
if __name__ == '__main__':
MyClockApp().run()
When I run mart_layout.py:
When I run clock.py
From: Mart1
Sent: Sunday, November 24, 2019 8:19 AM
To: Kivy users support
Subject: [kivy-users] Re: Put a Clock in a BoxLayout
Yeah I just did "python mart_layout" and it raised me this error...I just download them and it raised me this.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
Yes, the same method should work.
Take notice, templates : [ClockNumber@Label]
have been deprecated in kivy. I suggest updating this to not use templates.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/22c6ec5a-ba7c-4802-a152-2e472c04aa50%40googlegroups.com.
Use this instead of the template:
<ClockNumber@Label>:
i: 0
text: str(self.i)
pos_hint: {"center_x": 0.5+0.42*math.sin(math.pi/6*(self.i-12)), "center_y": 0.5+0.42*math.cos(math.pi/6*(self.i-12))}
font_size: self.height/16
From: Elliot Garbus
Sent: Sunday, November 24, 2019 10:06 AM
To: kivy-...@googlegroups.com
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5ddab8a3.1c69fb81.40d44.2d67SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
from kivy.uix.widget import Widget
from kivy.graphics import Color, Line
from kivy.uix.floatlayout import FloatLayout
from math import cos, sin, pi
from kivy.clock import Clock
from kivy.properties import NumericProperty
import datetime
class MyClockWidget(FloatLayout):
def __init__(self, **kwargs):
Clock.schedule_interval(self.ticks.update_clock, 1)
super().__init__(**kwargs)
if __name__ == "__main__":
from kivy.app import App
from kivy.lang import Builder
class MyClockApp(App):
def build(self):
return Builder.load_string(kv)
class Ticks(Widget):
def __init__(self, **kwargs):
super(Ticks, self).__init__(**kwargs)
self.bind(pos=self.update_clock)
self.bind(size=self.update_clock)
def update_clock(self, *args):
self.canvas.clear()
with self.canvas:
time = datetime.datetime.now()
Color(0.2, 0.5, 0.2)
Line(points=[self.center_x, self.center_y, self.center_x + 0.8 * self.r * sin(pi / 30 * time.second),
self.center_y + 0.8 * self.r * cos(pi / 30 * time.second)], width=1, cap="round")
Color(0.3, 0.6, 0.3)
Line(points=[self.center_x, self.center_y, self.center_x + 0.7 * self.r * sin(pi / 30 * time.minute),
self.center_y + 0.7 * self.r * cos(pi / 30 * time.minute)], width=2, cap="round")
Color(0.4, 0.7, 0.4)
th = time.hour * 60 + time.minute
Line(points=[self.center_x, self.center_y, self.center_x + 0.5 * self.r * sin(pi / 360 * th),
self.center_y + 0.5 * self.r * cos(pi / 360 * th)], width=3, cap="round")
MyClockApp().run()
The definition of the class Ticks and it’s methods need to be above the if __name__ == ‘__main__’:
Cut and paste this code toward the top of the file. Do you understand why?
From: Mart1
Sent: Sunday, November 24, 2019 10:40 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: Put a Clock in a BoxLayout
I tried this :
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/3a2bdaf7-14d5-4384-82a9-d8e1cd6b1523%40googlegroups.com.
from kivy.uix.widget import Widget
from kivy.graphics import Color, Line
from kivy.uix.floatlayout import FloatLayout
from math import cos, sin, pi
from kivy.clock import Clock
from kivy.properties import NumericProperty
import datetime
class MyClockWidget(FloatLayout):
def __init__(self, **kwargs):
Clock.schedule_interval(self.ticks.update_clock, 1)
super().__init__(**kwargs)
class Ticks(Widget):
def __init__(self, **kwargs):
super(Ticks, self).__init__(**kwargs)
self.bind(pos=self.update_clock)
self.bind(size=self.update_clock)
def update_clock(self, *args):
self.canvas.clear()
with self.canvas:
time = datetime.datetime.now()
Color(0.2, 0.5, 0.2)
Line(points=[self.center_x, self.center_y, self.center_x + 0.8 * self.r * sin(pi / 30 * time.second),
self.center_y + 0.8 * self.r * cos(pi / 30 * time.second)], width=1, cap="round")
Color(0.3, 0.6, 0.3)
Line(points=[self.center_x, self.center_y, self.center_x + 0.7 * self.r * sin(pi / 30 * time.minute),
self.center_y + 0.7 * self.r * cos(pi / 30 * time.minute)], width=2, cap="round")
Color(0.4, 0.7, 0.4)
th = time.hour * 60 + time.minute
Line(points=[self.center_x, self.center_y, self.center_x + 0.5 * self.r * sin(pi / 360 * th),
self.center_y + 0.5 * self.r * cos(pi / 360 * th)], width=3, cap="round")
if __name__ == "__main__":
from kivy.app import App
from kivy.lang import Builder
kv =
"""
#:import math math
<ClockNumber@Label>:
i: 0
text: str(self.i)
pos_hint: {"
center_x": 0.5+0.42*math.sin(math.pi/6*(self.i-12)), "center_y": 0.5+0.42*math.cos(math.pi/6*(self.i-12))}
center_x":0.5, "center_y":0.5}
MyClockApp().run()
[CRITICAL] [Application ] No window is created. Terminating application run.
You need to instance MyClockWidget in you kv code for the test. At the bottom of the KV code in the file below add:
MyClockWidget:
This will instance the clock widget.
From: Mart1
Sent: Sunday, November 24, 2019 11:23 AM
To: Kivy users support
Subject: Re: [kivy-users] Re: Put a Clock in a BoxLayout
Yeah I agree with you and I did this at first but it didn't work so I tried another thing which is the code I paste the post above.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/6d02ada7-591f-45a7-8897-62542fccd531%40googlegroups.com.
":0.5}
MyClockWidget:
"""
No need to add any properties, MyClockWidget: needed to be all the way to the left. It is the root widget – but that was not quite the right answer.
That will make allow the clock file to run – but is not the complete answer.
The ‘rules in KV’ need to be part of the ‘base’ code for the widget, and the kv code needs to get loaded. It is easier to show it, blow. This will let you import the file and use the widget. Happy to answer you questions.
from kivy.uix.widget import Widget
from kivy.graphics import Color, Line
from math import cos, sin, pi
from kivy.clock import Clock
from kivy.lang import Builder
import datetime
kv = '''
#:import math math
<ClockNumber@Label>:
i: 0
text: str(self.i)
pos_hint: {"center_x": 0.5+0.42*math.sin(math.pi/6*(self.i-12)), "center_y": 0.5+0.42*math.cos(math.pi/6*(self.i-12))}
font_size: self.height/16
<MyClockWidget@FloatLayout>:
'''
Builder.load_string(kv)
class Ticks(Widget):
def __init__(self, **kwargs):
super(Ticks, self).__init__
(**kwargs)
Clock.schedule_interval(self.update_clock, 1)
self.bind(pos=self.update_clock)
self.bind(size=self.update_clock)
def update_clock(self, *args):
self.canvas.clear()
with self.canvas:
time = datetime.datetime.now()
Color(0.2, 0.5, 0.2)
Line(points=[self.center_x, self.center_y, self.center_x+0.8*self.r*sin(pi/30*time.second), self.center_y+0.8*self.r*cos(pi/30*time.second)], width=1, cap="round")
Color(0.3, 0.6, 0.3)
Line(points=[self.center_x, self.center_y, self.center_x+0.7*self.r*sin(pi/30*time.minute), self.center_y+0.7*self.r*cos(pi/30*time.minute)], width=2, cap="round")
Color(0.4, 0.7, 0.4)
th = time.hour*60 + time.minute
Line(points=[self.center_x, self.center_y, self.center_x+0.5*self.r*sin(pi/360*th), self.center_y+0.5*self.r*cos(pi/360*th)], width=3, cap="round")
if __name__ == '__main__':
from kivy.app import
App
kv_test = """
BoxLayout:
orientation: 'vertical'
Label:
text: 'Analog Clock Test'
MyClockWidget:
"""
class MyClockApp(App):
def build(self):
return Builder.load_string(kv_test)
MyClockApp().run()
From: Mart1
Sent: Sunday, November 24, 2019 12:18 PM
To: Kivy users support
Subject: Re: [kivy-users] Re: Put a Clock in a BoxLayout
Okay, I supposse that I need to put some properties in it nah ? Otherwise still get nothing :( (Sorry I'm not an expert as you can see so I'm trying to learn this language by the help of others, forums and all)
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d528c67e-b52b-4574-a4cb-b8c7c93436b6%40googlegroups.com.
#:import IncrediblyCrudeClock simple_clock.IncrediblyCrudeClock
#:import MyClockWidget clock.Ticks
<MenuScreen@Screen>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint_y: 8
Button:
text: 'Goto HVAC Controller'
on_press: root.manager.current = 'hvac'
Button:
text: 'Switch to Phone'
on_press: app.stop()
MyClockWidget:
IncrediblyCrudeClock:
size_hint_y: 2
font_size: '50sp'
There was a problem with the way the analog clock was implemented. The hands were not drawn relative to the face. I updated the clock file.
Notice that in the original code the hands were drawn relative to self. Now they are drawn relative to the parent. The parent is the clock face (the parent widget).
I set p = self.parent to reduce the total amount of typing.
def update_clock(self, *args):
self.canvas.clear()
p = self.parent
with self.canvas:
time = datetime.datetime.now()
Color(0.2, 0.5, 0.2)
Line(points=[p.center_x, p.center_y, p.center_x+0.8*self.r*sin(pi/30*time.second), p.center_y+0.8*self.r*cos(pi/30*time.second)], width=1, cap="round")
Color(0.3, 0.6, 0.3)
Line(points=[p.center_x, p.center_y, p.center_x+0.7*self.r*sin(pi/30*time.minute), p.center_y+0.7*self.r*cos(pi/30*time.minute)], width=2, cap="round")
Color(0.4, 0.7, 0.4)
th = time.hour*60 + time.minute
Line(points=[p.center_x, p.center_y, p.center_x+0.5*self.r*sin(pi/360*th), p.center_y+0.5*self.r*cos(pi/360*th)], width=3, cap="round")
The analog clock is a float layout, so you can put it anywhere.
MyClockWidget:
size_hint: None, None
size: 200, 200
pos_hint:{'center_x': .5, 'center_y': .5 }
From: Mart1
Sent: Sunday, November 24, 2019 3:18 PM
To: Kivy users support
Subject: Re: [kivy-users] Re: Put a Clock in a BoxLayout
Awesome that's perfect almost working like a charm !!
I imported it with :
#:import IncrediblyCrudeClock simple_clock.IncrediblyCrudeClock
#:import MyClockWidget clock.Ticks
<MenuScreen@Screen>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint_y: 8
Button:
text: 'Goto HVAC Controller'
on_press: root.manager.current = 'hvac'
Button:
text: 'Switch to Phone'
on_press: app.stop()
MyClockWidget:
IncrediblyCrudeClock:
size_hint_y: 2
font_size: '50sp'
Something strange happens.
As you can see the background of the clock is at the right position but not the clock pointer (wich is at the bottom)...Where is the problem here ?
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/85e8125b-44c0-4a43-8cc6-a48bb008122f%40googlegroups.com.