愚問をてってれーするの楽しすぎるでしょβ

ESRのエッセイに唆されてPythonする初心者が半端な勉強ログを書き散らす場所 ICT文化にどうしても住みたいという遠大な啓示が下った人の住処/気持ちよさのためだけに/お客様のお忘れになった恥の概念が母胎に届いているようです

GUIをいじるモジュール、tkinterを使うの段、その4

from tkinter import *
import random#2つのモジュールを何故それぞれこのように参照しているのか?

dead=0
alive=1
status=(dead, alive)#なんで()なんだっけ??との違いは何だったっけ?

height=15
width=15
field=

random.seed(0)

def init():
    for i in range(height):
        row=
        for j in range(width):
            row.append(dead)
            field.append(row)
    draw()

def rand_set():
    if is_run:
        return#elseの条件文要らないのか…うーん
    for i in range(height):
        for j in range(width):
            field[i][j]=random.choice(status)
    draw()

def reset():
    if is_run:
        return#こういう脱出のさせ方もありなんだなー
    for i in range(height):
        for j in range(width):
            field[i][j]=dead
    draw()

def draw():
    canvas.delete("field")
    for i in range(height):
        for j in range(width):
            x0=space+j*cell
            y0=space+i*cell
            x1=x0+cell
            y1=y0+cell
            canvas.create_rectangle(x0,y0,x1,y1,fill=color[field[i][j]],tags="field")

def next():
    global field
    new_field=
    for i in range(height):
        row=[]
        for j in range(width):
            num=count(j, i)
            if num==3:
                row.append(alive)
            elif num==2:
                row.append(field[i][j])
            else:
                row.append(dead)
        new_field.append(row)
   field=new_field
   draw()

def count(y, x):
    num=0

    if x!=0:
        num=num+field[y][x-1]
        if y!=0:
            num=num+field[y-1][x-1]
        if y!=height-1:
            num=num+jield[y+1][x-1]

    if x!=width-1:
        num=num+field[y][x+1]
        if y!=0:
            num=num+field[y-1][x+1]
        if y!=height-1:
            num=num+field[y+1][x+1]

    if y!=0:
        num=num+field[y-1][x]
    if y!=height-1:
        num=num+field[y+1][x]

    return num

def run():
    if is_run:
        next()
    root.after(500, run)

def start_stop():
    global is_run
    is_run=not is_run

space=5
cell=16
color={alive: "red", dead: "white"}#colorというのは辞書型らしいが、どこから取ってるんだ?そしてこの定義は何?
is_run=False

root=Tk()

canvas_h=space*2+height*cell
canvas_w=space*2+width*cell
canvas=Canvas(root, width=canvas_w, height=canvas_h)
canvas.pack()

reset_button=Button(root, text="reset", command=reset)
reset_button.pack(side="left")

rand_button=Button(root, text="rand", command=rand_set)
rand_button.pack(side="left")

run_button=Button(root, text="start/stop", command=start_stop)
run_button.pack(side="left")

exit_button=Button(root, text="exit", command=root.destroy)
exit_button.pack(side="right")

init()
run()
root.mainloop()

結果:

f:id:komosudare:20150326092509p:plain

C:\pythonlifegame>python 0326-2.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:\python34\lib\tkinter\__init__.py", line 582, in callit
func(*args)
File "0326-2.py", line 91, in run
next()
File "0326-2.py", line 54, in next
num=count(j, i)
File "0326-2.py", line 73, in count
num=num+jield[y+1][x-1]
NameError: name 'jield' is not defined

 

 

エラーの読み方がよく分からない。resetとrandはいいけど、start_stop周りの定義がおかしい。

あとは、canvasのサイズを固定しないとボタンが表示されていない状態を作れてしまう。