473,748 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

wxPython AND PyGame: IMAGES

tharden3
916 Contributor
Ok, I've been going through these tutorials for both PyGame and wxPython, but I've hit a big speed bump. In each tutorial, I've gotten to the point where I need to "import" or "load" images for various purposes (animation, icons, etc.). Each time I follow the tutorial, I can't quite get it right... please don't tell me "Hey, you can't use the pictures in the tutorial example because those are not the directories, paths, or pics that are on your computer". I know that already. Help me to figure out (please provide code and examples in your posts) how to use images on my computer. I will provide the code in the tutorials, links to the tutorials, and the error I get from my code. This might require you to answer with some depth and instruction, but it would be a big help to me and my progress in learning python!

P.S.
- When I show my code, it is not the only thing I tried. Don't just tell me I'm wrong, provide a clear answer.
- It is basically the same problem in wxPython and PyGame... it's all about making images work in these apps

I've been studying and working very hard in python, and this has really stopped me dead in my tracks. Please provide whatever help you can.

What the tutorial says to do:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2.  
  3. # menuexample.py
  4.  
  5. import wx
  6.  
  7. class MenuExample(wx.Frame):
  8.     def __init__(self, parent, id, title):
  9.         wx.Frame.__init__(self, parent, id, title, size=(250, 150))
  10.  
  11.         menubar = wx.MenuBar()
  12.         file = wx.Menu()
  13.         quit = wx.MenuItem(file, 1, '&Quit\tCtrl+Q')
  14.         quit.SetBitmap(wx.Bitmap('icons/exit.png'))
  15.         file.AppendItem(quit)
  16.  
  17.         self.Bind(wx.EVT_MENU, self.OnQuit, id=1)
  18.  
  19.         menubar.Append(file, '&File')
  20.         self.SetMenuBar(menubar)
  21.  
  22.         self.Centre()
  23.         self.Show(True)
  24.  
  25.     def OnQuit(self, event):
  26.         self.Close()
  27.  
  28. app = wx.App()
  29. MenuExample(None, -1, '')
  30. app.MainLoop()
What I tried (but not the only thing I tried):

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2.  
  3. # menuexample.py
  4.  
  5. import wx
  6.  
  7. class MenuExample(wx.Frame):
  8.     def __init__(self, parent, id, title):
  9.         wx.Frame.__init__(self, parent, id, title, size=(250, 150))
  10.  
  11.         menubar = wx.MenuBar()
  12.         file = wx.Menu()
  13.         quit = wx.MenuItem(file, 1, '&Quit\tCtrl+Q')
  14.         quit.SetBitmap(wx.Bitmap('C:\Python24\tony.bmp'))
  15.         file.AppendItem(quit)
  16.  
  17.         self.Bind(wx.EVT_MENU, self.OnQuit, id=1)
  18.  
  19.         menubar.Append(file, '&File')
  20.         self.SetMenuBar(menubar)
  21.  
  22.         self.Centre()
  23.         self.Show(True)
  24.  
  25.     def OnQuit(self, event):
  26.         self.Close()
  27.  
  28. app = wx.App()
  29. MenuExample(None, -1, '')
  30. app.MainLoop()
Jul 12 '08 #1
2 2856
jlm699
314 Contributor
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. # menuexample.py
  3.  
  4. import wx
  5.  
  6. class MenuExample(wx.Frame):
  7.     def __init__(self, parent, id, title):
  8.         wx.Frame.__init__(self, parent, id, title, size=(250, 150))
  9.  
  10.         menubar = wx.MenuBar()
  11.         file = wx.Menu()
  12.         quit = wx.MenuItem(file, 1, '&Quit\tCtrl+Q')
  13.         quit.SetBitmap(wx.Bitmap('C:\Python24\tony.bmp'))
  14.         file.AppendItem(quit)
  15.  
  16.         self.Bind(wx.EVT_MENU, self.OnQuit, id=1)
  17.  
  18.         menubar.Append(file, '&File')
  19.         self.SetMenuBar(menubar)
  20.  
  21.         self.Centre()
  22.         self.Show(True)
  23.  
  24.     def OnQuit(self, event):
  25.         self.Close()
  26.  
  27. app = wx.App()
  28. MenuExample(None, -1, '')
  29. app.MainLoop()
