Python 中 Tk 的背景颜色

发布于 2024-08-30 17:28:07 字数 549 浏览 3 评论 0原文

我正在用 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)

I'm writing a slideshow program with Tkinter, but I don't know how to change the background color to black instead of the standard light gray. How can this be done?

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

离去的眼神 2024-09-06 17:28:07
root.configure(background='black')

或者更一般地说

<widget>.configure(background='black')
root.configure(background='black')

or more generally

<widget>.configure(background='black')
ぃ双果 2024-09-06 17:28:07

我知道这是一个老问题,但是:

root["bg"] = "black"

也会做你想做的事情,而且需要更少的打字。

I know this is kinda an old question but:

root["bg"] = "black"

will also do what you want and it involves less typing.

給妳壹絲溫柔 2024-09-06 17:28:07
widget['bg'] = '#000000'

或者

widget['background'] = '#000000'

也可以工作,因为十六进制值的颜色也被接受。

widget['bg'] = '#000000'

or

widget['background'] = '#000000'

would also work as hex-valued colors are also accepted.

静若繁花 2024-09-06 17:28:07

config 是另一种选择:

widget1.config(bg='black')
widget2.config(bg='#000000')

或:

widget1.config(background='black')
widget2.config(background='#000000')

config is another option:

widget1.config(bg='black')
widget2.config(bg='#000000')

or:

widget1.config(background='black')
widget2.config(background='#000000')
半山落雨半山空 2024-09-06 17:28:07

已经更新了,

root.configure(background="red")

现在是:

root.configure(bg="red")

It's been updated so

root.configure(background="red")

is now:

root.configure(bg="red")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文