简体   繁体   English

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

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

I just wanted to know how you could change the background color of window for a specific portion only , instead of changing the whole window's color.我只是想知道如何仅针对特定部分更改 window 的背景颜色,而不是更改整个窗口的颜色。

Like This (MS Paint)像这样(MS Paint)

Create a background image by using this:使用以下方法创建背景图像:

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

Find an image online that suits your needs and your ready to go;在线查找适合您需求并准备好 go 的图像; another option is making an image and saving it locally and using that.另一种选择是制作图像并将其保存在本地并使用它。

Get back to me if it didn't work or you need anymore help.如果它不起作用或者您需要更多帮助,请回复我。 -Jack -杰克

Without knowing what else your window has in it, the simplest solution is to create two frames, one for each section.在不知道 window 中还有什么的情况下,最简单的解决方案是创建两个框架,每个部分一个。 You can then use place to place the two widgets in the window without affecting the layout of any other widgets.然后,您可以使用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