简体   繁体   English

设置Tkinter / ttk框架背景色

[英]Setting Tkinter/ttk Frame background color

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. 我正在尝试更改ttk框架的背景颜色,并且查找了其他示例,但似乎都没有用。 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()

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. 我希望除按钮外的整个窗口都是颜色#334353。 How do I do this? 我该怎么做呢?

EDIT: 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 :/(请注意。我显然没有足够的代表来张贴图像,因此,这是当前窗口的imgur链接: http ://imgur.com/KyhbdMB

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') ). 对框架进行网格化时,应添加粘滞选项以使其展开以填充可用空间(例如: 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. 为此,您想对父窗口使用grid_rowconfigure和grid_columnconfigure方法。 In this case: 在这种情况下:

p.grid_columnconfigure(1, weight=1)
p.grid_rowconfigure(1, weight=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. 它告诉主框架网格几何管理器应为单元格和第1行的第1列留出备用空间。这将导致框架扩展以填充窗口。

It works on my PC! 它可以在我的PC上运行!

Try this: 尝试这个:

  • Update your Python environment(Tested under Py 3.4 Windows 32bit) 更新您的Python环境(在Py 3.4 Windows 32bit下测试)
  • Install the lastest TTK package 安装最新的TTK软件包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM