python - 迭代代码并在tkinter

时间:2017-10-26 09:18:44

标签: python tkinter

def draw_pattern(a_canvas, size, pattern_str_list):
    x0 = size
    x1 = x0 + size
    y0 = size
    y1 = y0 + size

    for word in pattern_str_list:
      for char in word:

         if char == "A":
            a_canvas.create_oval(x0,y0,x1,y1,fill = "gray")
            x0 = x0 + size
            x1 = x0 + size
         if char == "B":
            a_canvas.create_oval(x0,y0,x1,y1,fill = "red")
            x0 = x0 + size
            x1 = x0 + size
         if char == "C":
            a_canvas.create_oval(x0,y0,x1,y1,fill = "blue")
            x0 = x0 + size
            x1 = x0 + size
         if char == "D":
            a_canvas.create_oval(x0,y0,x1,y1,fill = "green")
            x0 = x0 + size
            x1 = x0 + size
         if char == "_":
            a_canvas.create_oval(x0,y0,x1,y1,fill = "black")
            x0 = x0 + size
            x1 = x0 + size


        y0 += size

您好,我正在尝试构建一个程序,该程序接受字符串列表,读取并从中获取数据,并根据列表中的内容通过tkinter小部件显示彼此相邻的行形状。假设a_canvas是一个大的方格,size = 50pattern_str_list = (ABC, D_A, BB__A_D)。如何遍历列表并显示正确的彩色圆圈,然后移到下面的行中以查找列表中的下一个项目?

基本上看起来像:

灰红色蓝色

绿黑灰色

红色,黑色,黑色,灰色,绿色

在tkinter的彩色圆圈中?

1 个答案:

答案 0 :(得分:0)

尝试使用此尺寸:

def draw_pattern(a_canvas, size, pattern_str_list):
    colours = {"A":"gray", "B":"red", "C":"blue", "D":"green"}
    x = size
    y = size

    for word in pattern_str_list:
        for char in word:
            a_canvas.create_oval(x, y , x + size, y + size, fill=colours.get(char, "black"))
            x += size
        y += size