2016-12-12 115 views
0

我已經能夠改變整個窗口,並通過該特定標籤的背景色,Python Tkinter,更改特定的行背景顏色?

master.configure(background = 'SteelBlue1') 
titlelabel = Tkinter.Label(master, text="my Title", fg = "blue4", bg = "gray80").grid(row=0, column = 1) 

有一個簡單的方法,使行灰度0,而不僅僅是標籤周圍的區域?由於

+1

我不是100%確定(因此評論不回答),但我認爲你需要將你的標籤包裝在一個框架(設置爲填充整行),並改變顏色那。 – scotty3785

回答

1

如果只有該行標題,您可以使用columspan,這樣對所有的列標題跨度和擴大水平標籤:

import Tkinter 

master = Tkinter.Tk() 

master.configure(background='SteelBlue1') 
master.columnconfigure(0, weight=1) # make the column 1 expand when the window is resized 
nb_of_columns = 2 # to be replaced by the relevant number 
titlelabel = Tkinter.Label(master, text="my Title", fg="blue4", bg ="gray80") 
titlelabel.grid(row=0, column=0, sticky='ew', columnspan=nb_of_columns) # sticky='ew' expands the label horizontally 

master.geometry('200x200') 
master.mainloop() 

否則,解決的辦法是遵循scotty3785意見和使用一個框架:

import Tkinter 

master = Tkinter.Tk() 

master.configure(background='SteelBlue1') 
master.columnconfigure(1, weight=1) 

nb_of_columns = 2 # to be replaced by the relevant number 
titleframe = Tkinter.Frame(master, bg ="gray80") 
titleframe.grid(row=0, column=0, columnspan=nb_of_columns, sticky='ew') 
titlelabel = Tkinter.Label(titleframe, text="my Title", fg="blue4", bg ="gray80") 
titlelabel.grid(row=0, column=1) 
# other widgets on the same row: 
Tkinter.Button(titleframe, text='Ok').grid(row=0, column=2) 

master.geometry('200x200') 
master.mainloop()