设置tkinter ttk框架的背景颜色 [英] Setting background color of a tkinter ttk frame

查看:1291
本文介绍了设置tkinter ttk框架的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在tkinter窗口中为框架设置背景颜色.窗口的背景色已成功设置,但内部框架的背景色未成功设置.仅仅输入bg ='color_name或color_code'是行不通的.

I want to simply set a background color to the frame within the tkinter window. The background color for the window was successful set, but the not for the frame inside. Simply entering bg='color_name or color_code' is not working.

如何设置ttk框架的背景颜色?

How can one set the background color of a ttk frame?

除此之外,是否有一种方法还可以更改其他小部件选项,即选项卡栏的颜色,字体?

In addition to this, is there a method to also change other widget options i.e. the color, fonts of the tabs bar?

我尝试使用命令bg ='Color_name或color_code',还尝试使用frame_name = config(bg ='color_name或color_code').两者都不起作用.

I have tried using command bg='Color_name or color_code' and also tried using frame_name=config(bg='color_name or color_code'). Neither worked.

from tkinter import * 
from tkinter import ttk 


window = Tk() 
window.title("Title Name") 
window.config(bg='#FFFAFA')
window.geometry('1024x800')


menubar = Menu(window) 
filemenu = Menu(menubar,tearoff=0)
menubar.add_cascade(label='File',menu=filemenu)
filemenu.add_command(label='Open')
filemenu.add_command(label='Clear')
filemenu.add_command(label='Save As')
filemenu.add_separator()
filemenu.add_command(label='Exit')


helpmenu = Menu(menubar,tearoff=0)
menubar.add_cascade(label='Help',menu=helpmenu)
helpmenu.add_command(label='Precuations')
helpmenu.add_command(label='Version Info')
helpmenu.add_command(label='Technical Support')

window.config(menu=menubar)

rows = 0
while rows<50:
    window.rowconfigure(rows,weight=1)
    window.columnconfigure(rows, weight=1)
    rows +=1

#creation of frame
mainframe = ttk.Notebook(window,width=50)
mainframe.grid(row=1,column=2,columnspan=45,rowspan=43,sticky='NESW')

#create tabs within the frame
tab1 = ttk.Frame(mainframe)
mainframe.add(tab1, text="Tab1")


tab2 = ttk.Frame(mainframe)
mainframe.add(tab2, text="Tab2")


tab3 = ttk.Frame(mainframe)
mainframe.add(tab3, text="Tab3")

tab4 = ttk.Frame(mainframe)
mainframe.add(tab4, text="Tab4")

tab5 = ttk.Frame(mainframe)
mainframe.add(tab5, text="Tab4")

window.mainloop()

推荐答案

使用ttk小部件时,所有样式均应使用ttk.Style完成.

When using ttk widgets, all styling should be done using ttk.Style.

您需要使用s = ttk.Style()初始化样式类,然后可以使用s.configure('StyleName', option='value')

You need to initialize the style class with s = ttk.Style(), and can then change attributes of the different widget styles with s.configure('StyleName', option='value')

您可以找到默认样式名称

You can find the default style names here. So for a Frame the style name is TFrame. When you configure an option for this style, it will be used by all frames. When you want to configure an option for a single frame, you can create a new style based on the original style by using a name of the form newName.oldName. In your case, this could be Frame1.TFrame. You can then tell a frame to use this style by passing style='Frame1.TFrame'.

如果在代码中使用以下内容,则会看到第一帧为红色,第二帧为蓝色,所有其他帧为绿色:

If you use the following in your code, you will see that the first frame is red, the second is blue and all other frames are green:

# Initialize style
s = ttk.Style()
# Create style used by default for all Frames
s.configure('TFrame', background='green')

# Create style for the first frame
s.configure('Frame1.TFrame', background='red')
# Use created style in this frame
tab1 = ttk.Frame(mainframe, style='Frame1.TFrame')
mainframe.add(tab1, text="Tab1")

# Create separate style for the second frame
s.configure('Frame2.TFrame', background='blue')
# Use created style in this frame
tab2 = ttk.Frame(mainframe, style='Frame2.TFrame')
mainframe.add(tab2, text="Tab2")

这篇关于设置tkinter ttk框架的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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