I've gotten bitmaps to work before in wxPython, however I've never attempted such an act in PyGame. In order to get a bitmap into a wx-friendly format I used the following:
Expand|Select|Wrap|Line Numbers
  1. bmp = wx.Image('Splash.bmp', wx.BITMAP_TYPE_BMP, -1).ConvertToBitmap()
  2.  
And then in your code you would use
Expand|Select|Wrap|Line Numbers
  1.         quit.SetBitmap(bmp)
  2.  
Please let us know if that helped at all... there are many things about bitmaps that make image handling touchy. Most of the problems you'll face revolve around how Python recognizes the image file. That's why I needed to specify the type of the image with wx.BITMAP_TYPE_ BMP
Jul 14 '08 #2
tharden3
916 Contributor
I've gotten bitmaps to work before in wxPython, however I've never attempted such an act in PyGame. In order to get a bitmap into a wx-friendly format I used the following:
Expand|Select|Wrap|Line Numbers
  1. bmp = wx.Image('Splash.bmp', wx.BITMAP_TYPE_BMP, -1).ConvertToBitmap()
  2.  
And then in your code you would use
Expand|Select|Wrap|Line Numbers
  1.         quit.SetBitmap(bmp)
  2.  
Please let us know if that helped at all... there are many things about bitmaps that make image handling touchy. Most of the problems you'll face revolve around how Python recognizes the image file. That's why I needed to specify the type of the image with wx.BITMAP_TYPE_ BMP
Thanks for the help. I did end up getting this to work (in wxPython, not pygame), but with PNG and JPEG files. But I'll be able to use this in the future.
Jul 14 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2886
by: jblazi | last post by:
I am having this problem: I use Python for my teaching and right now we are trying to implement some sort of very simple graphical game the pupils can play across the Internet. For this, we use wxPython. Now it has turned out that wxPython does not implement wxWidget sockets, so we have to use Python sockets instead. Implementing a separate thread for this would be too complicated and I am going to use the wxIdle event to do the socket...
3
2552
by: Erik Bethke | last post by:
Hello All, I am having a great time with python and pygame, and last night I took a long look at the wxPython demo. I think that rocks as well. So now, my question is do wxPython and PyGame work together? How is the windows event loop managed? How is the display window managed?
3
4459
by: tlviewer | last post by:
hello, The script below is a prototype for loading playing card images from the bitmap resource in cards.dll (WinXP) I like the idea of not keeping copies of the images as files. I'm able to extract the card images as files, then load them in pygame as a surface, but I keep getting errors
2
2825
by: DK | last post by:
I'm somewhat new to Python but not to programming. I just want to know if it's possible to have a SINGLE wxPython frame containing two sections where one will contain widgets and the other will host an animation done using PyGame. More specifically, I want to have a list of names of animations on the left and depending on what is selected, run the PyGame animation in the main area.
2
1436
by: Blaze Bresko | last post by:
Hi, I am trying to make a game using either livewires or pygame. The game is tetris. Right now I have gotten the program to a point where everything works (as in user input, score, lines, etc), except I can't get more than one block to work. Right now I have the user playing a game where a single block falls at a time instead of one of the seven different patterns. I was curious how you would program the seperate images to fall together...
1
6825
by: liuliuliu | last post by:
hi -- sorry if this is trivial -- but how do you make a screenshot of a pygame display? i have a surface which is basically the entire visible screen -- how do you write this surface as an image file during specific events in the script execution? image format doesnt matter. thanks! christine
4
5608
by: Iacopo.Marmo | last post by:
Hi! I need to manipulate multicolor strings, i.e. strings with a color associated with each letter. Any suggestions?
0
1349
by: eric dexter | last post by:
I am looking for a simple example of moving a bitmap on a wxpython canvas. I am wanting to do something that most people do with pygame (because pygame is slow but working on my computer may be my graphics card). I have been searching through google and I haven't found that much (found some tkinter stuff though)... any help would be nice.
5
3033
by: defn noob | last post by:
Im using PyGame to draw images of graphs and trees. Howver right now i am looping using: while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill(screencolor) pygame.draw.circle(screen, linecolor, (500, 20), 12, 0)
0
8983
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9236
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6072
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4592
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.