![](/img/trans.png)
[英]How can I count each dictionary value output in Python in the following code example?
[英]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()
動機:創建一個顯示麥當勞薯條包的 tkinter 屏幕。 我不想重復用於薯條的create_polygon
代碼。 我嘗試使用函數和類,但較早的薯條死了,只有最后一個炸薯條是可見的,其余的都是黑色的。
這應該可以解決問題
# 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')
您可以在此處閱讀有關用於解壓縮任意列表參數的*-operator
。
只看你的薯條多邊形,你可能想試試這個:
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')
您可以創建一個函數來繪制單個魚苗,然后多次調用該函數:
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)
您可以定義一種方法,用於使用給定的起始坐標和畫布對象創建多個薯條:
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()
以這種方式存儲您的參數
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
有關*操作數的更多信息,請查看這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.