如何在Tkinter中为我的应用程序添加背景图像?

时间:2017-06-24 15:36:24

标签: python-3.x tkinter tk

代码图片需要完美地符合屏幕尺寸。我看了一堆教程,但似乎没有任何工作。我尝试添加一个画布,但它覆盖了屏幕的一半。我的所有按钮都在图像本身下面而不是在它上面。这让我神经紧张。 这是我的代码:

import tkinter as tk

import PIL
from PIL import Image, ImageTk
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox

root = Tk()

w = Label(root, text="Send and receive files easily")
w.config(font=('times', 32))
w.pack()


def create_window():
window = tk.Toplevel(root)
window.geometry("400x400")

tower= PhotoImage(file="D:/icons/tower.png")
towlab=Button(root,image=tower, command=create_window)
towlab.pack()



class Window(Frame):
def __init__(self, master=None):
    Frame.__init__(self, master)
    self.master = master
    self.init_window()

def init_window(self):
    self.master.title("Bifrost v1.0")



    self.pack(fill=BOTH, expand=1)
    self.img1 = PhotoImage(file="D:/icons/download.png")
    self.img2 = PhotoImage(file="D:/icons/upload.png")

    sendButton = Button(self, image=self.img2)
    sendButton.place(x=305, y=15)
    receiveButton = Button(self, image=self.img1)
    receiveButton.place(x=355, y=15)

    menu = Menu(self.master)
    self.master.config(menu=menu)

    file = Menu(menu)
    file.add_command(label='Exit', command=self.client_exit)
    menu.add_cascade(label='File', menu=file)

    edit = Menu(menu)
    edit.add_command(label='abcd')
    menu.add_cascade(label='Edit', menu=edit)

    help = Menu(menu)

    help.add_command(label='About Us', command=self.about)
    menu.add_cascade(label='Help', menu=help)

def callback():
    path = filedialog.askopenfilename()
    e.delete(0, END)  # Remove current text in entry
    e.insert(0, path)  # Insert the 'path'
    # print path

w = Label(root, text="File Path:")
e = Entry(root, text="")
b = Button(root, text="Browse", fg="#a1dbcd", bg="black", command=callback)

w.pack(side=TOP)
e.pack(side=TOP)
b.pack(side=TOP)

def client_exit(self):
    exit()

def about(self):
    top = Toplevel()
    msg = Message(top, text="This is a project developed by Aditi,Sagar and 
Suyash as the final year project.",
                  font=('', '15'))
    msg.pack()
    top.geometry('200x200')

  button = Button(top, text="Okay", command=top.destroy)
    button.pack()
    top.mainloop()
root.resizable(0,0)
#size of the window
root.geometry("700x400")
app = Window(root)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

叠加元素很棘手。我认为这可能与你正在寻找的大致相同。至少它是一个开始...

temp