如何使用类在 tkinter 画布上配置多边形? [英] How to configure a polygon on a tkinter canvas using a class?
本文介绍了如何使用类在 tkinter 画布上配置多边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我当前的工作代码,但我想添加一个函数,该函数将在主循环"期间更改形状的颜色:
This is my current working code but I want to add in a function that will change the color of the shape during 'mainloop':
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()
我在想这样的事情会起作用(在课堂内):
I was thinking something like this would work (inside the class):
def configure(self,colour):
Canvas.itemconfig(self,fill=colour)
然后我用:
polygon.configure('red')
但是我一直收到这个错误,我不知道如何解决它:
But I keep getting this error and I dont know how to fix it:
Exception in Tkinter callback
File "C:/Users/User/Documents/Algies homework/Hexaheaflexagon sim.py", line 117, in configure
Canvas.itemconfig(self,fill=colour)
TypeError: itemconfigure() missing 1 required positional argument: 'tagOrId'
推荐答案
我想你尝试这样做
from Tkinter import*
# --- class ---
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)
# default - poly not exists
self.poly = None
def create_poly(self, points, outline='gray', fill='gray', width=2):
# remember poly
self.poly = self.create_polygon(points, outline=outline, fill=fill, width=width)
def set_poly_fill(self, color):
# if poly exists then you can change fill
if self.poly:
self.itemconfig(self.poly, fill=color)
# --- main ---
root = Tk()
polygon = GUI(root)
polygon.create_poly([150,75,225,0,300,75,225,150])
polygon.set_poly_fill('red')
polygon.pack()
root.mainloop()
这篇关于如何使用类在 tkinter 画布上配置多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文