473,698 Members | 2,833 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 2851
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
2876
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
2548
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
4456
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
2820
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
1432
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
6817
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
5597
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
1347
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
3027
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
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
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...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.