2017-04-07 33 views
0

我试图将图像应用于背景。随着我的按钮在给定的图像(与他们的图片)顶部。我尝试了Canvas,PIL,但其中任何一个都不起作用,或者图像超出了一切。有任何想法吗? (再次对我的代码的方向感到抱歉)。Python 3.X,无法获得我的按钮后面的背景图像

from tkinter import * 
import os 
from PIL import Image, ImageTk 


# create the main window and title 



app = Tk() 
app.title("Virtual Receptionist") 

app.geometry("300x300") 
app.configure(background="black") 

# background images 




label1 = Label(app, text="Please select a name from the Directory or press 
    Deliveries") 
label1.place(x= 64, y = 22, height=135, width=20) 

mainwindow = app 


"""Define any items that will be utilized when the button(s) are pressed (Close Window, Bat files for calling, any additional options)""" 


# Testing area 



# closed the window commandlet# 

    def closewindow(): 
     exit() 



# call numbers listed in individual bat files for use when button is pressed (each bat will have it own number or extension) 

def callOffice(): 
    os.system("c:/netis/phone/callfile.bat") 
def callOffice1(): 
    os.system("c:/netis/phone/callfile.bat") 
def callOffice2(): 
    os.system("c:/netis/phone/callfile.bat") 
def callOffice3(): 
    os.system("c:/netis/phone/callfile.bat") 
def callOffice4(): 
    os.system("c:/netis/phone/callfile.bat") 
def callOffice5(): 
    os.system("c:/netis/phone/callfile.bat") 
def callOffice6(): 
    os.system("c:/netis/phone/callfile.bat") 
def callOffice7(): 
    os.system("c:/netis/phone/callfile.bat") 
def callOffice8(): 
    os.system("c:/netis/phone/callfile.bat") 
def callOffice9(): 
    os.system("c:/netis/phone/callfile.bat") 
def callOffice10(): 
    os.system("c:/netis/phone/callfile.bat") 
def callOffice11(): 
    os.system("c:/netis/phone/callfile.bat") 


# images for the pictures that display over the buttons listed below 

photo1=PhotoImage(file="image folder/customerimg1.png") 
photo2=PhotoImage(file="image folder/customerimg2.png") 
photo3=PhotoImage(file="image folder/customerimg3.png") 
photo4=PhotoImage(file="image folder/customerimg4.png") 
photo5=PhotoImage(file="image folder/customerimg5.png") 
photo6=PhotoImage(file="image folder/customerimg6.png") 
photo7=PhotoImage(file="image folder/customerimg7.png") 
photo8=PhotoImage(file="image folder/customerimg8.png") 
photo9=PhotoImage(file="image folder/customerimg9.png") 
photo10=PhotoImage(file="image folder/customerimg10.png") 
photo11=PhotoImage(file="image folder/customerimg11.png") 
photo12=PhotoImage(file="image folder/customerimg12.png") 
photoc=PhotoImage(file="image folder/close.png") 



# creating the button schema for the extensions 

button1 = Button(mainwindow, text="Close Window", command=closewindow, 
    image=photoc, bg='Dark Slate Gray') 
button2 = Button(mainwindow, text="client name1", command=callOffice, 
    image=photo1, bg='Dark Slate Gray') 
button3 = Button(mainwindow, text="client name2", command=callOffice1, 
    image=photo2, bg='Dark Slate Gray') 
button4 = Button(mainwindow, text="client name3", command=callOffice2, 
    image=photo3, bg='Dark Slate Gray') 
button5 = Button(mainwindow, text="client name4", command=callOffice3, 
    image=photo4, bg='Dark Slate Gray') 
button6 = Button(mainwindow, text="client name5", command=callOffice4, 
    image=photo5, bg='Dark Slate Gray') 
button7 = Button(mainwindow, text="client name6", command=callOffice5, 
    image=photo6, bg='Dark Slate Gray') 
button8 = Button(mainwindow, text="client name7", command=callOffice6, 
    image=photo7, bg='Dark Slate Gray') 
button9 = Button(mainwindow, text="client name8", command=callOffice7, 
    image=photo8, bg='Dark Slate Gray') 
button10 = Button(mainwindow, text="client name9", command=callOffice8, 
    image=photo9, bg='Dark Slate Gray') 
button11 = Button(mainwindow, text="client name10", command=callOffice9, 
    image=photo10, bg='Dark Slate Gray') 
button12 = Button(mainwindow, text="client name11", command=callOffice10, 
    image=photo11, bg='Dark Slate Gray') 
button13 = Button(mainwindow, text="client name12", command=callOffice11, 
    image=photo12, bg='Dark Slate Gray') 



#Configs for the buttons Size(s):# 

button1.config(image=photoc, width="155", height="150") 
button2.config(image=photo1, width="155", height="150") 
button3.config(image=photo2, width="155", height="150") 
button4.config(image=photo3, width="155", height="150") 
button5.config(image=photo4, width="155", height="150") 
button6.config(image=photo5, width="155", height="150") 
button7.config(image=photo6, width="155", height="150") 
button8.config(image=photo7, width="155", height="150") 
button9.config(image=photo8, width="155", height="150") 
button10.config(image=photo9, width="155", height="150") 
button11.config(image=photo10, width="155", height="150") 
button12.config(image=photo11, width="155", height="150") 
button13.config(image=photo12, width="155", height="150") 


# Button Orentation within the window itself (calling to grid func)# 

button1.grid(row=0, column=0) 
button2.grid(row=2, column=4) 
button3.grid(row=2, column=5) 
button4.grid(row=2, column=6) 
button5.grid(row=2, column=7) 
button6.grid(row=2, column=8) 
button7.grid(row=2, column=9) 
button8.grid(row=4, column=4) 
button9.grid(row=4, column=5) 
button10.grid(row=4, column=6) 
button11.grid(row=4, column=7) 
button12.grid(row=4, column=8) 
button13.grid(row=4, column=9) 

app.mainloop() 
+1

不要对你的代码感到抱歉。 _修补你的code_。如果你不愿意花几分钟格式化你的问题,为什么你会期望陌生人花时间去理解它?另外,请阅读并遵循以下建议:[如何创建最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。如果您的问题出在按钮和背景图片上,那么您将不需要13个按钮来说明问题。 –

回答

0

要看到背景的文本在该行调整的数字:

label1.place(x= 64, y = 22, height=135, width=20) 

为数字使文字没有更多的隐藏在后面的按钮:

label1.place(x=200, y=22, height=20, width=400) 

另一种选择是使用一个大的背景图像,以便您可以在任何情况下看到按钮后面的图像:

mainwindow = app 

filename = PhotoImage(file="/path/To/Background/image.png") 
background_label = Label(app, image=filename) 
background_label.place(x=0, y=0, relwidth=1, relheight=1) 

欲了解更多关于tkinter幕后发生的事情,请在stackoverflow.com上的搜索框中输入术语“tkinter背景图像” - 有很多关于此主题的有价值的信息。

玩得开心:)编码。

+0

另外,感谢您的回复。 – codex123098

+0

所以,只需将代码片段更高一些地放在测试区域中,更新一个新的jpg文件,并且它可以工作,您只需注意这是一个软电话的放置位置,它会调用一个带有exe文件的球棒该软件的文件和一个数字(在cmd下运行),其设计是在触摸屏上制作一个虚拟接待系统,再次感谢@ Claudio !!!! – codex123098