Python tkinter根bg颜色

Python tkinter根bg颜色,python,tkinter,Python,Tkinter,我正在使用tkinter创建时间表。我在代码中将根bg颜色设置为白色,但没有变为白色。(我还将一些标签设置为白色,以查看它是否变为白色。) 但是,当我在一个新文件中测试它时,它可以工作 这是我的日程安排代码 root=Tk() root['bg']='white' cp=coursePlan(root) cp.pack() root.title('Schedule') root.mainloop() 这是我的测试代码 root=Tk() root['bg']='white' l=Label(r

我正在使用tkinter创建时间表。我在代码中将根bg颜色设置为白色,但没有变为白色。(我还将一些标签设置为白色,以查看它是否变为白色。)

但是,当我在一个新文件中测试它时,它可以工作

这是我的日程安排代码

root=Tk()
root['bg']='white'
cp=coursePlan(root)
cp.pack()
root.title('Schedule')
root.mainloop()
这是我的测试代码

root=Tk()
root['bg']='white'
l=Label(root,text='test')
l.grid(row=0,column=0)
root.mainloop()


当您将
父项
分配给
标签(根…)
之类的小部件时,在显示小部件时,默认情况下,小部件在其父项上占据一些空间

小部件有自己的背景色,不一定是透明的,因此,如果小部件的
bg
设置为
“红色”
,而其父部件的
bg
设置为
“白色”
,则基本上有三种结果:

  • 小部件显示时选择了填充选项(
    pack(fill=…
    grid(sticky=…)
    ),或者小部件在x和y维度上超过了其父部件,这导致父部件完全位于小部件后面,因此只显示小部件的背景,
    “红色”
  • 小部件的显示部分或完全不考虑其可调整性,因此部分占用了其父部件,在这种情况下,小部件及其父部件的背景显示为“红色”和“白色”
  • 最后,小部件根本不显示,因此只显示父级的bg,
    “白色”
  • 请参阅下面的演示:

    import tkinter as tk
    
    
    def demo(*args):
        if choice.get() == 1:
            widget.pack(fill='both', expand=True)
        elif choice.get() == 2:
            widget.pack(fill='x', expand=False)
        elif choice.get() == 3:
            widget.pack_forget()
    
    def default(event):
        root.state('zoomed')
        choice.set(2)
        widget.pack(fill='x', expand=False)
    
    root = tk.Tk()
    
    root['bg'] = 'white'
    root.state('zoomed')
    
    widget = tk.LabelFrame(root, text="Displaying Options", bg='red')
    
    choice = tk.IntVar(value=2)
    fill = tk.Radiobutton(widget, variable=choice, text="Fill / Overfit", value=1)
    no_resize = tk.Radiobutton(widget, variable=choice,
                                text="Some / No regard to resizability", value=2)
    no_display = tk.Radiobutton(widget, variable=choice, text="Not displayed",
                                                                        value=3)
    choice.trace_add('write', demo)     # to call demo when choice is modified
    root.bind_all("<Escape>", default)  # to go back to initial state when user hits esc
    
    widget.pack(fill='x', expand=False)
    fill.pack(side='left')
    no_resize.pack(side='left')
    no_display.pack(side='left')
    root.mainloop()
    
    将tkinter作为tk导入
    def演示(*args):
    如果choice.get()==1:
    pack(fill='both',expand=True)
    elif choice.get()==2:
    widget.pack(fill='x',expand=False)
    elif choice.get()==3:
    widget.pack_忘记()
    def默认值(事件):
    root.state('缩放')
    选择集(2)
    widget.pack(fill='x',expand=False)
    root=tk.tk()
    根['bg']='white'
    root.state('缩放')
    widget=tk.LabelFrame(root,text=“显示选项”,bg='red')
    choice=tk.IntVar(值=2)
    fill=tk.Radiobutton(小部件,变量=choice,text=“fill/Overfit”,值=1)
    no_resize=tk.Radiobutton(小部件,变量=choice,
    text=“部分/不考虑大小调整”,值=2)
    no_display=tk.Radiobutton(小部件,变量=choice,text=“未显示”,
    数值=3)
    选项。trace_添加('write',demo)#在修改选项时调用demo
    root.bind_all(“,默认值)#在用户点击esc时返回初始状态
    widget.pack(fill='x',expand=False)
    填充。包装(侧面为左)
    没有大小调整包(侧边为左)
    无显示。包装(侧面为左)
    root.mainloop()
    
    什么是
    coursePlan
    ?如果这是一个帧,那么您需要将其背景也设置为白色:
    cp=coursePlan(root,bg='white')
    。Tkinter对象中未使用的空间是不透明的。