Combine PySide and PyGame - python

I want to develop an UI containing both forms for the user to configure the app and also a "render" part which will print some sprites for example.
Is it possible to combine PySide and PyGame in the same application? For example, how can i get a PySide Frame containing a Pygame application?
Thank you

The simplest, most natural solution is probably just to use a QGraphicsView. E.g. for sprites, implement a subclass of QGraphicsItem, and override the paint() function to display the appropriate frame.
If you absolutely insist on using PyGame, you could easily run the PySide and PyGame bits in separate windows. For embedding PyGame inside a PySide widget, it's probably going to be much harder, and the PySide widgets won't gel as seamlessly (you can actually stick GUI elements like buttons inside a QGraphicsView and transform them!).

Related

import a tkinter widget in wxpython application as a panel

I created a analog rpm gauge using the canvas widget of Tkinter and I want to import it in a wx GUI application (as a panel, maybe). Is there any way to do it or I must rewrite this widget in wx?
There is no (simple) way to do that - WxWidgets is an abstraction over different toolkits in different systems, and use different mainloop functions, while Tkinter has its own mainloop - that is to start with.
So making that work would at leas require:
that you'd setup different threads able to run both mainloops in
paralell,
finding a way to get Tkinter to render the widget to an
in memory bitmap
create a custom widget in wx which would render
that bitmap to the screen
and map events on it back to Tkinter, if
it is supposed to respond events
So you are definitely better of writting the widget again.
WxPython has a speed meter widget just use that instead.
import wx.lib.agw.speedmeter
You would have to rewrite the widget in wxPython or find a widget that does the same thing that's already included with wx. Tkinter is a completely different GUI toolkit that draws its own widgets and is based on TCL whereas wxPython is a wrapper around wxWidgets which is based on C++. There is no easy way to embed a widget from Tkinter into wxPython.
As Yoriz mentioned, you might be able to use the speedmeter widget in wxPython. Check out the wxPython demo package as it will show you how to use that widget and most of wxPython's other widgets. Hopefully you can find something that's already included. Otherwise, you may want to take a look at the following page:
http://wiki.wxpython.org/CreatingCustomControls

Best Qt Widget to use for properties window... in PySide?

This question is the exact same as this one except that I'm looking for such a widget to be used in PySide. Anyone knows some code out there that provide easy-to-reuse property editor widget?
There is the proposal of using QTreeView or QTableView to build such a widget that is an option with PySide, but this is not a straight forward solution...
I don't know if you would be OK with this, but if you are happy to add the whole of pyqtgraph as a dependency, then you might want to try using pyqtgraph's ParameterTree.
There is a pretty comprehensive set of examples, just install pyqtgraph and then run:
import pyqtgraph.examples
pyqtgraph.examples.run()
Launch the ParameterTree example!

wxPython, or PyGame canvas within wxPython -> which is faster?

My goal is to capture frames from a webcam as efficiently as possible using OpenCV. At the moment I'm able to capture 30FPS 6408*480 drawing directly onto a wxPython panel using the standard drawing context (BufferedPaintDC), with about 15% CPU usage (older Core Duo processor). What I'm curious is what sort of performance boost (if any) I'll see if I embed a PyGame canvas within a wxPython frame, and draw directly to the PyGame canvas.
What I'm not sure about is whether the bottleneck is the wxPython frame, and if embedding a PyGame canvas will actually do anything. Or does the wxPython frame act simply like a container and has no influence on the PyGame canvas? I'm hoping I'm making sense here.
The other option would be to use PyGame exclusively, however I really like the functionality of the wxPython widgets, so I'd hate to lose that.
Or is there a faster canvas that I can integrate into wxPython that I'm not aware of?
Thoughts? Thanks.
I don't know why you'd want to embed a gaming library into wxPython in the hopes of gaining a performance boost. Personally, I don't think that will happen. You should take a look at the currently supported drawing canvases that wxPython provides instead or explain what you're trying to do. People have done in games in wxPython...
Anyway, the main drawing interfaces for wx today are wx.GCDC / wx.GraphicsContext, cairo, FloatCanvas, or GLCanvas. Of course, there are also wx.DC, wx.PaintDC and the one you found as well.

Background Image on Jython GUI

I am trying to create a GUI in Jython. I want to import a background image that I can place buttons and textfields on. I've already created the frame with the buttons and labels in their appropriate places, I just need to know how to import a background image. The GUI is implemented in Jython.
Take a look at the Java swing material, essentially you are just using the same api in python syntax. This might help: http://forums.sun.com/thread.jspa?threadID=599393

pyGame within a pyGTK application

What is the best way to use PyGame (SDL) within a PyGTK application?
I'm searching for a method that allows me to have a drawing area in the GTK window and at the same time being able to manage both GTK and SDL events.
I've never attempted it myself, but hearing plenty about other people who've tried, it's not a road you want to go down.
There is the alternative of putting the gui in pygame itself. There are plenty of gui toolkits built specifically for pygame that you could use. Most of them are rather unfinished, but there are 2 big, actively maintained ones: PGU and OcempGUI. The full list on the pygame site is here.
You may be interested in this message thread. Looks like they recommend against it.
PyGame works much better when it can manage its own window, or even better, use the whole screen. GTK has flexible enough widgets to allow creation of a drawing area.
This page may help, though, if you want to try it.
There's a simple solution that might work for you.
Write the PyGTK stuff and PyGame stuff as separate applications. Then from the PyGTK application call the PyGame application, using os.system to call the PyGame application. If you need to share data between the two then either use a database, pipes or IPC.
http://faq.pygtk.org/index.py?file=faq23.042.htp&req=show mentions it all:
You need to create a drawing area and set the environment variable SDL_WINDOWID after it's realized:
import os
import gobject
import gtk
import pygame
WINX = 400
WINY = 200
window = gtk.Window()
window.connect('delete-event', gtk.main_quit)
window.set_resizable(False)
area = gtk.DrawingArea()
area.set_app_paintable(True)
area.set_size_request(WINX, WINY)
window.add(area)
area.realize()
# Force SDL to write on our drawing area
os.putenv('SDL_WINDOWID', str(area.window.xid))
# We need to flush the XLib event loop otherwise we can't
# access the XWindow which set_mode() requires
gtk.gdk.flush()
pygame.init()
pygame.display.set_mode((WINX, WINY), 0, 0)
screen = pygame.display.get_surface()
image_surface = pygame.image.load('foo.png')
screen.blit(image_surface, (0, 0))
gobject.idle_add(pygame.display.update)
window.show_all()
while gtk.event_pending():
# pygame/SDL event processing goes here
gtk.main_iteration(False)
I tried doing this myself a while ago, and I never got it to work perfectly. Actually I never got it to work at all under Windows, as it kept crashing the entire OS and I ran out of patience. I continued to use it though as it was only important it ran on Linux, and was only a small project. I'd strongly recommend you investigate alternatives. It always felt like a nasty hack, and made me feel dirty.
The Sugar project has several Activities built with PyGTK and PyGame.
They wrote a support lib to achieve this, called Sugargame. You should be able to modify it for regular PyGTK apps instead of Sugar.
Here's a chapter in Sugar's development book about how to use it.
The lib allows for communicating events between GTK and PyGame.
Enjoy!

Categories

Resources