扩展背景图像以适合窗口并在tkinter中将框架堆叠在其上

时间:2019-07-25 08:37:08

标签: python tkinter

我正在尝试使用tkinter将背景图像添加到我的GUI应用程序中。在根窗口中,我有两个框架,分别名为container_1和container_2。当我添加背景图像时,这些带有边框的图像被推入图像的外部。我可以看到它们,因为窗口大小大于图像大小。如果更少,它甚至是不可见的。如何将它们放置在背景图像上?是否可以为container_1和container_2提供不同的背景图像?
请回答。

(评论链接中的imgae)

import tkinter as tk

def FnToShow():
    container_2.pack(side="right",expand=True, fill="x", padx=1, pady=1)

def FnToHide():
    container_2.pack_forget()

root = tk.Tk()
root.geometry('800x600')
#bg image
background_image=tk.PhotoImage(file= "bgPic.png")
background_label = tk.Label(root, image=background_image)
background_label.pack() # is pack problematic here?

container_1 = tk.Frame(root, borderwidth=2, relief="solid")
container_2 = tk.Frame(root, borderwidth=2, relief="solid")

settingBtn  = tk.Button(container_1, text="Settings", command= FnToShow)
settingBtn.grid(row=6, column=4)

setting_1 = tk.Label(container_2, text="Setting-1", fg='#000000')
setting_1.grid(row=3, column=10)
setting_2 = tk.Label(container_2, text="Setting-2", fg='#000000')
setting_2.grid(row=4, column=10)
closeSettingBtn  = tk.Button(container_2, text="close Settings", command= FnToHide)
closeSettingBtn.grid(row=5, column=10)

container_1.pack(side="left", expand=True, fill="x", padx=1, pady=1)
root.mainloop()

1 个答案:

答案 0 :(得分:1)

#setting background image
background_main_img=tk.PhotoImage(file= "bgMain.png")
background_main = tk.Label(root, image=background_main_img)
background_main.place(relx=.5, rely=.5, anchor="center")

#setting background image for container frame
background_con_1=tk.PhotoImage(file= "bgContainer.png")
background_label = tk.Label(container_1, image=background_con_1)
background_label.place(relx=.5, rely=.5, anchor="center")