941,512 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 11440
  • Python RSS
Mar 4th, 2007
0

Tkinter window background

Expand Post »
sigh...

I need a way to make an image as a background for a window, and still be able to put buttons and other controls on top of it. I even tried using wxPython's wxPanel, but it proved a real pain because I found the documentation very sparce... (google didn't even help).

Is there anyway I can do this? I've heard about the canvas but I have no idea how to use this...

Thanks.
Similar Threads
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Mar 4th, 2007
0

Re: Tkinter window background

My new machine has Windows Vista OS and I can't get Tkinter to work, but wxPython works fine ...
python Syntax (Toggle Plain Text)
  1. # create a background image on a wxPython panel
  2. # and show a button on top of the image
  3.  
  4. import wx
  5.  
  6. class Panel1(wx.Panel):
  7. """class Panel1 creates a panel with an image on it, inherits wx.Panel"""
  8. def __init__(self, parent, id):
  9. # create the panel
  10. wx.Panel.__init__(self, parent, id)
  11. try:
  12. # pick an image file you have in the working folder
  13. # you can load .jpg .png .bmp or .gif files
  14. image_file = 'roses.jpg'
  15. bmp1 = wx.Image(image_file, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
  16. # image's upper left corner anchors at panel coordinates (0, 0)
  17. self.bitmap1 = wx.StaticBitmap(self, -1, bmp1, (0, 0))
  18. # show some image details
  19. str1 = "%s %dx%d" % (image_file, bmp1.GetWidth(), bmp1.GetHeight())
  20. parent.SetTitle(str1)
  21. except IOError:
  22. print "Image file %s not found" % imageFile
  23. raise SystemExit
  24.  
  25. # button goes on the image --> self.bitmap1 is the parent
  26. self.button1 = wx.Button(self.bitmap1, id=-1, label='Button1', pos=(8, 8))
  27.  
  28. app = wx.PySimpleApp()
  29. # create a window/frame, no parent, -1 is default ID
  30. # change the size of the frame to fit the backgound images
  31. frame1 = wx.Frame(None, -1, "An image on a panel", size=(350, 400))
  32. # create the class instance
  33. panel1 = Panel1(frame1, -1)
  34. frame1.Show(True)
  35. app.MainLoop()
Attached Thumbnails
Click image for larger version

Name:	roses.jpg
Views:	1745
Size:	30.5 KB
ID:	2970  
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,789 posts
since Oct 2004
Mar 4th, 2007
0

Re: Tkinter window background

quick question. Can I use Tkinter and wxpython in the same app? (See i built this splash screen class in Tkinter --doesn't use buttons, but does a lot of work (loading images/files/wat not)).

If not, is there a way to remove the title bar and border in wxpython like
python Syntax (Toggle Plain Text)
  1. root.overideredirect(1)
does in Tkinter?
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Mar 5th, 2007
0

Re: Tkinter window background

No, you can not use Tkinter and wxPython in the same application. Their eventloops would clash.

Yes you can remove the title bar from a wx.Frame ...
Quote ...
A frame without titlebar:
You can change frame style to wxMINIMIZE_BOX, but now you have
to supply an exit button somewhere.
I finally doctored the Python25 Vista installation to make Tkinter work. Here is a similar code for Tkinter ...
python Syntax (Toggle Plain Text)
  1. # use a Tkinter label as a panel/frame with a background image
  2. # (note that Tkinter reads only GIF and PGM/PPM images)
  3.  
  4. import Tkinter as tk
  5.  
  6. root = tk.Tk()
  7. root.title('background image')
  8.  
  9. # pick a .gif image file you have in the working directory
  10. image1 = tk.PhotoImage(file="roses.gif")
  11. w = image1.width()
  12. h = image1.height()
  13.  
  14. root.geometry("%dx%d+0+0" % (w, h))
  15.  
  16. # tk.Frame has no image argument
  17. # so use a label as a panel/frame
  18. panel1 = tk.Label(root, image=image1)
  19. panel1.pack(side='top', fill='both', expand='yes')
  20.  
  21. button2 = tk.Button(panel1, text='button2')
  22. button2.pack(side='top')
  23.  
  24. # save the panel's image from 'garbage collection'
  25. panel1.image = image1
  26.  
  27. # start the event loop
  28. root.mainloop()
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,789 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Disabling the title bar?
Next Thread in Python Forum Timeline: Python/Tkinter Imagebutton using Base64





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC