Tkinter window background

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2007
Posts: 1,613
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Tkinter window background

 
0
  #1
Mar 4th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,069
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 938
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Tkinter window background

 
0
  #2
Mar 4th, 2007
My new machine has Windows Vista OS and I can't get Tkinter to work, but wxPython works fine ...
  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
roses.jpg  
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,613
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Tkinter window background

 
0
  #3
Mar 4th, 2007
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
  1. root.overideredirect(1)
does in Tkinter?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,069
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 938
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Tkinter window background

 
0
  #4
Mar 5th, 2007
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 ...
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 ...
  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()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC