如何使用 class 在 tkinter canvas 上绘制多边形?
How to draw a polygon on a tkinter canvas using a class?
from tkinter import*
root = Tk()
shape = Canvas(root)
class GUI():
def __init__(self):
pass
def create_polygon(self, points, colour, posit):
try:
shape.delete(self.poly)
except:
pass
self.poly = shape.create_polygon(points, colour, posit)
self.poly.shape.grid(column=posit[0],row=posit[1])
polygon = GUI()
polygon.create_polygon([150,75,225,0,300,75,225,150],'yellow',[1,2])
我刚开始使用 tkinter
和 classes,但我想制作一个非常简单的 class 来创建正多边形。该程序中的代码应删除之前制作的任何多边形,然后在调用程序时继续制作新的多边形,但我不断收到我不理解的错误。另外,您将如何着手绘制六边形?
Traceback (most recent call last):
File "//xsvr-02/Students/10SAMP_Al/HW/polygon creator.py", line 19, in <module>
polygon.create_polygon([150,75,225,0,300,75,225,150],'yellow',[1,2])
File "//xsvr-02/Students/10SAMP_Al/HW/polygon creator.py", line 15, in create_polygon
self.poly = shape.create_polygon(points, colour, posit)
File "C:\Python34\lib\tkinter\__init__.py", line 2305, in create_polygon
return self._create('polygon', args, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 2287, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: wrong # coordinates: expected an even number, got 11
只是调用参数错误
如果您想更改代码,此解决方案可以帮助您。
Class GUI 只是继承自 Canvas,没有实现任何东西。
from Tkinter import*
root = Tk()
class GUI(Canvas):
'''inherits Canvas class (all Canvas methodes, attributes will be accessible)
You can add your customized methods here.
'''
def __init__(self,master,*args,**kwargs):
Canvas.__init__(self, master=master, *args, **kwargs)
polygon = GUI(root)
polygon.create_polygon([150,75,225,0,300,75,225,150], outline='gray',
fill='gray', width=2)
polygon.pack()
root.mainloop()
如需更多帮助,请添加评论。
from tkinter import*
root = Tk()
shape = Canvas(root)
class GUI():
def __init__(self):
pass
def create_polygon(self, points, colour, posit):
try:
shape.delete(self.poly)
except:
pass
self.poly = shape.create_polygon(points, colour, posit)
self.poly.shape.grid(column=posit[0],row=posit[1])
polygon = GUI()
polygon.create_polygon([150,75,225,0,300,75,225,150],'yellow',[1,2])
我刚开始使用 tkinter
和 classes,但我想制作一个非常简单的 class 来创建正多边形。该程序中的代码应删除之前制作的任何多边形,然后在调用程序时继续制作新的多边形,但我不断收到我不理解的错误。另外,您将如何着手绘制六边形?
Traceback (most recent call last):
File "//xsvr-02/Students/10SAMP_Al/HW/polygon creator.py", line 19, in <module>
polygon.create_polygon([150,75,225,0,300,75,225,150],'yellow',[1,2])
File "//xsvr-02/Students/10SAMP_Al/HW/polygon creator.py", line 15, in create_polygon
self.poly = shape.create_polygon(points, colour, posit)
File "C:\Python34\lib\tkinter\__init__.py", line 2305, in create_polygon
return self._create('polygon', args, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 2287, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: wrong # coordinates: expected an even number, got 11
只是调用参数错误
如果您想更改代码,此解决方案可以帮助您。
Class GUI 只是继承自 Canvas,没有实现任何东西。
from Tkinter import*
root = Tk()
class GUI(Canvas):
'''inherits Canvas class (all Canvas methodes, attributes will be accessible)
You can add your customized methods here.
'''
def __init__(self,master,*args,**kwargs):
Canvas.__init__(self, master=master, *args, **kwargs)
polygon = GUI(root)
polygon.create_polygon([150,75,225,0,300,75,225,150], outline='gray',
fill='gray', width=2)
polygon.pack()
root.mainloop()
如需更多帮助,请添加评论。