#widget2.py
from kivy.app import App
from kivy.graphics import Color, Rectangle, Line, Ellipse
from kivy.uix.widget import Widget
#from kivy.core.window import Window
class Widget2(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
with self.canvas:
w = self.width
h = self.height
Color(0, 1, 0)
self.rect1 = Rectangle(pos=(w/4, h/4), size=(w/2, h/2))
Color(1, 0, 0)
self.ellipse1 = Ellipse(pos=(w/8, h/8), size=(w/4, h/4))
Color(1, 0, 0)
self.ellipse2 = Ellipse(pos=(w*5/8, h/8), size=(w/4, h/4))
Color(0, 0, 1)
if w>h:
self.line1 = Line(circle=(w/2, h/2, h/8), width=2)
else:
self.line1 = Line(circle=(w / 2, h / 2, w / 8), width=2)
self.bind(pos=self.window_resize, size=self.window_resize)
def window_resize(self, *args):
w = self.width
h = self.height
self.rect1.pos = (w/4, h/4)
self.rect1.size = (w/2, h/2)
self.ellipse1.pos = (w/8, h/8)
self.ellipse1.size = (w / 4, h / 4)
self.ellipse2.pos = (w *5/ 8, h / 8)
self.ellipse2.size = (w / 4, h / 4)
if w>h:
self.line1.circle = (w/2, h/2, h/8)
else:
self.line1.circle = (w / 2, h / 2, w / 8)
self.line1.width = 2
class Widget2App(App):
def build(self):
return Widget2()
Widget2App().run()
reference:
window resize event
No comments:
Post a Comment