How to use the Progressbar widget in real time - python-2.7

I have the following code that uses the Tkinter widget:
from Tkinter import *
from ttk import Progressbar
root = Tk()
def addThoseNumbers():
y = 0
for x in range(1000000):
y += x
if x % 10000.0 == 0:
invoiceStatus['value'] = x/10000.0
print y
invoiceStatus = Progressbar(root, length = 280, mode = 'determinate')
invoiceStatus.pack()
invoiceButton = Button(root, text = "Confirm", font = ("Helvetica", 10), \
command = addThoseNumbers)
invoiceButton.pack()
root.mainloop()
Ideally, the progressbar would update while the program is working to show how much of the task has been completed, however it just cuts from 0% to 100% once the task has completed. How do I write my program so the progressbar shows the progress in real-time?

You just need to add root.update()
from Tkinter import *
from ttk import Progressbar
root = Tk()
def addThoseNumbers():
y = 0
for x in range(1000000):
y += x
if x % 10000.0 == 0:
invoiceStatus['value'] = x/10000.0
root.update()
print y
invoiceStatus = Progressbar(root, length = 280, mode = 'determinate')
invoiceStatus.pack()
invoiceButton = Button(root, text = "Confirm", font = ("Helvetica", 10), \
command = addThoseNumbers)
invoiceButton.pack()
root.mainloop()

Related

NameError: not defined (python)

Trying to build a simple game using turtle graphics and Python.
I created enemies and put them in the while loop so that whenever they touch the boundaries on either sides they move down by 40 units. I put the value of y co-ordinate in a variable u. But when I run the code it says:
nameError: 'u' not defined
Help!!
#!/usr/bin/python
import turtle
import os
#screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("spaceinvaders")
#boarder
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pendown()
border_pen.pensize(3)
for side in range(4):
border_pen.fd(600)
border_pen.lt(90)
border_pen.hideturtle()
#player
player = turtle.Turtle()
player.color("blue")
player.shape("triangle")
player.penup()
player.speed(0)
player.setposition(0,-250)
player.setheading(90)
playerspeed = 15
#enemy
enemy = turtle.Turtle()
enemy.color("red")
enemy.shape("circle")
enemy.penup()
enemy.speed(0)
enemy.setposition(-200,250)
enemyspeed = 2
#move
def move_left():
x = player.xcor()
x -= playerspeed
if x < -280:
x = - 280
player.setx(x)
def move_right():
x = player.xcor()
x += playerspeed
if x > 280:
x = +280
player.setx(x)
#key bindings
turtle.listen()
turtle.onkey(move_left,"Left")
turtle.onkey(move_right,"Right")
#mainloop
while True:
#enemy moves
x = enemy.xcor()
x += enemyspeed
enemy.setx(x)
if enemy.xcor() < -280:
u = enemy.ycor()
u -= 40
enemyspeed *= -1
enemy.sety(u)
if enemy.xcor() > 280:
u = enemy.ycor()
u -= 40
enemyspeed *= -1
enemy.sety(u)
delay = raw_input("press enter to finish")
Even with the incorrect loop indentation that #downshift noted, you shouldn't have gotten the error you quoted as u is set immediately before being used.
The main problem I see with your code design is your use of while True: which should not occur in an event driven program. Rather, the enemy's motion should be handled via a timer event and program control turned over to mainloop() so that other events can fire correctly. I've reworked your program along those lines below and made some style and optimizaion tweaks:
import turtle
# player motion event handlers
def move_left():
turtle.onkey(None, 'Left') # avoid overlapping events
player.setx(max(-280, player.xcor() - playerspeed))
turtle.onkey(move_left, 'Left')
def move_right():
turtle.onkey(None, 'Right')
player.setx(min(280, player.xcor() + playerspeed))
turtle.onkey(move_right, 'Right')
# enemy motion timer event handler
def move_enemy():
global enemyspeed
# enemy moves
enemy.forward(enemyspeed)
x = enemy.xcor()
if x < -280 or x > 280:
enemy.sety(enemy.ycor() - 40)
enemyspeed *= -1
wn.ontimer(move_enemy, 10)
# screen
wn = turtle.Screen()
wn.bgcolor('black')
wn.title('spaceinvaders')
# border
STAMP_SIZE = 20
border_pen = turtle.Turtle('square', visible=False)
border_pen.shapesize(600 / STAMP_SIZE, 600 / STAMP_SIZE, 3)
border_pen.pencolor('white')
border_pen.stamp()
# player
player = turtle.Turtle('triangle', visible=False)
player.color('blue')
player.speed('fastest')
player.penup()
player.setheading(90)
player.setposition(0, -250)
player.showturtle()
playerspeed = 15
# enemy
enemy = turtle.Turtle('circle', visible=False)
enemy.color('red')
enemy.speed('fastest')
enemy.penup()
enemy.setposition(-200, 250)
enemy.showturtle()
enemyspeed = 2
# key bindings
turtle.onkey(move_left, 'Left')
turtle.onkey(move_right, 'Right')
turtle.listen()
wn.ontimer(move_enemy, 100)
turtle.mainloop() # for Python 3 use wn.mainloop()
This should hopefully smooth your path to adding more functionality to your game.

How to modify a variable when a while loop is running Python

I am using wx.python along with VPython to make an orbit simulator, however i'm having trouble trying to get the sliders in the GUI to effect the simulation, I assume it's because I am trying to get the number associated with the slider button to go into a while loop when it is running.
So my question is, how do i get the function SetRate to update in the while loop located at the bottom of the code? (I have checked to see that the slider is retuning values)
Here is my code for reference:
Value = 1.0
dt = 100.0
def InputValue(Value):
dt = Value
def SetRate(evt):
global Value
Value = SpeedOfSimulation.GetValue()
return Value
w = window(menus=True, title="Planetary Orbits",x=0, y=0, width = 1000, height = 1000)
Screen = display(window = w, x = 30, y = 30, width = 700, height = 500)
gdisplay(window = w, x = 80, y = 80 , width = 40, height = 20)
p = w.panel # Refers to the full region of the window in which to place widgets
SpeedOfSimulation = wx.Slider(p, pos=(800,10), size=(200,100), minValue=0, maxValue=1000)
SpeedOfSimulation.Bind(wx.EVT_SCROLL, SetRate)
TestData = [2, 0, 0, 0, 6371e3, 5.98e24, 0, 0, 0, 384400e3, 0, 0, 1737e3, 7.35e22, 0, 1e3, 0]
Nstars = TestData[0] # change this to have more or fewer stars
G = 6.7e-11 # Universal gravitational constant
# Typical values
Msun = 2E30
Rsun = 2E9
vsun = 0.8*sqrt(G*Msun/Rsun)
Stars = []
colors = [color.red, color.green, color.blue,
color.yellow, color.cyan, color.magenta]
PositionList = []
MomentumList = []
MassList = []
RadiusList = []
for i in range(0,Nstars):
s=i*8
x = TestData[s+1]
y = TestData[s+2]
z = TestData[s+3]
Radius = TestData[s+4]
Stars = Stars+[sphere(pos=(x,y,z), radius=Radius, color=colors[i % 6],
make_trail=True, interval=10)]
Mass = TestData[s+5]
SpeedX = TestData[s+6]
SpeedY = TestData[s+7]
SpeedZ = TestData[s+8]
px = Mass*(SpeedX)
py = Mass*(SpeedY)
pz = Mass*(SpeedZ)
PositionList.append((x,y,z))
MomentumList.append((px,py,pz))
MassList.append(Mass)
RadiusList.append(Radius)
pos = array(PositionList)
Momentum = array(MomentumList)
Mass = array(MassList)
Mass.shape = (Nstars,1) # Numeric Python: (1 by Nstars) vs. (Nstars by 1)
Radii = array(RadiusList)
vcm = sum(Momentum)/sum(Mass) # velocity of center of mass
Momentum = Momentum-Mass*vcm # make total initial momentum equal zero
Nsteps = 0
time = clock()
Nhits = 0
while True:
InputValue(Value) #Reprensents the change in time
rate(100000) #No more than 100 loops per second on fast computers
# Compute all forces on all stars
r = pos-pos[:,newaxis] # all pairs of star-to-star vectors (Where r is the Relative Position Vector
for n in range(Nstars):
r[n,n] = 1e6 # otherwise the self-forces are infinite
rmag = sqrt(sum(square(r),-1)) # star-to-star scalar distances
hit = less_equal(rmag,Radii+Radii[:,newaxis])-identity(Nstars)
hitlist = sort(nonzero(hit.flat)[0]).tolist() # 1,2 encoded as 1*Nstars+2
F = G*Mass*Mass[:,newaxis]*r/rmag[:,:,newaxis]**3 # all force pairs
for n in range(Nstars):
F[n,n] = 0 # no self-forces
Momentum = Momentum+sum(F,1)*dt
# Having updated all momenta, now update all positions
pos = pos+(Momentum/Mass)*dt
# Update positions of display objects; add trail
for i in range(Nstars):
Stars[i].pos = pos[i]
I know nothing about vpython but in a normal wxPython app, you will use wx.Timer instead of while loop.
here is an example of wx.Timer modified from https://www.blog.pythonlibrary.org/2009/08/25/wxpython-using-wx-timers/
You will want to separate the while loop part from your SetRate class method and put it in update.
import wx
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Timer Tutorial 1",
size=(500,500))
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.update, self.timer)
SpeedOfSimulation = wx.Slider(p, pos=(800,10), size=(200,100), minValue=0, maxValue=1000)
SpeedOfSimulation.Bind(wx.EVT_SCROLL, SetRate)
self.SpeedOfSimulation = SpeedOfSimulation
def update(self, event):
# Compute all forces on all stars
SpeedOfSimulation = self.SpeedOfSimulation.GetValue()

Python Tkinter perlin noise animation is very slow

I'm trying to animate a 2D perlin noise in the 3rd dimension using Tkinter un Python 2.7.
It's all working, but the performances are very low (about 2 fps).
I think that the problem is in the "animate" function, in which I create a new image every time I update the noise.
How can I achieve a good frame rate? In this code the image is 640x480px, but I need it to be at least 1080x720px.
I'm open to every type of suggestions (even changing the GUI library).
from Tkinter import *
from noise import snoise3, pnoise3
from PIL import Image, ImageTk
import threading
class Noise:
def __init__(self):
self.root = Tk()
self.w = 640
self.h = 480
self.canvas = Canvas(self.root, width = self.w, height = self.h)
self.canvas.pack()
self.img = Image.new("1",(self.w,self.h))
self.pix = self.img.load()
self.z = 0
octv = 8
freq = 16.0 * octv;
for y in xrange(self.h):
for x in xrange(self.w):
self.pix[x,y] = int(snoise3(x/freq,y/freq,self.z,octaves = octv)*127.0+128.0)
self.image = ImageTk.PhotoImage(self.img)
self.canvas.create_image(self.w/2,self.h/2,image=self.image)
self.canvas.pack()
self.root.after(0, self.tr)
self.root.mainloop()
def tr(self):
self.t1 = threading.Thread(target=self.animate)
self.t1.daemon = True
self.t1.start()
def animate(self):
octv = 8
freq = 16.0 * octv;
while True:
self.z += 0.01
for y in xrange(self.h):
for x in xrange(self.w):
self.pix[x,y] = int(snoise3(x/freq,y/freq,self.z,octaves = octv)*127.0+128.0)
self.image = ImageTk.PhotoImage(self.img)
self.canvas.create_image(self.w/2,self.h/2,image=self.image)
self.canvas.pack()
Noise()
Thank you all ^^

pyinstaller for python 2.7.9 get executable that when ran just flashes on screen

Hi I installed pyinstaller and pywin32 64 bit version to get an .exe.
I did get a .exe built, but when I double click on it, it just flashes on the screen and closes. I used this as a setup.py
from distutils.core import setup
import py2exe
setup(console=['BP.py'])
The script BP.py asks the user to input some values and make some plots.
Any ideas?
Thanks
Below is the code
Code for BP.py is below
##
##BP.py
import matplotlib.pyplot as plt
import numpy as np
# A function to plot the lines
def plot_lines(x,y,colors,j):
ax = plt.subplot(2,2,j)
for i in range(len(colors)):
plt.plot(x,y[i],colors[i])
def plot_setup():
fig = plt.figure()
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True)
def Max_avg(row,col,x,colors,ao1,ao2,j,a_list):
for a in a_list:
i = 0
y_a1 = np.zeros((row,col))
y_a2 = np.zeros((row,col))
y = np.zeros((row,col))
for ao1_v,ao2_v in zip(ao1,ao2):
y_a1[i] = 3*x**2
y_a2[i] = 5*x
y[i] = np.add(y_a1[i],y_a2[i])
i = i+1
plot_lines(x,y,colors,j)`enter code here`
j = j+1
def main():
x1 = -10
x2 = 10
numpts = 100
x = np.linspace(x1,x2,numpts,endpoint=True)
col = len(x)
# a_list = [-1.7,-5]
print "Please enter a list of coefficients seperated by a white space."
string_input = raw_input()
a_list = string_input.split()
a_list = [int(a) for a in a_list]
ao1 = (-5.0,2.0)
ao2 = (1.0,10.0)
col_plt = 2
colors = ['b','g']
j = 1
plot_setup()
Max_avg(2,col,x,colors,ao1,ao2,j,a_list)
plt.show()
if __name__ == "__main__":
main()

