pyglet resolution of screen into variables - python-2.7

is there any options how to get values height and width of screen into variables using pyglet? I am able to print it but not extract these values.
import pyglet
platform = pyglet.window.get_platform()
display = platform.get_default_display()
screen = display.get_screens()
->
>>> screen
[XlibScreen(display=<pyglet.canvas.xlib.XlibDisplay object at 0x7f4644cf0990>, x=0, y=0, width=1366, height=768, xinerama=0)]
>>>
Any idea? Thanks in advance.

it should be as simple as this:
platform = pyglet.window.get_platform()
display = platform.get_default_display()
screen = display.get_default_screen()
screen_width = screen.width
screen_height = screen.height

In new versions of pyglet pyglet.window.get_platform() is deprecated/removed proof
Therefore code will look like:
display = pyglet.canvas.Display()
screen = display.get_default_screen()
screen_width = screen.width
screen_height = screen.height

Related

Python 2.7, Tkinter, make a point "trace" a waveform

I'm trying to make a widget that demonstrates the relationship between a sine wave and it's phasor diagram. I want a pointer that follows the user's mouse movements but instead of freely moving around the page, I want it to stick to the waveform I've plotted and only follow the co-ordinates that make up that line. Kind of like a slider but along a sine wave! Any suggestions? Thanks. Here's what I've got so far;
import math
import Tkinter as tk
from Tkinter import PhotoImage, Canvas
from PIL import Image, ImageTk
sinwidget = tk.Tk()
main_canvas = tk.Canvas(sinwidget, width = 400, height = 400, bg= "white")
main_canvas.pack()
def callback(event):
canvas = event.widget
x = canvas.canvasx(event.x)
y = canvas.canvasy(event.y)
print canvas.find_closest(x, y)
draw(event.x, event.y)
def draw(x, y):
box.coords(pointer, x-20, y-20, x+20, y+20)
def exit_():
sinwidget.destroy()
box = main_canvas
box.bind('<Motion>', callback)
box.pack()
pointer = box.create_rectangle(0.2,0.2,0.2,0.2)
wavelength = 360
height = 400
center = height//2
degree = 1
increment = 0.0175
amplitude = -80
sin = []
for x in range(360):
sin.append(x * degree)
sin.append(int(math.sin(x * increment) * amplitude) + center)
sinwave = main_canvas.create_line(sin, fill="red", width=2.0)
xaxis = main_canvas.create_line(0, center, wavelength, center, fill="black",
width=3.0)
yaxis = main_canvas.create_line(2, 100, 2, 300, fill="black", width=3.0)
exit_button = tk.Button(sinwidget, text = "exit", command = exit_)
exit_button.pack()
sinwidget.mainloop()
Thanks for any help you can offer!

How to keep a Tkinter widget on top of the others

I have some code that moves images left and right but I do not want them to appear on top of the right border, which I draw as a rectangle.
What are the options in Tkinter to keep a widget (in my example a rectangle) on top of some other widgets (in my code a tile, which is an image)?
I am drawing the rectangle and the image on one canvas.
I can image that using two canvas could do the trick, but are there any other options/settings?
Thanks
import Tkinter as tk # for Python2
import PIL.Image, PIL.ImageTk
win = tk.Tk()
#Create a canvas
canvas = tk.Canvas(win, height = 500, width = 500)
#Create a rectangle on the right of the canvas
rect = canvas.create_rectangle(250, 0, 500, 250, width = 2, fill = "red")
#Create an image
SPRITE = PIL.Image.open("sprite.png")
tilePIL = SPRITE.resize((100, 100))
tilePI = PIL.ImageTk.PhotoImage(tilePIL)
tile = canvas.create_image(100, 100, image = tilePI, tags = "a tag")
#Place the canvas
canvas.grid(row = 1, column = 0, rowspan = 5)
#Move the tile to the right.
#The tile will go on top of red rectangle. How to keep the rectangle on top of the tile?
canvas.coords(tile, (300, 100))
canvas.mainloop()
Use tag_raise() method:
canvas.tag_raise(tile)

Vb.Net : Dynamically create rectangles in a picturebox side by side

I am current working on an application in which I am using a video stream eventhandler to produce a camera image in a Forms picturebox in VB.Net (yes it sounds like a kinect app...it is...)
I can create a red rectangle over my bitmap image (camer stream) using this code:
Dim g As Graphics = Graphics.FromImage(kinectVideoBitMap)
Dim rect As Rectangle = New Rectangle
rect.Width = 6.4
rect.Height = 480
g.DrawRectangle(Pens.Red, rect)
End If
Straight forward enough.
But, what I really would like to happen is to be able to create multiple rectangles side by side crossing the width of my picture box at "6.4" (<--pixels I think??)
I have attempted using a "For Loop" to create these rectangles and then setting a new location for each one, like this:
Dim g As Graphics = Graphics.FromImage(kinectVideoBitMap)
Dim i As Integer
For i = 0 To 100
Dim rect As Rectangle = New Rectangle
rect.Width = 6.4
rect.Height = 480
rect.Location = New System.Drawing.Point(*{not sure how to measure here}*, *{or here either}*)
g.DrawRectangle(Pens.Red, rect)
Next
Any help would be wonderful.
My plan is to use these rectangles to help me choose skeletons to monitor chosen by the microphone array sound source position. Hence the rectangles width of "6.4"
640 is the Width resolution of the video stream, and I divided it by "100" the measure of "-50 to 50" in the microphone array.
But, in the end I just need help with the rectangles.
EDIT: Here is something here that I have come up with. I am now very close to an answer:
Dim g As Graphics = Graphics.FromImage(kinectVideoBitMap)
Dim i As Integer = 1
Dim newLocationX As Integer = 0
Do Until i = 100
Dim rect(i) As Rectangle
Rect(i) = New Rectangle
Rect(i).Width = 6
Rect(i).Height = 480
Rect(i).Location = New System.Drawing.Point(video.Left + newLocationX, Rect(i).Location.Y)
g.DrawRectangle(Pens.Red, Rect(i))
newLocationX = newLocationX + 6
i += 1
Loop
End If
Last Edit: My math was way way wrong. Here is how I got it to work:
The resolution of the image width is 640
I needed to fill the width with 100 rectangles.
The math is 640 / 100 = 6.4 <-- the width of the rectangles
I thought that I had to repeat the For Loop 100 times to get 100 rectangles. this is not true. Repeat the For Loop the width of the picture box: 640 times!
The fact that you place rectangles in the picture box at a width 6.4 will automatically create 100 rectangles in a picture box with a width of 640.
here is the code that works:
Dim g As Graphics = Graphics.FromImage(kinectVideoBitMap)
Dim i As Integer = 0
Dim newLocationX As Integer = 0
Do Until i = 640
Dim rect(i) As Rectangle
rect(i) = New Rectangle
rect(i).Width = 6.4
rect(i).Height = 480
rect(i).Location = New System.Drawing.Point(video.Right - newLocationX, rect(i).Location.Y)
g.DrawRectangle(Pens.Red, rect(i))
newLocationX = newLocationX + 6.4
i += 1
Loop

