3

enter image description here

I want to expand label to fill window

For example we use in tk button

parent.columnconfigure(0, weight=1)

button.grid(sticky='ew')

Can we do something like this to expand both label name to capture all available screen

And my second question : How to change background color or all available settings for that label or button tab

Thanks in advance

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.geometry('600x400+0+0')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

tabs = ttk.Notebook()
tabs.grid(row=0, column=0, sticky='nsew')

tab1 = tk.Frame(tabs, bg='red')
tab2 = tk.Frame(tabs, bg='green')

tabs.add(tab1, text='First Tab')
tabs.add(tab2, text='Second Tab')

root.mainloop()

Manish Pushpam
  • 146
  • 2
  • 16
  • You say you want to expand a label, but the code you posted doesn't have a `Label` widget in it. Are you asking how to expand the notebook tab? – Bryan Oakley Sep 19 '20 at 15:57
  • Sir @Bryan-Oakley I am referring label as a tab name. that's why I wrote label or tab button. – Manish Pushpam Sep 19 '20 at 16:03
  • Do you want to change the width of only the last tab, or of all tabs? There are already several questions on this site about changing the size of the tabs, have you done any research? For example, does this answer your question? https://stackoverflow.com/q/20918625/7432 – Bryan Oakley Sep 19 '20 at 16:46
  • Sir @Bryan-Oakley I want to change width for all available tab. Sir I have done some research. I don't know old python coding. So I am not able to understand that code. Sir can you please write one answer in 3.x python version. – Manish Pushpam Sep 19 '20 at 16:59
  • I think the language of duplicate post given is not python at all xp, ive voted to reopen the post – Delrius Euphoria Sep 19 '20 at 22:32
  • @BryanOakley The question is same maybe, but how can the OP relate to a Q that is not even python, while the OP asked a Q in python – Delrius Euphoria Sep 20 '20 at 14:32
  • @ManishPushpam What do you mean by change all available settings for the tab? This is controlled by a style so you cannot change tabs individually, you can change, for instance the background color for all of them, and set a specific one for the selected tab. – j_4321 Sep 22 '20 at 15:07

2 Answers2

6

First question

I used a style to configure the width of the tabs:

style = ttk.Style(root)
style.configure('TNotebook.Tab', width=1000)

Because I set a very large width, the window is too small to display fully all the tabs, so they are shrunk to fit, which gives exactly the desired result. To ensure that the tab width is large enough, regardless of the screen used, one can use .winfo_screenwidth().

Full example:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.geometry('600x400+0+0')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

style = ttk.Style(root)
style.configure('TNotebook.Tab', width=root.winfo_screenwidth())

tabs = ttk.Notebook()
tabs.grid(row=0, column=0, sticky='nsew')

tab1 = tk.Frame(tabs, bg='red')
tab2 = tk.Frame(tabs, bg='green')
tab3 = tk.Frame(tabs, bg='blue')

tabs.add(tab1, text='First Tab')
tabs.add(tab2, text='Second Tab')
tabs.add(tab3, text='Third Tab')

root.mainloop()

screenshot

Second question

I am not exactly sure whether this is what was asked, but the settings of the tabs can be changed using a style. For instance, to set the background color:

style.configure('TNotebook.Tab', background='green')

The above code set to green the background of all unselected tabs. The background of the selected tab can be set with

style.map('TNotebook.Tab', background=[('selected', 'yellow')])

However it is not possible to change the background color of the tabs individually. To do that the only option is to code your own notebook widget using buttons or labels as tabs.

j_4321
  • 15,431
  • 3
  • 34
  • 61
0

i don't find any options in ttk notebook to increase the tab space, but we can increase its size by a simple trick, if you lengthen the tab name its space also increase , so we can apply the same method to increase its width,

tabs.add(tab1, text=f'{"  ":<{10}}First Tab{"  ":<{10}}')
tabs.add(tab2, text=f'{"  ":<{10}}Second Tab{"  ":<{10}}')

in above code i just put only 10 spaces , increase the number as per your need

Arun K
  • 413
  • 4
  • 15
  • Hello @Arun-K this can't help because no. of tab is random. So I can't fix width size. – Manish Pushpam Sep 19 '20 at 15:47
  • no, you can do , suppose you can declare x=10, then tabs.add(tab1, text=f'{" ":<{x}}First Tab{" ":<{x}}'), then change the value of x as per your need – Arun K Sep 19 '20 at 15:57
  • Thanks for your effort @Arun-K. But this is not the pro way. I will use this if I don't get any perfect answer. – Manish Pushpam Sep 19 '20 at 16:07