简体   繁体   中英

How can I dynamically create the output of the following code?

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()

Motive: Create a tkinter screen which displays the McDonald's fries packet. I don't want to repeat the create_polygon code used for fries repeatedly. I tried it using function and class, but the earlier fries die and only the last fry is visible rest are black.

This should do the trick

# 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')

You can read here about the *-operator that is used to unpack arbitrary list arguments.

Looking only at your Fries polygons, you might like to try this:

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')

You can create a function to draw a single fry, then call that function multiple times:

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)

You can define a method for creating a number of fries with a given starting coordinates and canvas object:

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

and then call it using:

fry_start_coords = (150, 100, 160, 250, 170, 250, 160, 80)

create_fries(canvas, fry_start_coords, 12)

Implementing to your code:

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()

Store your arguments this way

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)]

And the unpack it in a for loop:

for arguments in arguments_for_polygons:
    canvas.create_polygon(*arguments)
    #This will unpack the arguments in position

For more info about * operand look at this

Edit 1: Look at the @BoarGules solution, that is the best way, I just answered a general example for any function for you to know how to use the tuple unpacking

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM