繁体   English   中英

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

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

我只是想知道如何仅针对特定部分更改 window 的背景颜色,而不是更改整个窗口的颜色。

像这样(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

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

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

在不知道 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()

截屏

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM