Вопрос

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

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?

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: https://i.stack.imgur.com/91dsB.jpg

Это было полезно?

Решение

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)

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.

Другие советы

It works on my PC!

Try this:

  • Update your Python environment(Tested under Py 3.4 Windows 32bit)
  • Install the lastest TTK package
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top