OpenStreetMap generate georeferenced image [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm new to Openstreetmap and mapnick,
I'm trying to export map image which will be geo-referenced
(So it can be used in other applications)
I've installed osm and mapnik inside ubuntu virtual machine
I've tried using generate_image.py script, but generated image is not equal to the bounding box. My python knowledge is not good enough for me to fix the script.
I've also tried using nik2img.py script using verbose mode, for example:
nik2img.py osm.xml sarajevo.png --srs 900913 --bbox 18.227 43.93 18.511 43.765 --dimensions 10000 10000
and tried using the log bounding box
Step: 11 // --> Map long/lat bbox: Envelope(18.2164733537,43.765,18.5215266463,43.93)
Unfortunately generated image is not equal to the bounding box :(
How can I change scripts so I can georeference generated image?
Or do you know an easier way to accomplish this task?
Image i'm getting using the http://www.openstreetmap.org/ export is nicely geo-referenced, but it's not big enough :(
I've managed to change generate_tiles.py to generate 1024x1024 images together with correct bounding box
Changed script is available bellow
#!/usr/bin/python
from math import pi,cos,sin,log,exp,atan
from subprocess import call
import sys, os
from Queue import Queue
import mapnik
import threading
DEG_TO_RAD = pi/180
RAD_TO_DEG = 180/pi
# Default number of rendering threads to spawn, should be roughly equal to number of CPU cores available
NUM_THREADS = 4
def minmax (a,b,c):
a = max(a,b)
a = min(a,c)
return a
class GoogleProjection:
def __init__(self,levels=18):
self.Bc = []
self.Cc = []
self.zc = []
self.Ac = []
c = 1024
for d in range(0,levels):
e = c/2;
self.Bc.append(c/360.0)
self.Cc.append(c/(2 * pi))
self.zc.append((e,e))
self.Ac.append(c)
c *= 2
def fromLLtoPixel(self,ll,zoom):
d = self.zc[zoom]
e = round(d[0] + ll[0] * self.Bc[zoom])
f = minmax(sin(DEG_TO_RAD * ll[1]),-0.9999,0.9999)
g = round(d[1] + 0.5*log((1+f)/(1-f))*-self.Cc[zoom])
return (e,g)
def fromPixelToLL(self,px,zoom):
e = self.zc[zoom]
f = (px[0] - e[0])/self.Bc[zoom]
g = (px[1] - e[1])/-self.Cc[zoom]
h = RAD_TO_DEG * ( 2 * atan(exp(g)) - 0.5 * pi)
return (f,h)
class RenderThread:
def __init__(self, tile_dir, mapfile, q, printLock, maxZoom):
self.tile_dir = tile_dir
self.q = q
self.m = mapnik.Map(1024, 1024)
self.printLock = printLock
# Load style XML
mapnik.load_map(self.m, mapfile, True)
# Obtain <Map> projection
self.prj = mapnik.Projection(self.m.srs)
# Projects between tile pixel co-ordinates and LatLong (EPSG:4326)
self.tileproj = GoogleProjection(maxZoom+1)
def render_tile(self, tile_uri, x, y, z):
# Calculate pixel positions of bottom-left & top-right
p0 = (x * 1024, (y + 1) * 1024)
p1 = ((x + 1) * 1024, y * 1024)
# Convert to LatLong (EPSG:4326)
l0 = self.tileproj.fromPixelToLL(p0, z);
l1 = self.tileproj.fromPixelToLL(p1, z);
# Convert to map projection (e.g. mercator co-ords EPSG:900913)
c0 = self.prj.forward(mapnik.Coord(l0[0],l0[1]))
c1 = self.prj.forward(mapnik.Coord(l1[0],l1[1]))
# Bounding box for the tile
if hasattr(mapnik,'mapnik_version') and mapnik.mapnik_version() >= 800:
bbox = mapnik.Box2d(c0.x,c0.y, c1.x,c1.y)
else:
bbox = mapnik.Envelope(c0.x,c0.y, c1.x,c1.y)
render_size = 1024
self.m.resize(render_size, render_size)
self.m.zoom_to_box(bbox)
self.m.buffer_size = 128
# Render image with default Agg renderer
im = mapnik.Image(render_size, render_size)
mapnik.render(self.m, im)
im.save(tile_uri, 'png256')
print "Rendered: ", tile_uri, "; ", l0 , "; ", l1
# Write geo coding informations
file = open(tile_uri[:-4] + ".tab", 'w')
file.write("!table\n")
file.write("!version 300\n")
file.write("!charset WindowsLatin2\n")
file.write("Definition Table\n")
file.write(" File \""+tile_uri[:-4]+".jpg\"\n")
file.write(" Type \"RASTER\"\n")
file.write(" ("+str(l0[0])+","+str(l1[1])+") (0,0) Label \"Pt 1\",\n")
file.write(" ("+str(l1[0])+","+str(l1[1])+") (1023,0) Label \"Pt 2\",\n")
file.write(" ("+str(l1[0])+","+str(l0[1])+") (1023,1023) Label \"Pt 3\",\n")
file.write(" ("+str(l0[0])+","+str(l0[1])+") (0,1023) Label \"Pt 4\"\n")
file.write(" CoordSys Earth Projection 1, 104\n")
file.write(" Units \"degree\"\n")
file.close()
def loop(self):
while True:
#Fetch a tile from the queue and render it
r = self.q.get()
if (r == None):
self.q.task_done()
break
else:
(name, tile_uri, x, y, z) = r
exists= ""
if os.path.isfile(tile_uri):
exists= "exists"
else:
self.render_tile(tile_uri, x, y, z)
bytes=os.stat(tile_uri)[6]
empty= ''
if bytes == 103:
empty = " Empty Tile "
self.printLock.acquire()
print name, ":", z, x, y, exists, empty
self.printLock.release()
self.q.task_done()
def render_tiles(bbox, mapfile, tile_dir, minZoom=1,maxZoom=18, name="unknown", num_threads=NUM_THREADS):
print "render_tiles(",bbox, mapfile, tile_dir, minZoom,maxZoom, name,")"
# Launch rendering threads
queue = Queue(32)
printLock = threading.Lock()
renderers = {}
for i in range(num_threads):
renderer = RenderThread(tile_dir, mapfile, queue, printLock, maxZoom)
render_thread = threading.Thread(target=renderer.loop)
render_thread.start()
#print "Started render thread %s" % render_thread.getName()
renderers[i] = render_thread
if not os.path.isdir(tile_dir):
os.mkdir(tile_dir)
gprj = GoogleProjection(maxZoom+1)
ll0 = (bbox[0],bbox[3])
ll1 = (bbox[2],bbox[1])
for z in range(minZoom,maxZoom + 1):
px0 = gprj.fromLLtoPixel(ll0,z)
px1 = gprj.fromLLtoPixel(ll1,z)
# check if we have directories in place
zoom = "%s" % z
if not os.path.isdir(tile_dir + zoom):
os.mkdir(tile_dir + zoom)
for x in range(int(px0[0]/1024.0),int(px1[0]/1024.0)+1):
# Validate x co-ordinate
if (x < 0) or (x >= 2**z):
continue
# check if we have directories in place
str_x = "%s" % x
if not os.path.isdir(tile_dir + zoom + '/' + str_x):
os.mkdir(tile_dir + zoom + '/' + str_x)
for y in range(int(px0[1]/1024.0),int(px1[1]/1024.0)+1):
# Validate x co-ordinate
if (y < 0) or (y >= 2**z):
continue
str_y = "%s" % y
tile_uri = tile_dir + zoom + '_' + str_x + '_' + str_y + '.png'
# Submit tile to be rendered into the queue
t = (name, tile_uri, x, y, z)
queue.put(t)
# Signal render threads to exit by sending empty request to queue
for i in range(num_threads):
queue.put(None)
# wait for pending rendering jobs to complete
queue.join()
for i in range(num_threads):
renderers[i].join()
if __name__ == "__main__":
home = os.environ['HOME']
try:
mapfile = "/home/emir/bin/mapnik/osm.xml" #os.environ['MAPNIK_MAP_FILE']
except KeyError:
mapfile = "/home/emir/bin/mapnik/osm.xml"
try:
tile_dir = os.environ['MAPNIK_TILE_DIR']
except KeyError:
tile_dir = home + "/osm/tiles/"
if not tile_dir.endswith('/'):
tile_dir = tile_dir + '/'
#-------------------------------------------------------------------------
#
# Change the following for different bounding boxes and zoom levels
#
#render sarajevo at 16 zoom level
bbox = (18.256, 43.785, 18.485, 43.907)
render_tiles(bbox, mapfile, tile_dir, 16, 16, "World")
Try Maperitive's export-bitmap command, it generates various georeferencing sidecar files
(worldfile, KML, OziExplorer .MAP file).