from tkinter import *
height = 600
width = 600
root = Tk()
canvas = Canvas(root, width = width, height = height, bg = 'red3')
canvas.pack()
# Code for Fries
canvas.create_polygon(150, 100, 160, 250, 170, 250, 160, 80, fill =
'yellow', outline = 'black')
canvas.create_polygon(160, 100, 170, 250, 180, 250, 170, 80, fill =
'yellow', outline = 'black')
canvas.create_polygon(170, 100, 180, 250, 190, 250, 180, 80, fill =
'yellow', outline = 'black')
canvas.create_polygon(180, 100, 190, 250, 200, 250, 190, 80, fill =
'yellow', outline = 'black')
canvas.create_polygon(190, 100, 200, 250, 210, 250, 200, 80, fill =
'yellow', outline = 'black')
canvas.create_polygon(200, 100, 210, 250, 220, 250, 210, 80, fill =
'yellow', outline = 'black')
canvas.create_polygon(210, 100, 220, 250, 230, 250, 220, 80, fill =
'yellow', outline = 'black')
canvas.create_polygon(220, 100, 230, 250, 240, 250, 230, 80, fill =
'yellow', outline = 'black')
canvas.create_polygon(230, 100, 240, 250, 250, 250, 240, 80, fill =
'yellow', outline = 'black')
canvas.create_polygon(240, 100, 250, 250, 260, 250, 250, 80, fill =
'yellow', outline = 'black')
canvas.create_polygon(250, 100, 260, 250, 270, 250, 260, 80, fill =
'yellow', outline = 'black')
canvas.create_polygon(260, 100, 270, 250, 280, 250, 270, 80, fill =
'yellow', outline = 'black')
# Packet
packet = canvas.create_polygon(200, 500, 400, 500, 450, 200, 150, 200, fill
= 'red4', outline = 'black')
# i'm lovin' it Text
canvas.create_text(300, 550, text = 'i\'m lovin\' it', fill = 'yellow', font
= ('Comic Sans MS', 23))
canvas.create_text(300, 350, text = 'M', font = ('mclawsuit', 110), fill =
'yellow')
canvas.mainloop()
我不想反复重复用于薯条的create_polygon
代码。
我尝试使用函数和类,但早期的薯条死了,只有最后一个鱼苗是可见的休息是黑色。
答案 0 :(得分:6)
这应该可以解决问题
# code for fries
fries_polygons = [
(150, 100, 160, 250, 170, 250, 160, 80),
(160, 100, 170, 250, 180, 250, 170, 80),
(170, 100, 180, 250, 190, 250, 180, 80),
(180, 100, 190, 250, 200, 250, 190, 80),
(190, 100, 200, 250, 210, 250, 200, 80),
(200, 100, 210, 250, 220, 250, 210, 80),
(210, 100, 220, 250, 230, 250, 220, 80),
(220, 100, 230, 250, 240, 250, 230, 80),
(230, 100, 240, 250, 250, 250, 240, 80),
(240, 100, 250, 250, 260, 250, 250, 80),
(250, 100, 260, 250, 270, 250, 260, 80),
(260, 100, 270, 250, 280, 250, 270, 80),
]
for fry in fries_polygons:
canvas.create_polygon(*fry, fill='yellow', outline='black')
您可以阅读here有关用于解压缩任意列表参数的*-operator
。
答案 1 :(得分:1)
只关注你的Fries多边形,你可能想试试这个:
for y in range (150,261,10):
canvas.create_polygon(y, 100, y+10, 250, y+20, 250, y+10, 80, fill = 'yellow', outline = 'black')
答案 2 :(得分:1)
您可以创建一个绘制单个鱼苗的功能,然后多次调用该功能:
def draw_fry(canvas, x,y):
canvas.create_polygon(x, y, x+10, y+150, x+20, y+150, x+10, y-20,
fill="yellow", outline="black")
for x in range(150, 260, 10):
draw_fry(canvas, x, 100)
答案 3 :(得分:1)
您可以定义一个方法,用于创建具有给定起始坐标和画布对象的多个薯条:
def create_fries(canvas, coords, fry_quantity=1):
_coords = list(coords)
_fry_width = _coords[4] - _coords[2] # to be used as separation as well
for _ in range(fry_quantity): # to create a number of fries
canvas.create_polygon(_coords, fill='yellow', outline='black')
for idx in range(len(_coords)): # to increase only even idx values
if not (idx % 2): # basically idx % 2 == 0
_coords[idx] += _fry_width
然后使用:
调用它fry_start_coords = (150, 100, 160, 250, 170, 250, 160, 80)
create_fries(canvas, fry_start_coords, 12)
实施代码:
from tkinter import *
def create_fries(canvas, coords, fry_quantity=1):
_coords = list(coords)
_fry_width = _coords[4] - _coords[2] # to be used as separation as well
for _ in range(fry_quantity): # to create a number of fries
canvas.create_polygon(_coords, fill='yellow', outline='black')
for idx in range(len(_coords)): # to increase only even idx values
if not (idx % 2): # basically idx % 2 == 0
_coords[idx] += _fry_width
height = 600
width = 600
root = Tk()
canvas = Canvas(root, width = width, height = height, bg = 'red3')
canvas.pack()
fry_start_coords = (150, 100, 160, 250, 170, 250, 160, 80)
create_fries(canvas, fry_start_coords, 12)
# Packet
packet = canvas.create_polygon(200, 500, 400, 500, 450, 200, 150, 200, fill
= 'red4', outline = 'black')
# i'm lovin' it Text
canvas.create_text(300, 550, text = 'i\'m lovin\' it', fill = 'yellow', font
= ('Comic Sans MS', 23))
canvas.create_text(300, 350, text = 'M', font = ('mclawsuit', 110), fill =
'yellow')
canvas.mainloop()
答案 4 :(得分:0)
以这种方式存储您的参数
arguments_for_polygons = [(150, 100, 160, 250, 170, 250, 160, 80, fill = 'yellow', outline = 'black'),\
(160, 100, 170, 250, 180, 250, 170, 80, fill = 'yellow', outline = 'black'),\
(#more arguments inside a tuple like the previous)]
然后将其解压缩为for循环:
for arguments in arguments_for_polygons:
canvas.create_polygon(*arguments)
#This will unpack the arguments in position
有关 * 操作数的详细信息,请查看this