Python Tkinter,改变特定的行背景颜色?

时间:2016-12-12 10:30:57

标签: python tkinter tkinter-canvas

我已经能够通过这个改变整个窗口和特定标签的背景颜色,

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

是否有一种简单的方法可以使第0行变为灰色而不仅仅是标签周围的区域?感谢

1 个答案:

答案 0 :(得分: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()