How can I change the font and background of my button with TTK?

75 views Asked by At

In fact, I wanted to make a button for my graphical project with TTK; But sadly I failed to do that. After doing a lot of trial and error, I realized that the problem is with the font and background.

I tried to fix the problem by deleting the font and background from my button, but they were absolutely essential for it. I expected it to work, but the result was an error!(I've got to say that my project is not completed and I'll add so many other tabs and commodities to it; But I wanna fix this problem before continuing.) Here's my code:

import tkinter as tk 
from tkinter import ttk
window = tk.Tk()
window.geometry('800x600')
window.title('store calculations')
window.configure(background='gray')

tab_control = ttk.Notebook(window)

tab_1 = ttk.Frame(tab_control)
tab_2 = ttk.Frame(tab_control)

tab_control.add(tab_1,text='Installment purchase')
tab_control.add(tab_2,text='Delmonti')
tab_control.pack(expand=1,fill='both')

def run ():
    amount = int(txt_amount.get())
    prepayment = int(txt_prepayment.get())
    installments = int(txt_installments.get())
    money_left = amount - prepayment
    each_month = money_left / installments + money_left * 3 / 100
    if each_month == int(each_month):
        each_month = int(each_month)
    else:
        each_month = round(each_month,2)
    txt_each_month.insert(0,str(each_month))

def clear():
    txt_amount.delete(first=0,last='end')
    txt_prepayment.delete(first=0,last='end')
    txt_installments.delete(first=0,last='end')
    txt_each_month.delete(first=0,last='end')

def one_third():
    amount = int(txt_amount.get())
    one_third_of_the_amount = amount * 1 / 3
    if one_third_of_the_amount == int(one_third_of_the_amount):
        one_third_of_the_amount = int(one_third_of_the_amount)
    else:
        one_third_of_the_amount = round(one_third_of_the_amount,2)
    txt_prepayment.insert(0,str(one_third_of_the_amount))

lbl_amount = tk.Label(tab_1,text='Amount',font=('Tahoma',18))
lbl_amount.grid(column=0,row=0)
txt_amount = tk.Entry(tab_1,font=('Tahoma',18))
txt_amount.grid(column=1,row=0)

lbl_prepayment = tk.Label(tab_1,text='Prepayment',font=('Tahoma',18))
lbl_prepayment.grid(column=0,row=1)
txt_prepayment = tk.Entry(tab_1,font=('Tahoma',18))
txt_prepayment.grid(column=1,row=1)

lbl_installments = tk.Label(tab_1,text='installments',font=('Tahoma',18))
lbl_installments.grid(column=0,row=2)
txt_installments = tk.Entry(tab_1,font=('Tahoma',18))
txt_installments.grid(column=1,row=2)

btn_run = ttk.Button(tab_1,text='Run',font=('Tahoma',18),background='white',command=run)
btn_run.grid(column=1,row=3)

lbl_each_month = tk.Label(tab_1,text='each month',font=('Tahoma',20))
lbl_each_month.grid(column=0,row=4)
txt_each_month = tk.Entry(tab_1,font=('Tahoma',20))
txt_each_month.grid(column=1,row=4)

btn_clear = ttk.Button(tab_1,text='clear',background='white',font=('Tahoma',18),command=clear)
btn_clear.grid(column=1,row=5)

btn_one_third = ttk.Button(tab_1,text='1/3',background='white',font=('Tahoma',18),command=one_third)
btn_one_third.grid(column=2,row=1)

lbl_note = tk.Label(tab_1,text='The added percentage is 3% per month!',font=('Tahoma',18))
lbl_note.grid(column=1,row=7)

lbl_mixer = tk.Label(tab_2,text='Mixer : ',font=('Tahoma',16))
lbl_mixer.grid(column=0,row=0)
lbl_mixer_price = tk.Label(tab_2,text='3200000',font=('Tahoma',16))
lbl_mixer_price.grid(column=1,row=0)

lbl_pro_agitator = tk.Label(tab_2,text='Pro agitator : ',font=('Tahoma',16)).grid(column=0,row=1)
lbl_pro_agitator_price = tk.Label(tab_2,text='4400000',font=('Tahoma',16)).grid(column=1,row=1)

lbl_toaster = tk.Label(tab_2,text='Toaster : ',font=('Tahoma',16)).grid(column=0,row=2)
lbl_toaster_price = tk.Label(tab_2,text='2400000',font=('Tahoma',16)).grid(column=1,row=2)

lbl_food_processor = tk.Label(tab_2,text='4-function food processor : ',font=('Tahoma',16)).grid(column=0,row=3)
lbl_food_processor_price = tk.Label(tab_2,text='7800000',font=('Tahoma',16)).grid(column=1,row=3)

lbl_fast_cooker = tk.Label(tab_2,text='Geepas fast cooker : ',font=('Tahoma',16)).grid(column=0,row=4)
lbl_fast_cooker_price = tk.Label(tab_2,text='7300000',font=('Tahoma',16)).grid(column=1,row=4)

lbl_citrus_juicer = tk.Label(tab_2,text='Citrus juicer : ',font=('Tahoma',16)).grid(column=0,row=5)
lbl_citrus_juicer_price = tk.Label(tab_2,text='3700000',font=('Tahoma',16)).grid(column=1,row=5)

window.mainloop()
2

There are 2 answers

1
Jeremy On

As far as your buttons go not show the color and correct font, the simple fix is you need to change the Buttons from

... = ttk.Button(tab_1,...)

to

... = tk.Button(tab_1,...)

and that should fix the styling of your buttons

if you really want to use ttk to set the style up for you buttons you will need to add

style.map("Mod.TButton",
      background = [("active", "white"), ("!active", "white")])

btn_run = ttk.Button(tab_1,text='Run',style="Mod.TButton",command=run)
0
Александр Юрков On

Hi there is a bug in your code, when you import tkinter library, you should carefully use its features in your applicationenter image description here

enter image description here

59 btn_run = ttk.Button(tab_1,text='Run',font=('Tahoma',18),background='white',command=run) 60 btn_run.grid(column=1,row=3)


59 btn_run = tk.Button(tab_1,text='Run',font=('Tahoma',18),background='white',command=run) 60 btn.pack()

I tried to use your font and it changed successfully.