问题描述
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
和类的新手,但我想创建一个非常简单的类来创建正多边形.这个程序中的代码应该删除以前创建的任何多边形,然后在调用程序时继续创建一个新的多边形,但我不断收到一个我不明白的错误.另外你会如何去画一个六边形?
I'm new to using tkinter
and classes but I want to make a very simple class to create a regular polygon. The code in this program should delete any polygon previously made and then proceed to make a new polygon when the program is called but I keep getting an error that I don't understand. Also how would you go about drawing a hexagon instead?
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
推荐答案
只是调用参数错误.
如果您想更改代码,此解决方案可以为您提供帮助.
If you want to change your code, this solution can help you.
类 GUI 只是继承自 Canvas,并没有实现任何东西.
Class GUI just inherits from Canvas and doesn't implement anything.
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()
如需更多帮助,请添加评论.
For more help add comments.
这篇关于如何使用类在 tkinter 画布上绘制多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!