Redgar Pro
Redgar Pro

Reputation: 51

Creating a UI with Tkinter. When trying to adjust the font of my button text and I run the program, I get this error back

This is the code I have written so far...

import tkinter as tk
import sys
import tkinter.font as font
from tkinter.ttk import *

app = tk.Tk()
app.geometry("400x400")
app.configure(bg='gray')


photo = tk.PhotoImage(file=r"C:\ex\ex_button_active.png")
myFont = font.Font(family='Helvetica', size=20, weight='bold')

tk.Label(app, text='ex', bg='gray', font=(
    'Verdana', 15)).pack(side=tk.TOP, pady=10)
app.iconbitmap(r'C:\ex\ex_icon.ico')

ex_activation_button = tk.Button(app,
                                    bg='black',
                                    image=photo,
                                    width=100,
                                    height=100)
ex_stop_button = tk.Button(app,
                              bg='Gray',
                              text='Stop',
                              width=15,
                              height=5)
tk.Button['font'] = myFont

app.title("ex")
ex_activation_button.pack(side=tk.TOP)
ex_stop_button.pack(side=tk.LEFT)

app.mainloop()

I get this error back...

tk.Button['font'] = myFont
TypeError: 'type' object does not support item assignment

Its really a simple font change for the text on the button. Any help greatly appreciated!

Upvotes: 0

Views: 40

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386342

tk.Button is a type, like int or float etc. You would get exactly the same error if you did int['font'] = myFont.

Instead, what you want to do is set the font on an instance of tk.Button. In your case that would be ex_stop_button:

ex_stop_button['font'] = myFont

-or-

ex_stop_button = tk.Button(app,
                           bg='Gray',
                           text='Stop',
                           width=15,
                           height=5,
                           font=myFont)

Upvotes: 1

Related Questions