我试图将图像应用到背景中。我的按钮位于给定图像的顶部(带有图片)。我尝试过Canvas,PIL,但是他们中的任何一个都没有工作,或者图像来自所有东西。有任何想法吗? (再次抱歉我的代码方向)。
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()
答案 0 :(得分: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 background image" - 关于这个主题有很多有价值的信息。