python - Tkinter process not shown during run -
i running creature simulator in python 2.7 using tkinter visualizer. map made of squares, colors represent land types, , red square represents creature. use canvas.move, move red square around board. has move quite lot. know should start , should end. have run simulation, bit bit, , when regulated maybe 2 moves ie. sim isn't running i'm testing it. can see movements. when run sim, buzzes , can see of canvas map, no creature , no creature movement. question this. firstly, how can possibly slow down process can see movements? or why simulation run , show of tkinter?
the simulation quite large , hard pick out important bits, code below more of simplification. matches how did tkinter stuff. sim added more calculations , loops. it's worth noting example works perfectly.
driver.py:
from tkinter import * import animation class alien(object): def __init__(self): #set canvas self.root = tk() self.canvas = canvas(self.root, width=400, height=400) self.canvas.pack() #vars self.map = [[1, 0, 0, 1, 0], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 1, 0, 0], [1, 0, 0, 1, 0]] self.x = 0 self.y = 0 r = 50 self.land = {} #draw init i, row in enumerate(self.map): j, cell in enumerate(row): color = "black" if cell else "green" self.canvas.create_rectangle(r * i, r * j, r * (i + 1), r * (j + 1), outline=color, fill=color) self.land[(i, j)] = self.canvas.create_text(r * i, r * j, anchor=ne, fill="white", text="1", tag=str((i, j))) self.creature = self.canvas.create_rectangle(r * self.x, r * self.y, r * (self.x + 1), r * (self.y + 1), outline="red", fill="red") self.canvas.pack(fill=both, expand=1) #action movement = animation.animation(self.root, self.canvas, self.creature, self.land) self.root.after(0, movement.animate) #clost tk self.root.mainloop() = alien()
animation.py:
from random import randrange import sys class animation(): def __init__(self, root, canvas, creature, land): self.x = self.y = 0 self.ctr = 10 self.canvas = canvas self.creature = creature self.root = root self.land = land #self.root.after(250, self.animate) self.canvas.move(self.creature, 2 * 50, 2 * 50) def animate(self): self.ctr -= 1 if self.ctr > 0: in range(2): = randrange(1, 5) if == 1: self.y = -1 elif == 2: self.y = 1 elif == 3: self.x = -1 elif == 4: self.x = 1 #root.after(250, self.animate(canvas, creature)) """moves creature around canvas""" self.movement() self.root.after(250, self.animate) def movement(self): self.canvas.move(self.creature, self.x * 50, self.y * 50)
you can slow down process using tkinters after
method can used difrent widget.
canvas.after(time, dosomething) #the first parameter how many milliseconds wait before call second parameter.
where dosomething function should way of updating cnavas.
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html
Comments
Post a Comment