Drawing Image in centre while maintaining the Aspect Ratio

Part 1 : In one of my projects, I want to display a Image in center of a Custom Control using GDI+ while maintaining the aspect ratio of the Image. Here is my code
Gdiplus::Image image(imagePathWStr); // imagePathWStr is the Image Path
int originalWidth = image.GetWidth();
int originalHeight = image.GetHeight();
// This code calculates the aspect ratio in which I have to draw the image
int16 cntrlwidth = controlPosition.right - controlPosition.left; // controlPosition is the custom Control Rect
int16 cntrlheigth = controlPosition.bottom - controlPosition.top;
float percentWidth = (float)cntrlwidth / (float)originalWidth;
float percentHeight = (float)cntrlheigth / (float)originalHeight;
float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
int newImageWidth = (int)(originalWidth * percent);
int newImageHeight = (int)(originalHeight * percent);
Gdiplus::RectF imageContainer;
imageContainer.X = controlPosition.left;
imageContainer.Y = controlPosition.top;
imageContainer.Width = controlPosition.right;
imageContainer.Height = controlPosition.bottom;
Gdiplus::Graphics gdiGraphics(hDC);
Gdiplus::Unit scrUnit = Gdiplus::UnitPixel;
gdiGraphics.DrawImage(&image, imageContainer, 0, 0, originalWidth, originalHeight, scrUnit);
However when the Control is resized vertically the image is moving to the right and not always stay in center, Also when Control is resized Horizontally, it is moving to the bottom. I am not able to figure out why.
I am using C++ on Windows.
Part 2: Now I have got a Rectangle drawn as well on top of this
Gdiplus::RectF cropRect;
cropRect.X = 100;
cropRect.Y = 100;
cropRect.Width = 300;
cropRect.Height = 300;
Gdiplus::Pen* myPen = new Gdiplus::Pen(Gdiplus::Color::White);
myPen->SetWidth(3);
gdiGraphics.DrawRectangle(myPen, cropRect);
Now When I resize the Control, Image is getting resized correctly but this rectangle is not, I multiplied Width and Height accordingly
Gdiplus::RectF cropRectangle;
cropRectangle.X = cropRect.GetLeft();
cropRectangle.Y = cropRec.GetTop();
cropRectangle.Width = (cropRec.Width)* percent;
cropRectangle.Height = (cropRec.Height ) * percent;
My Part 1 has been solved after Adrians Answer now I am stuck on Part 2 of my problems
Thanks
-Pankaj
when the size of the control changes you'll have a WM_SIZE message posted to your window. you must refresh your aspect ratio according to new size.

Making charts in RaphaelJS that are 100% width?

I have seen graphs in Flash & stuff that basically adapt nicely to whatever the size of the browser or flexible element they are inside of.... I'm not really too well versed with raphaelJS but can you do this, and if so, how?
In raphaeljs, you can call .setSize on a Raphael object to update its size. To adapt to runtime changes in browser window size, you can respond to a window resize event. Using jQuery, you could do:
// initialize Raphael object
var w = $(window).width(),
h = $(window).height();
var paper = Raphael($("#my_element").get(0), w,h);
$(window).resize(function(){
w = $(window).width();
h = $(window).height();
paper.setSize(w,h);
redraw_element(); // code to handle re-drawing, if necessary
});
This will get you a responsive SVG
var w = 500, h=500;
var paper = Raphael(w,h);
paper.setViewBox(0,0,w,h,true);
paper.setSize('100%', '100%');
Normally you could set the width to %100 and define a viewBox within SVG. But, Raphael JS manually set a width and height directly on your SVG elements, which kills this technique.
Bosh's answer is great, but it was distorting the aspect ratio for me. Had to tweak a few things to get it working correctly. Also, included some logic to maintain a maximum size.
// Variables
var width;
var height;
var maxWidth = 940;
var maxHeight = 600;
var widthPer;
function setSize() {
// Setup width
width = window.innerWidth;
if (width > maxWidth) width = maxWidth;
// Setup height
widthPer = width / maxWidth;
height = widthPer * maxHeight;
}
var paper = Raphael(document.getElementById("infographic"), 940, 600);
paper.setViewBox(0, 0, 940, 600, true);
window.onresize = function(event) {
setSize();
paper.setSize(width,height);
redraw_element();
}