Tk的背景颜色在Python中

olo*_*fom 42 python tk-toolkit tkinter

我正在用Tkinter编写幻灯片程序,但我不知道如何将背景颜色更改为黑色而不是标准浅灰色.如何才能做到这一点?

import os, sys
import Tkinter
import Image, ImageTk
import time

root = Tkinter.Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.focus_set()
root.bind("<Escape>", lambda e: e.widget.quit())
image = Image.open(image_path+f)
tkpi = ImageTk.PhotoImage(image)        
label_image = Tkinter.Label(root, image=tkpi)
label_image.place(x=0,y=0,width=w,height=h)
root.mainloop(0)
Run Code Online (Sandbox Code Playgroud)

msw*_*msw 85

root.configure(background='black')
Run Code Online (Sandbox Code Playgroud)

或更一般地说

<widget>.configure(background='black')
Run Code Online (Sandbox Code Playgroud)

  • 对于后代,有效的颜色是系统定义的颜色(例如[很多命名的](http://www.science.smith.edu/dftwiki/index.php/Color_Charts_for_TKinter))或十六进制值,可以是形式`#RGB`、`#RRGGBB`和`#RRRRGGGGBBBB`。 (2认同)

iCo*_*dez 19

我知道这有点老问题但是:

root["bg"] = "black"
Run Code Online (Sandbox Code Playgroud)

也会做你想做的事情,而且打字更少.


Nae*_*Nae 5

config是另一种选择:

widget1.config(bg='black')
widget2.config(bg='#000000')
Run Code Online (Sandbox Code Playgroud)

或者:

widget1.config(background='black')
widget2.config(background='#000000')
Run Code Online (Sandbox Code Playgroud)