【问题标题】:How to change background color for a specific portion of window (Python) (Tkinter)如何更改窗口特定部分的背景颜色(Python)(Tkinter)
【发布时间】:2021-05-08 15:00:17
【问题描述】:

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

Like This (MS Paint)

【问题讨论】:

  • 您是否将有一些小部件同时在两者之上(即:跨越两种颜色之间的线)?
  • 我已经使用 Grid Widgets 对其进行了编码...
  • 这不能回答我的问题
  • 是的,我的窗口上确实有一些小部件...

标签: python tkinter colors window


【解决方案1】:

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

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()

【讨论】:

  • 我的意思是,它可以满足我的要求,但现在标签小部件位于顶部(标签不与背景颜色合并)并且它们周围有自己的颜色...
【解决方案2】:

使用以下方法创建背景图像:

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

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

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

【讨论】:

  • 我还使用网格 (.grid) 编写了一个完整的程序
  • 酷,这听起来像是一个更好的解决方案
猜你喜欢
  • 1970-01-01
  • 2018-10-01
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 2013-03-21
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
相关资源
最近更新 更多