Package: yadraw
YaDrawYaDraw is a super-simple graphical interface built on top of pygame.
The key use case is when you have a program which performs some calculations,
and you want to add super-simple visualization to it. YaDraw runs
the graphical interface in a separate thread, so that:GUI is fully responsive during main thread computations.Adding GUI to an existing program is easy and doesn't require program design changes.Minimal exampleimportyadraw.yadrawasydimporttime# Create a windowwindow=yd.Window()# Fill it with grey color and draw a blue circlewindow.fill(color=(100,100,100))window.circle(center=(100,100),radius=10,color=(0,0,255))window.update()# Close after 5 secondstime.sleep(5)window.close()This example shows the straightforward application approach: the user tells the program what to do.Async draw exampleSuppose you have a calculation which you want to visualize in real time:You have an ongoing calculation (a for loop in this case).The result of this calculation (in this case list_of_points_to_draw) is dynamically updated and needs visualization during calculation.The pace of producing results and the pace of visualization (e.i. fps) should be independent.importyadraw.yadrawasydimporttime# Create a custom on_redraw handlerdefcustom_on_redraw(self:yd.Window):self.fill((200,200,200))# Fill entire screen with grey colorforpointinlist_of_points_to_draw:# For each already calculated pointself.circle(center=point,radius=4,color=(0,0,255))# Draw a blue circle at this point# Monkey-patch the on_redraw handler for yd.Window classyd.Window.on_redraw=custom_on_redraw# Create a window, set automatic update to 1 second interval = 1 fps.window=yd.Window(auto_update_s=1)# Start the "calculation" of the points to drawlist_of_points_to_draw=[]foriinrange(100):# this loop simulates a calculation of the points to drawtime.sleep(0.1)# calculation takes timelist_of_points_to_draw.append((i*8,i*8))# adding calculated points# Await GUI exit (i.e. wait for the user to close the window)window.wait_until_exit()This example also shows a different approach compared to the minimal example:
the user reacts to the events generated by system or pydraw.
In order to specify the desired behavior, custom event handler is created.Both approaches can be easily implemented and require nearly no boilerplate code.Using original pygame functionsYaDraw can be used with original pygame functions in case they haven't been implemented yet:importyadraw.yadrawasydimportpygameimporttime# Create a windowwindow=yd.Window()# Fill it with grey color and draw a blue circle using original pygame functionswindow.surface.fill(color=(100,100,100))pygame.draw.circle(surface=window.surface,center=(100,100),radius=50,color=(0,0,255))window.update()# Close after 5 secondstime.sleep(5)window.close()AreasThe window can be split into multiple areas. Each area can have its own position and scale.
The positions of points are scaled, while the properties such as widths and radii are not.importyadraw.yadrawasydimporttime# Create a windowwindow=yd.Window()# Adding an area called "main" with shifted origin of coordinateswindow.areas["main"]=yd.Area(x0=10,y0=10,# coordinated of top-left corner of areaw=400,h=800,# area width and heightxc=100,yc=100,# origin of coordinated relative to the top-left corner of areaxs=50,ys=50)# scale# Fill screen with blue colorwindow.fill(color=(0,0,100))# Fill the "main" area with grey color and draw a couple of circleswindow.areas["main"].fill(color=(100,100,100))window.areas["main"].circle(center=(0,0),radius=50,color=(0,0,255))# Center at (110, 110), radius is 50window.areas["main"].circle(center=(1,1),radius=50,color=(0,255,0))# Center at (160, 160), radius is 50window.update()# Close after 5 secondstime.sleep(5)window.close()Copyright © 2022Ivan I. YakovkinGithubPyPi