2

I was working on setting the background image and upon that image adding the labels, buttons and all. everything is coming, but not on the background image, its like this:

enter image description here

And my code is:

from Tkinter import Tk, Frame, BOTH
import Tkinter
from PIL import Image, ImageTk

class Example(Frame):

   def __init__(self, parent):
      Frame.__init__(self, parent)            
      self.parent = parent        
      self.initUI()

   def initUI(self):

      self.parent.title("PISE")
      self.pack(fill=BOTH, expand=1)

root = Tk()
root.geometry("1111x675+300+300")
app = Example(root)

im = Image.open('wood.png')
tkimage = ImageTk.PhotoImage(im)
Tkinter.Label(root,image = tkimage).pack()

custName = StringVar(None)
yourName = Entry(app, textvariable=custName)
yourName.pack()

relStatus = StringVar()
relStatus.set(None)

labelText = StringVar()
labelText.set('Accuracy Level')
label1 = Label(app, textvariable=labelText, height=2)
label1.pack()

radio1 = Radiobutton(app, text='100%', value='1', variable = relStatus, command=beenClicked1).pack()
radio2 = Radiobutton(app, text='50%', value='5', variable = relStatus, command=beenClicked5).pack()

root.mainloop()  

How to fit the background image properly?

Thanks in advance!

Alok
  • 2,629
  • 4
  • 28
  • 40
varsha_holla
  • 852
  • 7
  • 23
  • 41
  • I think you should try [place](http://effbot.org/tkinterbook/place.htm) instead of pack() – Alok Mar 29 '14 at 06:38
  • Check this: Same Problem: http://stackoverflow.com/questions/10158552/how-to-put-a-image-as-a-background-in-tkinter-in-python – sshashank124 Mar 29 '14 at 07:06
  • @shaktimaan , its working but only background is being displayed, and no other widgets are being displayed – varsha_holla Mar 29 '14 at 07:41

1 Answers1

3

You should use place() for placing the image & then use grid() (personally i prefer grid) or pack() for other widgets.

from Tkinter import Tk, Frame, BOTH
import Tkinter
from PIL import Image, ImageTk

class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)            
        self.parent = parent        
        self.initUI()

    def initUI(self):
        self.parent.title("PISE")
        self.pack(fill=BOTH, expand=1)

root = Tk()
root.geometry("1111x675+300+300")
app = Example(root)

im = Image.open('Test1.png')
tkimage = ImageTk.PhotoImage(im)
myvar=Tkinter.Label(root,image = tkimage)
myvar.place(x=0, y=0, relwidth=1, relheight=1)

custName = StringVar(None)
yourName = Entry(root, textvariable=custName)
yourName.pack()

relStatus = StringVar()
relStatus.set(None)

labelText = StringVar()
labelText.set('Accuracy Level')
label1 = Label(root, textvariable=labelText, height=2)
label1.pack()

def beenClicked1():
    pass

def beenClicked5():
    pass

radio1 = Radiobutton(root, text='100%', value='1', variable = relStatus, command=beenClicked1).pack()
radio2 = Radiobutton(root, text='50%', value='5', variable = relStatus, command=beenClicked5).pack()

root.mainloop()

The reason the widgets were not visible was because you were using two different parents ,i.e, app(its an Instance of class Example,so don't use this) and root.

Alok
  • 2,629
  • 4
  • 28
  • 40