【问题标题】:Tkinter Canvas polygon object issuesTkinter Canvas 多边形对象问题
【发布时间】:2018-02-17 19:43:21
【问题描述】:

我看到了这个帖子: How to draw a polygon on a tkinter canvas using a class?

但是当我试图画一个正方形时:

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([0, 0, 100, 100, 100, 0, 100], outline='gray', fill='gray', width=2)
polygon.pack()
root.mainloop()

我明白了:

expected even number: got 7

我已经尝试了我能想到的所有方法,但我无法让它发挥作用!

【问题讨论】:

  • 那么,你考虑过给偶数个坐标吗?

标签: python tkinter polygon


【解决方案1】:

create_polygon 方法要求偶数个参数时,为什么不提供偶数?

create_polygon 方法采用 [x0, y0, x1, y1, ...] 形式的参数,其中 (x0, y0), ... 是多边形的顶点。每个顶点有 2 个坐标,因此参数的数量必须是偶数。

polygon.create_polygon([0, 0, 100, 0, 100, 100, 0, 100], outline='gray', fill='gray', width=2) 应该适用于顶点为(0,0)(100,0)(100,100)(0,100) 的正方形。

【讨论】:

  • 但是当我尝试这样做时,我得到了某种沙漏形状。
  • 你确定你有0、0、100、0、100、100、0、100的顺序吗?它与 0, 0, 100, 100, 100, 0, 0, 100 之类的东西略有不同,后者会给出沙漏形状。 (Python 2.7.9)
  • 哦,谢谢。我现在真的对 tkinter 多边形感到困惑。
  • page 上的图片有帮助吗?多边形由其顶点的坐标和连续顶点之间的线定义。因此必须以正确的顺序给出坐标。点 (0,0), (100, 0), ... 的坐标被展平为 [0,0,100,0,...]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-18
  • 2022-10-01
  • 2019-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多