0

In case of label or button, I know how it's done.

style=ttk.Style()
style.configure('One.TLabel', font=('Arial', 32))
style.configure('Two.TLabel', font=('Arial', 18))

h1=Label(root, text='Heading', style='One.TLabel')
h2=Label(root, text='Sub-heading', style='Two.TLabel')

h1.pack()
h2.pack()

This works.

But in case of a notebook tab, say I wanna change tab padding and font. Same this method doesn't work.

style=ttk.Style()
style.configure('One.TNotebook.Tab', font=('Arial', 14), padding=20)
style.configure('Two.TNotebook.Tab', font=('Arial', 12), padding=10)

# adding book1 tabs
book1=Notebook(root, style='One.TNotebook')
# adding book2 tabs
book2=Notebook(root, style='Two.TNotebook')

book1.pack()
book2.pack()

How do we do it? Is there a work-around?

  • Your code works for me when I add all of the missing pieces. I get two notebooks with different tab styles. Please [edit] your question to include a complete [mcve]. – Bryan Oakley May 29 '20 at 14:13

1 Answers1

2

You can use ttk.Style(), for the notebooks as follows, to define two different styles for two different notebooks that cohabit in the same window:

enter image description here

import tkinter as tk
from tkinter import ttk


root = tk.Tk()


style = ttk.Style()
style.configure('One.TNotebook.Tab', font=('Arial', 24), padding=40)

book1 = ttk.Notebook(root, style='One.TNotebook')
book1.pack()
frame1 = ttk.Frame(book1, width=400, height=200, relief=tk.SUNKEN)
frame2 = ttk.Frame(book1, width =400, height=200, relief=tk.SUNKEN)
book1.add(frame1, text = 'One')
book1.add(frame2, text = 'Two')


style.configure('Two.TNotebook.Tab', font=('Arial', 12), padding=10)

book2 = ttk.Notebook(root, style='Two.TNotebook')
book2.pack()
frame3 = ttk.Frame(book2, width=400, height=200, relief=tk.FLAT)
frame4 = ttk.Frame(book2, width =400, height=200, relief=tk.FLAT)
book2.add(frame3, text='Three')
book2.add(frame4, text='Four')


root.mainloop()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • I made two styles like this: `self.style = TTK.Style() self.style.configure('Window.TNotebook.Tab', font=('Candara', 14, 'bold'), padding=[35, 7]) self.body_notebook = TTK.Notebook(self.frame_body, style='Window.TNotebook') self.style = TTK.Style() self.style.configure('Result.TNotebook.Tab', font=('Candara', 13, 'bold'), padding=[10, 2]) self.result_notebook = TTK.Notebook(self.body_frame, style='Result.TNotebook')` **Style of `body_notebook` changes to what I defined in `Result_TNotebook` but `result_notebook` is as if no style is applied to it.** What do I do? – RisingUnderDog May 29 '20 at 13:17
  • Oh! I'm sorry, I made a typo during the comment editing I think. In the program, I have written `self.style` and `self.style_result`. But it doesn't work. – RisingUnderDog May 29 '20 at 13:35
  • Yes I have read it. I also copied your whole code and ran it on terminal. It worked! I didn't know for sure when I was doing it, but I tried the way you told above before asking the question already. But it doesn't work in my program I dunno what's going wrong :( – RisingUnderDog May 29 '20 at 13:38
  • no worries - I am afraid that maybe I don't quite understand your problem: you asked "Is there a way to keep two notebooks with different tab style in tkinter using python?" - I answered, and gave you an example to illustrate that it is possible, and how to implement it... My suggestion is to maybe stare at it for a little while, and see if the proposed approach solves your specific problem. – Reblochon Masque May 29 '20 at 13:40
  • Yes, I think I don't quite understand what's causing the problem just yet. But anyway now that you have verified this thing, I can be sure that it's not causing the problem. Nonetheless, there was no such question asked on stackoverflow and your post is a correct answer to the question I asked. So thanks for the answer. – RisingUnderDog May 29 '20 at 13:43
  • You do not need to create two style objects. Your example works just as well with a single instance of `ttk.Style()`. (ie: remove `style2` and use `style1` twice. – Bryan Oakley May 29 '20 at 14:08
  • @ReblochonMasque Good news! I figured it out. Couldn't have done it without you eliminating that doubt I had. I was going round and round within it. Good bless you! ^_^ – RisingUnderDog May 29 '20 at 14:27