【问题标题】:How to add background image to my application in Tkinter?如何在 Tkinter 中为我的应用程序添加背景图像?
【发布时间】:2017-06-24 15:36:23
【问题描述】:

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

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

【问题讨论】:

    标签: python-3.x tkinter tk


    【解决方案1】:

    叠加元素很棘手。我认为这可能是您正在寻找的大致内容。至少这是一个开始……

    import PIL
    from PIL import Image, ImageTk
    from tkinter import *
    from tkinter import filedialog
    from tkinter import messagebox
    
    root = Tk()
    
    
    
    class Window:
    
        def __init__(self, master=None):
            tower = PIL.Image.open("Images/island.png")
            master.update()
            win_width = int(master.winfo_width())
            win_height = int(master.winfo_height())
            # Resize the image to the constraints of the root window.
            tower = tower.resize((win_width, win_height))
            tower_tk = ImageTk.PhotoImage(tower)
            # Create a label to hold the background image.
            canvas = Canvas(master, width=win_width, height=win_height)
            canvas.place(x=0, y=0, anchor='nw')
            canvas.create_image(0, 0, image=tower_tk, anchor='nw')
            canvas.image = tower_tk
            frame = Frame(master)
            frame.place(x=win_width, y=win_height, anchor='se')
            master.update()
            w = Label(master, text="Send and receive files easily", anchor='w')
            w.config(font=('times', 32))
            w.place(x=0, y=0, anchor='nw')
    
            master.title("Bifrost v1.0")
            self.img1 = PhotoImage(file="Images/logo.png")
            self.img2 = PhotoImage(file="Images/magnifier.png")
    
            frame.grid_columnconfigure(0, weight=1)
            sendButton = Button(frame, image=self.img2)
            sendButton.grid(row=0, column=1)
            sendButton.image = self.img2
            receiveButton = Button(frame, image=self.img1)
            receiveButton.grid(row=0, column=2)
            receiveButton.image = self.img1
    
            menu = Menu(master)
            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):
            message = "This is a project developed by Aditi,Sagar and"
            message += "Suyash as the final year project."
            messagebox.showinfo("Delete Theme", message)
    
    root.resizable(0,0)
    
    #size of the window
    root.geometry("700x400")
    app = Window(root)
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      相关资源
      最近更新 更多