Tom bass
Tom bass

Reputation: 45

Can't get tkinter window title to change from 'tk'

I thought the line to change the title of a window was:

tk=Tk()

tk.title="my title"

But I can't get the title to change in my code:

def welcome_screen():
    #creates the login window
    window = Tk()
    window.title = "Login Screen"
    window.geometry("960x540+450+250")
    canvas = Canvas(window, width=960, height=540, bd=10, bg='#494949')
    canvas.pack()
    window.mainloop()

welcome_screen()

The title should change to "login screen" but when run it just says 'tk'. everything else works as expected.

Upvotes: 4

Views: 2672

Answers (2)

Nouman
Nouman

Reputation: 7303

title is a function. Use it like:

root.title("My new title")

Example:

root = Tk()
root.title("my title")
root.mainloop()

Upvotes: 7

martineau
martineau

Reputation: 123423

As @jasonharper pointed out in a comment, title is a method not an attribute, so you need to call it and pass the title string. Another problem with your code is that it calls Tk() twice, which is generally not going to work. It also looks like you using a from tkinter import * which is considered by many to be a poor programming practice because it can cause hard-to-debug name collisions.

So here's code that addressing all those issues:

import tkinter as tk

def welcome_screen():
    """ Create and display login window. """
    window = tk.Tk()
    window.title("Login Screen")
    window.geometry("960x540+450+250")
    canvas = tk.Canvas(window, width=960, height=540, bd=10, bg='#494949')
    canvas.pack()
    window.mainloop()

welcome_screen()

Upvotes: 3

Related Questions