2014-03-29 95 views
2

我正在設置背景圖像,並在該圖像添加標籤,按鈕和所有。一切都來了,但不是背景圖像上,它是這樣的:如何使用Tkinter設置背景圖像到窗口python 2.7

enter image description here

而且我的代碼是:

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() 

如何正確適合的背景圖像?

在此先感謝!

+0

我認爲你應該嘗試[地方](http://effbot.org/tkinterbook/place.htm),而不是包() – Gogo

+0

檢查此:同樣的問題:http://stackoverflow.com/questions/10158552/如何把圖像作爲背景在python – sshashank124

+0

@shaktimaan,它的工作,但只有背景正在顯示,並沒有其他小工具正在顯示 –

回答

3

您應該使用place()來放置圖像&然後使用grid()(我個人更喜歡網格)或pack()其他小部件。

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() 

的部件是不可見的原因是因爲你使用兩種不同的parents,即,app(其Example類的Instance,所以不要用這個)和root