如何更改 window (Python) 特定部分的背景颜色 (Tkinter)

How to change background color for a specific portion of window (Python) (Tkinter)

我只是想知道如何仅更改 window 特定部分的背景颜色,而不是更改整个 window'颜色.

Like This (MS Paint)

使用此方法创建背景图片:

from tkinter import *
from tkinter import messagebox
top = Tk()

C = Canvas(top, bg="blue", height=250, width=300)
filename = PhotoImage(file = "C:\Users\location\imageName.png")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

C.pack()
top.mainloop

在线查找适合您需要并准备好出发的图片;另一种选择是制作图像并将其保存在本地并使用它。

如果它不起作用或您需要更多帮助,请回复我。 -杰克

不知道您的 window 中还有什么,最简单的解决方案是创建两个框架,每个部分一个。然后,您可以使用 place 将两个小部件放置在 window 中,而不影响任何其他小部件的布局。

import tkinter as tk

root = tk.Tk()
red_frame = tk.Frame(bd=0, highlightthickness=0, background="red")
blue_frame = tk.Frame(bd=0, highlightthickness=0, background="blue")
red_frame.place(x=0, y=0, relwidth=1.0, relheight=.5, anchor="nw")
blue_frame.place(x=0, rely=.5, relwidth=1.0, relheight=.5, anchor="nw")

root.mainloop()