3

I am planning to write a card game in python, and now looking for a GUI (I'm new to Python). I have so far tried out two GUI's :TK(inter) and wxPython.

Neither of them seem to be able , and correct me if I'm wrong, to do this :

dragging a panel with an image of a Card in it

And it's not even about the dragging. This is the situation

  1. I want to place the Card in a container. It should be able to show both front and backside of the card so I want each card to be a panel rather than a bare image.
  2. The image should be able to have transparent areas (card corners are rounded)
  3. The panel (container) should be able to be transparant (So the card can be dragged over different backgroundcolors, the table other cards,....)

TKinter : can't do 3 as I understand it

wxPython : can't do 2 as I understand it.

I can go along and discover GUI after GUI, but this is kind of time-consuming, so my questions are :

  1. Is it so that TKinter and wxPython have these shortcomings?
  2. If so, what GUI to go to?

I've heard of pygame, but it's really not that much I am asking of the GUI. Swing (java) could do it, and that's not gaming centered either.

Peter
  • 44,153
  • 43
  • 129
  • 175

4 Answers4

8

As you want to write a game, I would recommend to not use a GUI library. Instead, you should look for Game Libraries like PyGame, PyOpenGl or Kivy (aside others). They should all fit up your needs.

Niklas R
  • 14,369
  • 23
  • 82
  • 179
  • Can you say what is it in 'a game' precisely that makes you recommend against a gui library? – Peter Feb 07 '12 at 18:17
  • @Peter They are game-engines and optimized for this. (*PyGame* is built on top of the SDL library, *PyOpenGL* on OpenGL of course) You gain more functionality from using these packages (e.g. Anti-aliasing, built-in complex rendering operations [like gradients], etc.) You also have more references, there are many Open-source games for these platforms and many Demo-scripts. – Niklas R Feb 07 '12 at 18:17
  • Niklas, I appreciate your links (and gave you +1 for it). But this is a card game. Cards move, that's it. Is there any advantage for this scenario? – Peter Feb 07 '12 at 18:19
  • @Peter Ty. Well, those libraries fit your claims. I don't know how the workflow with *Tkinter* and *wx* is, but I like the one of *Kivy* and *PyGame*. I haven't worked with *PyOpenGL* yet, but it seems to be very nice, too. – Niklas R Feb 07 '12 at 18:35
  • Thanks, I will be looking in them later, espacially kivy – Peter Feb 07 '12 at 18:37
  • @Peter You may be interested into [this video](http://www.youtube.com/watch?v=1Qa98oSPgi0) where Kivy was used to create a multi-touch game on a tablet-PC. – Niklas R Feb 07 '12 at 18:38
  • Pygame it'll be, tx! (kivy is win7+, not suitable now) – Peter Feb 07 '12 at 19:51
6

wxPython can do transparency, you just need to use Transparent pngs.

png = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
wx.StaticBitmap(self, -1, png, (10, 5), (png.GetWidth(), png.GetHeight()))

Showing a .png image in a window in wxPython

Community
  • 1
  • 1
mcneo
  • 189
  • 5
  • what about http://stackoverflow.com/questions/6738440/display-a-transparent-png-in-wxpython ? – Peter Feb 07 '12 at 18:00
  • wxPython has a fantastic demo application that includes an example for transparent pngs. I think the question you are referring too is in regards to putting the transparent png over the window controls. If you need even greater transparency effects, wxPython can also create windows out of transparent pngs. I think wxPython is where I would start. Pygame is great, but I think it would increase your development time considerably for what wxPython will give you built in. – mcneo Feb 07 '12 at 18:14
  • But : isn't this an expensive operation? It's a conversion just for "kinda" (bitmap masked) transparancy. I started my game out slicing a pic and even that I had to change for performance. I have 52 panels with 52 cards, cards need to be draggable, and so on... – Peter Feb 07 '12 at 18:21
  • Your example is not working : the transparancy is faked on the background, but not really transparant. – Peter Feb 07 '12 at 18:40
1

You should take a look at the wxPython demo for examples and ask on the wxPython mailing list to see if is anyone who has made something similar. In the demo, there is a DragImage demo that does some of what you're looking for.

Otherwise, one of the Python gaming libraries may suit your needs better. PyGame is probably the most well known. For a while, Pyglet was gaining momentum. See also wxPython or pygame for a simple card game?

Community
  • 1
  • 1
Mike Driscoll
  • 31,394
  • 6
  • 39
  • 83
  • I've tried it, but nothing is quite the same. I am trying pygame right now, and it's already working, thanks – Peter Feb 07 '12 at 19:53
1

Tkinter can handle transparent gifs. Your "container" requirements are a bit hard to understand, though. I don't understand what transparency has to do with the ability to drag it over different colored backgrounds.

Certainly you can group objects (images, rounded retangles, ec) together on a canvas, and those objects can have blank spaces between / around them, and you can certainly draw round corners and drag objects.

Bryan Oakley
  • 310,202
  • 36
  • 445
  • 584
  • Well, I need a containter, and as I am not mistaken, I read an answer of on SO somewhere stating that TKinter widgets don't handle transparacy? Can you give me an example of how to declare a transparent Label? – Peter Feb 07 '12 at 21:08
  • @Peter: I'm not sure what you mean by "container". If you placed two images side-by-side on a canvas, drew a rounded rectangle around them, does that match your definition of "container"? If you are thinking about implementing this as a bunch of widgets rather than drawing objects on a canvas, then you are correct: tk widgets don't handle transparency. – Bryan Oakley Feb 07 '12 at 21:14
  • I want a playingcard in another 'object', so that I can drag that object. A label for example. That label is the card, the image on it can be the front or the card or the back. That label than should be transparant. So that's my problem I guess. – Peter Feb 08 '12 at 08:29