我正在尝试使用 Tkinter 库编写一个基本的绘图小部件。
我现在使用的非常基本的代码是:
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()
实际想要做的是从 3 个变量开始,x,y(圆心)和大小。从那里,我可以使用简单的数学计算出制作圆圈所需的 (x0, y0, x1, y1) 集 (http://docs.huihoo.com/tkinter/tkinter-reference-a-gui-for- python/create_oval.html)
我想以编程方式执行此操作,通过输入大小作为数据集中的值,并将 x,y 作为依赖值(如果我需要 1 个圆,如果我需要,我会使用 x1,y1需要两个圆,它们是 x2,y2 和 x3,y3 等)。目的是尝试为我拥有的数据集构建一个基本的可视化工具。我想我可以编写一个 x,y 坐标数组,我可以根据需要查找它,并且大小值将从列表中提取 - 所以最好编写一个获取大小的函数,查找x,y 根据需要并为 create_circle 调用提供适当的值。
我知道我需要使用 x0,y0,x1,y1 值调用 create_oval 函数,我想知道是否有一种方法可以调用另一个函数,该函数允许我每次通过传递 x,y 来生成这些值(圆心)和大小(半径)值,并为其返回相关的 x0,y0,x1,y1 值。
由于这是一个可重用的数学部分,我想我需要创建一个类,但我找不到一个教程来帮助我理解如何定义类函数,然后在每次需要时调用它。
我很感激我的措辞可能不太好,我正在尝试自己学习基本的Python(没有CS背景),所以如果我命名错误,或者错过了一些重要的东西,请原谅我。
有人能给我一个提示或指向一个像样的资源吗?
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?
发布评论
评论(1)
Python 允许您从函数返回任何类型的对象;特别是,您可以返回
create_oval
所需的元组(x0,y0,x,1,y1)
:然后您可以使用
*args
语法 使用从序列(列表、元组等)获取的一组参数来调用函数。您可以使用它来调用create_oval
: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
*args
syntax to call a function with a set of arguments taken from a sequence (a list, a tuple, etc.). You can use it to callcreate_oval
this way: