不能在Tkinter的背景色 [英] can't background color in tkinter

查看:190
本文介绍了不能在Tkinter的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个简单的tkinter程序感到绝望:我似乎无法更改背景颜色(或单个小部件的颜色)!这里出了什么问题?

I'm growing desperate with this simple tkinter program: I can't seem to be able to change the background color (or the color of the individual widgets)! What is going wrong here?

下面是我进行的各种尝试以及收到的错误的要点

Below is a gist of the various attempts that I and the errors that I received

import tkinter
import tkinter.ttk as tk

root = tkinter.Tk()

frame= tk.Frame(root)
frame.grid(column=0, row=0)

tk.Button(frame, text="Open file", command=None).grid(column=0, row=1 )
lab=tk.Label(frame, text="test test test test test test ").grid(column=0, row=2 )

#root.config(background="black")    # does nothing
#frame.config(background="black")   # Error: unknown option "-background"
#lab.config(background="black")     # Error: 'NoneType' object has no attribute 'config'

root.mainloop()


推荐答案


  • frame.config(background = black)返回错误未知选项 -background ,因为这是 ttk.Frame ,而不是 tkinter.Frame 和背景使用 ttk.Style 更改 ttk.Frame (请参见下面的示例)。

    • frame.config(background="black") returns the error unknown option "-background" because this is a ttk.Frame, not a tkinter.Frame and the background of a ttk.Frame is changed using a ttk.Style (see example below).

      lab.config(background = black)给出错误'NoneType'对象没有属性'config',因为您做了 lab = tk.Label(...)。grid(...)网格返回,因此 lab ,而不是 Label

      lab.config(background="black") gives the error 'NoneType' object has no attribute 'config' because you did lab = tk.Label(...).grid(...) and grid returns None so lab is None, not a Label.

      root.config(背景= black)不执行任何操作,因为窗口小部件填充了窗口,因此您无法像naknak12所述看到背景。

      root.config(background="black") does nothing because the widgets fill the window so you cannot see the background like naknak12 explained.

      以下是使用 ttk 小部件的示例:

      Here is an example using ttk widgets:

      import tkinter
      from tkinter import ttk
      
      root = tkinter.Tk()
      root.configure(background='black')
      # style configuration
      style = ttk.Style(root)
      style.configure('TLabel', background='black', foreground='white')
      style.configure('TFrame', background='black')
      
      frame = ttk.Frame(root)
      frame.grid(column=0, row=0)
      
      ttk.Button(frame, text="Open file", command=None).grid(column=0, row=1)
      lab = ttk.Label(frame, text="test test test test test test ")
      lab.grid(column=0, row=2)
      
      
      root.mainloop()
      

      没有<$ c的示例$ c> ttk :

      import tkinter
      
      root = tkinter.Tk()
      
      frame = tkinter.Frame(root)
      frame.grid(column=0, row=0)
      
      tkinter.Button(frame, text="Open file", command=None).grid(column=0, row=1 )
      lab = tkinter.Label(frame, text="test test test test test test ")
      lab.grid(column=0, row=2)
      
      root.configure(background='black')
      lab.configure(background='black', foreground='white')
      frame.configure(background='black')
      
      root.mainloop()
      

      这篇关于不能在Tkinter的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