Shephard_
Shephard_

Reputation: 31

I can't change the font of my button in tkinter without an error

I am currently working on a simple GUI to assist me with a future project, and I am just getting started by placing labels and a button to open a new window. Currently it works, however whenever I try to change the font of the text on the button (with getStarted.config(font=("Courier",15))) it doesn't work, while in other projects I usually have no issue changing the button font. (When I comment out the one line that changes the font/size of the text it works, just with default text.) I suspect it has something to do with my Class system, however I am unsure on how to fix it, so any help is appreciated.

from tkinter.ttk import *

def getStarted():
    NewWindow(homeWindow)


class NewWindow(Toplevel):

    def __init__(self, homeWindow=None):
        super().__init__(master=homeWindow)
        self.title("Information")
        self.geometry("600x600")
        label = Label(self, text="Enter your information")
        label.pack()



homeWindow = Tk()
homeWindow.title("Rota System")
homeWindow.geometry("600x600")

welcome = Label(homeWindow, text="Rota System")
welcome.config(font=("Courier", 30))
welcome.pack(side=TOP, pady=10)

byTom = Label(homeWindow, text="Created by Tom")
byTom.config(font=("Courier", 10))
byTom.pack(side=TOP, pady=10)

getStarted = Button(homeWindow, text="Click here to get started", command = getStarted)
getStarted.config(font=("Courier",15)) #This is the line causing issues, it works in other projects
getStarted.place (x=25,y=500,height=50,width=550)




mainloop()

Upvotes: 0

Views: 983

Answers (1)

TheLizzard
TheLizzard

Reputation: 7710

Try this:

import tkinter as tk
from tkinter import ttk

def get_started():
    NewWindow(home_window)


class NewWindow(tk.Toplevel):
    def __init__(self, master=None):
        super().__init__(master=master)
        self.title("Information")
        self.geometry("600x600")
        label = ttk.Label(self, text="Enter your information")
        label.pack()


home_window = tk.Tk()
home_window.title("Rota System")
home_window.geometry("600x600")

welcome = ttk.Label(home_window, text="Rota System")
welcome.config(font=("Courier", 30))
welcome.pack(side="top", pady=10)

byTom = ttk.Label(home_window, text="Created by Tom")
byTom.config(font=("Courier", 10))
byTom.pack(side="top", pady=10)

# Use `tk.Button` instead of `ttk.Button`
getStarted = tk.Button(home_window, text="Click here to get started",
                        command=get_started)
getStarted.config(font=("Courier", 15))
getStarted.place(x=25, y=500, height=50, width=550)

home_window.mainloop()

This is why from ... import * is discouraged. You were using ttk.Button which doesn't have the font attribute. The tk.Button has the font attribute so I think that is what you wanted to use.

Upvotes: 2

Related Questions