设置 Tkinter/ttk 框架背景颜色 [英] Setting Tkinter/ttk Frame background color

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

问题描述

我正在尝试更改 ttk 框架的背景颜色,并且我已经查找了其他示例,但似乎都没有奏效.到目前为止,这是我的代码:

I'm trying to change the background color of a ttk frame and I've looked up other examples, but none have seemed to work. This is my code so far:

from Tkinter import *
import ttk

p = Tk()

p.geometry('600x350')
p.configure(bg='#334353')

gui_style = ttk.Style()
gui_style.configure('My.TButton', foreground='#334353')
gui_style.configure('My.TFrame', background='#334353')

frame = ttk.Frame(p, style='My.TFrame')
frame.grid(column=1, row=1)

ttk.Button(frame, text='test', style='My.TButton').grid(column=0, row=0)
ttk.Button(frame, text='Test 2', style='My.TButton').grid(column=3, row=3)

p.mainloop()

窗口具有我想要的背景颜色,但框架仍然具有默认的灰色背景.我需要添加不同的东西吗?我希望除了按钮之外的整个窗口都是颜色 #334353.我该怎么做?

The window has the background color that I want, but the frame still has the default gray background. Is there something i need to add differently? I want the entire window except for the buttons to be the color #334353. How do I do this?

我附上了我的窗户的样子.我不要灰色:/(注意.我显然没有足够的代表来发布图片,所以这里有一个指向我当前窗口的 imgur 的链接:http://imgur.com/KyhbdMB

I've attached what my window looks like. I don't want the gray. :/ (Note. I don't have enough rep to post images apparently, so here is a link to imgur with my current window: http://imgur.com/KyhbdMB

推荐答案

您的框架仅调整为容纳两个子窗口(按钮)所需的最小尺寸.似乎您希望框架填充主窗口.当您对框架进行网格化时,您应该添加粘性选项以使其扩展以填充可用空间(例如:frame.grid(column=1,row=1,sticky='news')).然后,您需要让父级将所有空间空间分配给此网格单元格.为此,您希望对父窗口使用 grid_rowconfigure 和 grid_columnconfigure 方法.在这种情况下:

Your frame is only sized to the minimum size required to hold the two child windows (the buttons). It seems like you want the frame to fill the main window. When you grid the frame you should add the sticky option to have it expand to fill the available space (eg: frame.grid(column=1,row=1,sticky='news')). Then you need to have the parent allocate all the space space to this grid cell. For that you want to use the grid_rowconfigure and grid_columnconfigure methods for the parent window. In this case:

p.grid_columnconfigure(1, weight=1)
p.grid_rowconfigure(1, weight=1)

它告诉主框架网格几何管理器应该为单元格和第 1 行第 1 列分配空闲空间.这将导致您的框架扩展以填充窗口.

which tells the main frame grid geometry manager that spare space should be given to the cell and row 1 column 1. This will lead to your frame expanding to fill the window.

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

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