窗口小部件覆盖其框架的背景

时间:2015-08-10 05:50:29

标签: python background tkinter widget

当我创建一个框架并为其设置背景颜色时,颜色会很好地显示出来。但是,当我向框架添加一个小部件时,背景颜色会消失。我尝试将相同的颜色设置为小部件的背景,但框架中仍然有许多白点。

以下是无效的代码块:

    DAC_frame = Frame(self, width=180, height=240, bg="red")
    DAC_frame.pack(side=RIGHT)

    DAC_delete_label = Label(DAC_frame, text="Delete A Term:", bg="red")
    DAC_delete_label.pack(fill=X)

    DAC_delete_entry = Entry(DAC_frame)
    DAC_delete_entry.pack()

    DAC_delete = Button(DAC_frame, text="Delete Term")
    DAC_delete.pack()

    DAC_clear = Button(DAC_frame, text="Clear Dictionary")
    DAC_clear.pack()

1 个答案:

答案 0 :(得分:0)

您无法在Tkinter中设置透明色,这就是Frame背景消失的原因。所以"白点"只是你的小部件的背景。您现在有两个选择:

  1. 使用Canvas here is a example
  2. 创建自己的小部件
  3. 为每个小部件设置相同的backgroundcolor,对于可以将borderwidth设置为0的按钮。

    from Tkconstants import RIGHT, X
    from Tkinter import Frame, Tk, Label, Entry, Button
    import Tkinter
    
    
    class App(Tk):
    
        def __init__(self):
            Tkinter.Tk.__init__(self)
            DAC_frame = Frame(self, width=180, height=240, bg="red")
            DAC_frame.pack(side=RIGHT)
    
            DAC_delete_label = Label(DAC_frame, text="Delete A Term:", bg="red")
            DAC_delete_label.pack(fill=X)
    
            DAC_delete_entry = Entry(DAC_frame)
            DAC_delete_entry.pack()
    
            DAC_delete = Button(DAC_frame, text="Delete Term",borderwidth=0, bg="red", activebackground="red")
            DAC_delete.pack()
    
            DAC_clear = Button(DAC_frame, text="Clear Dictionary",borderwidth=0, bg="red", activebackground="red")
            DAC_clear.pack()
    
    ap = App()
    ap.mainloop()