I’m trying to write a basic drawing widget using the Tkinter library.
The very basic code I am using for now is:
from Tkinter import *
master = Tk()
w = Canvas(master, width=1200, height=800)
w_centre = 600
h_centre = 400
w.pack()
w.create_oval(w_centre-50, h_centre-50, w_centre+50, h_centre+50)
mainloop()
What actually want to do is start with 3 variables, x,y (centre of circle) and size. From there, I can use simple maths to work out the (x0, y0, x1, y1) set required to make the circle (http://docs.huihoo.com/tkinter/tkinter-reference-a-gui-for-python/create_oval.html)
I want to do this programatically, by feeding in the size as a value from a dataset, and x,y as dependant value (if I need 1 circle, it would I would use x1,y1 if I need two circles they would be x2,y2 & x3,y3 etc). The purpose being to try and build a basic visualiser for a dataset I have. I figure I can write an array of the x,y coords that I can look up as required, and as the size value will be pulled from a list – so it would be better to write a function that would take the size, lookup the x,y as required and feed the create_circle call the appropriate values.
I know I need to call the create_oval function with the x0,y0,x1,y1 values, and I wonder if there was a way I could call another function that would allow me to make these values every time by handing it the x,y (centre of circle) and size (radius) value, and for it give me back the relevant x0,y0,x1,y1 values.
As this is a reusable piece of maths, I think I need to make a class, but I can’t find a tutorial that helps me to understand how to define the class function, and then to call it every time I need it.
I appreciate I’ve probably not worded this very well, I’m trying to learn rudimentary python on my own (with no CS background) so please forgive me if I’ve named something wrong, or missed something important.
Could someone one throw me a hint or a pointer towards a decent resouce?
Python allows you to return any kind of object from a function; in particular, you can return the tuple
(x0,y0,x,1,y1)that you need forcreate_oval:Then you can use the
*argssyntax to call a function with a set of arguments taken from a sequence (a list, a tuple, etc.). You can use it to callcreate_ovalthis way: