背景图片不正常

时间:2014-12-17 16:44:07

标签: image tkinter label

我的目标:拥有占据显示器整个窗口的背景图像。然后在它上面放置图像按钮,这个按钮将很好地居中在这个窗口上(按钮排列在网格中)。 图像是1920X1080。 我有其他代码片段,它们遵循完全相同的序列,图像显示在背景中。所以完全不知所措。但是想要正确理解这段代码的逻辑/而不是依靠运气!注意在其他工作代码上,我同时做了background_label的一个地方和网格!使用此代码,按钮根本不会出现。 当我使用background_label(内容....)时,图像确实出现在背景中,前面有按钮,但在这种情况下整个显示窗口没有被占用。图像只是扩展以填充按钮图像占用的区域。

root = Tk()
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))

content = ttk.Frame(root, padding=(3,3,12,12))
print(root.winfo_screenwidth(), root.winfo_screenheight())
#prints as 1920 & 1080

#old background_image_file='waves1600x926.gif'

background_image_file='Lake.gif' #this is 1080X1920
background_image=PhotoImage(file=background_image_file)
background_label =ttk.Label(root,image=background_image) #tried content instead of root also
background_label.place(x=0, y=0,relwidth=1, relheight=1)
background_label.grid()

logos=['abc','cbs','nbc','fox','cnbc','amc','bet']
logobuttons=defaultdict(str)
logoimgs=defaultdict(str)
for logo in logos:

    logoimgs[logo]=PhotoImage(file=LOGODIR+logo+'.gif')

    logobuttons[logo]=ttk.Button(content,image=logoimgs[logo])


content.grid(column=0, row=0, sticky=(N,S,E,W))

col=0
row=0
maxcols=5
for logo in logos:
    logobuttons[logo].grid(row=row,column=col,pady=5,padx=5)
    if col == maxcols:
        col=0
        row=row+1
    else:
        col=col+1

root.mainloop()

1 个答案:

答案 0 :(得分:0)

当您同时使用placegrid时,只有您为给定窗口小部件调用的最后一个窗口才会生效。因此,当你致电background_label.grid()时,它完全否定了background_label.place(...)的影响。换句话说,您需要删除此小部件的grid调用。

如果您需要背景图像,最佳解决方案是使用相对宽度和高度为1的地方。您还应该首先创建包含图像的窗口小部件,因此它在堆叠顺序中最低。但是,你可以随时降低它。