不能在Tkinter的背景色 [英] can't background color in tkinter
问题描述
我对这个简单的tkinter程序感到绝望:我似乎无法更改背景颜色(或单个小部件的颜色)!这里出了什么问题?
I'm growing desperate with this simple tkinter program: I can't seem to be able to change the background color (or the color of the individual widgets)! What is going wrong here?
下面是我进行的各种尝试以及收到的错误的要点
Below is a gist of the various attempts that I and the errors that I received
import tkinter
import tkinter.ttk as tk
root = tkinter.Tk()
frame= tk.Frame(root)
frame.grid(column=0, row=0)
tk.Button(frame, text="Open file", command=None).grid(column=0, row=1 )
lab=tk.Label(frame, text="test test test test test test ").grid(column=0, row=2 )
#root.config(background="black") # does nothing
#frame.config(background="black") # Error: unknown option "-background"
#lab.config(background="black") # Error: 'NoneType' object has no attribute 'config'
root.mainloop()
推荐答案
-
frame.config(background = black)
返回错误未知选项 -background
,因为这是ttk.Frame
,而不是tkinter.Frame
和背景使用ttk.Style
更改ttk.Frame
(请参见下面的示例)。frame.config(background="black")
returns the errorunknown option "-background"
because this is attk.Frame
, not atkinter.Frame
and the background of attk.Frame
is changed using attk.Style
(see example below).lab.config(background = black)
给出错误'NoneType'对象没有属性'config'
,因为您做了lab = tk.Label(...)。grid(...)
和网格
返回无
,因此lab
为无
,而不是Label
。lab.config(background="black")
gives the error'NoneType' object has no attribute 'config'
because you didlab = tk.Label(...).grid(...)
andgrid
returnsNone
solab
isNone
, not aLabel
.root.config(背景= black)
不执行任何操作,因为窗口小部件填充了窗口,因此您无法像naknak12所述看到背景。root.config(background="black")
does nothing because the widgets fill the window so you cannot see the background like naknak12 explained.以下是使用
ttk
小部件的示例:Here is an example using
ttk
widgets:import tkinter from tkinter import ttk root = tkinter.Tk() root.configure(background='black') # style configuration style = ttk.Style(root) style.configure('TLabel', background='black', foreground='white') style.configure('TFrame', background='black') frame = ttk.Frame(root) frame.grid(column=0, row=0) ttk.Button(frame, text="Open file", command=None).grid(column=0, row=1) lab = ttk.Label(frame, text="test test test test test test ") lab.grid(column=0, row=2) root.mainloop()
没有<$ c的示例$ c> ttk :
import tkinter root = tkinter.Tk() frame = tkinter.Frame(root) frame.grid(column=0, row=0) tkinter.Button(frame, text="Open file", command=None).grid(column=0, row=1 ) lab = tkinter.Label(frame, text="test test test test test test ") lab.grid(column=0, row=2) root.configure(background='black') lab.configure(background='black', foreground='white') frame.configure(background='black') root.mainloop()
这篇关于不能在Tkinter的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!