I want to set a theme to my tkinter window as well as config my tkinter window buttons, labels with options like bg. Following is my approach,
Imports,
from tkinter import *
import tkinter as tk
from tkinter import ttk
from ttkthemes import ThemedTk
Code w/o setting the theme:
root = Tk()
root.geometry('500x500')
rootlabel1 = Label(root, text="Total Income : ", bg = "red")
rootlabel1.grid(row=1, column = 0)
rootlabel1.configure(anchor="center")
btn1 = Button(root, text = 'Add Income', pady = 20)
btn1.grid(row = 2, column = 1)
rootlabel = Label(root, text="Total Income : ")
rootlabel.grid(row=4, column = 0)
rootlabel.configure(anchor="center")
root.mainloop()
Above was successful but did not had theme set.
Code after setting the theme:
root = ThemedTk(theme = "xpnative", themebg = True)
root.geometry('500x500')
rootlabel1 = ttk.Label(root, text="Total Income : ", bg = "red")
rootlabel1.grid(row=1, column = 0)
rootlabel1.configure(anchor="center")
btn1 = ttk.Button(root, text = 'Add Income', pady = 20)
btn1.grid(row = 2, column = 1)
rootlabel = ttk.Label(root, text="Total Income : ")
rootlabel.grid(row=4, column = 0)
rootlabel.configure(anchor="center")
root.mainloop()
I am getting the following error,
Traceback (most recent call last):
File "c:/Users/admin/Desktop/Stackoverflow/hskjf.py", line 11, in <module>
rootlabel1 = ttk.Label(root, text="Total Income : ", bg = "red")
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\tkinter\ttk.py", line 759, in __init__
Widget.__init__(self, master, "ttk::label", kw)
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\tkinter\ttk.py", line 557, in __init__
tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2567, in __init__
self.tk.call(
_tkinter.TclError: unknown option "-bg"
As soon as I add prefix ttk. to my buttons or labels
eg. - btn1 = ttk.Button(root, text = 'Add Income', bg = "red")
I am unable to use bg for these buttons/labels.
My Attempts,
1. btn1 = ttk.Button(root, text = 'Add Income', bg = "red")
2. rootlabel1 = ttk.Label(root, text="Total Income : ")
rootlabel1.grid(row=1, column = 0)
rootlabel1.configure(anchor="center", bg = "red")
I am unable resolve the error, what I can do to fix it? so that I get both themes as well as options in my button/label.
(Previously, I wasnt able to use padx, pady but according to suggestion given in comment it worked well.)
Most of the config of grid
layout manager is here.(Also,padx
or pady
is to set the padding.For sure it should be put in layout manager.)
Also,there are some differences between tk
widget and ttk
widget.In tk
widget,you could use bg
to set the background directly.But In ttk
widget,you need to custom a ttk.Style()
In tk
widget:
l1 = tkinter.Label(text="Test", bg="white")
But in ttk
widget,you need to:
style = ttk.Style()
style.configure("BW.TLabel", background="white")
l1 = ttk.Label(text="Test", style="BW.TLabel")
Refer it in: ttk-Styling