81

I am trying to add a custom title to a window but I am having troubles with it. I know my code isn't right but when I run it, it creates 2 windows instead, one with just the title tk and another bigger window with "Simple Prog". How do I make it so that the tk window has the title "Simple Prog" instead of having a new additional window. I dont think I'm suppose to have the Tk() part because when i have that in my complete code, there's an error

from tkinter import Tk, Button, Frame, Entry, END

class ABC(Frame):
    def __init__(self,parent=None):
        Frame.__init__(self,parent)
        self.parent = parent
        self.pack()
        ABC.make_widgets(self)

    def make_widgets(self):
        self.root = Tk()
        self.root.title("Simple Prog")
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Dan
  • 8,263
  • 16
  • 51
  • 53
  • Came here but I was actually looking for http://stackoverflow.com/questions/30009909/change-title-of-tkinter-application-in-os-x-menu-bar in case someone else as well – Andrei Jun 07 '15 at 10:39
  • 1
    Above comment by @Andrei links to [Change title of Tkinter application in OS X Menu Bar](https://stackoverflow.com/questions/30009909/change-title-of-tkinter-application-in-os-x-menu-bar). – questionto42 May 14 '23 at 19:04

12 Answers12

93

If you don't create a root window, Tkinter will create one for you when you try to create any other widget. Thus, in your __init__, because you haven't yet created a root window when you initialize the frame, Tkinter will create one for you. Then, you call make_widgets which creates a second root window. That is why you are seeing two windows.

A well-written Tkinter program should always explicitly create a root window before creating any other widgets.

When you modify your code to explicitly create the root window, you'll end up with one window with the expected title.

Example:

from tkinter import Tk, Button, Frame, Entry, END

class ABC(Frame):
    def __init__(self,parent=None):
        Frame.__init__(self,parent)
        self.parent = parent
        self.pack()
        self.make_widgets()

    def make_widgets(self):
        # don't assume that self.parent is a root window.
        # instead, call `winfo_toplevel to get the root window
        self.winfo_toplevel().title("Simple Prog")

        # this adds something to the frame, otherwise the default
        # size of the window will be very small
        label = Entry(self)
        label.pack(side="top", fill="x")

root = Tk()
abc = ABC(root)
root.mainloop()

Also note the use of self.make_widgets() rather than ABC.make_widgets(self). While both end up doing the same thing, the former is the proper way to call the function.

j_4321
  • 15,431
  • 3
  • 34
  • 61
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Yes, I just ran this code in python ABC().mainloop() and it made 2 things, a window called Simple Prog and another window but just the titlebar part with "tk" – Dan Mar 08 '10 at 04:33
  • 2
    @Dan: My guess is, because you're creating a frame before you create the main window, Tkinter is creating a toplevel window for you (or maybe just a disembodied frame widget). The way you have your code is wrong. You need to create the root window before you create any other windows. – Bryan Oakley Mar 08 '10 at 12:05
87

Here it is nice and simple.

root = tkinter.Tk()
root.title('My Title')

root is the window you create and root.title() sets the title of that window.

timeBenter
  • 887
  • 6
  • 10
16

Try something like:

from tkinter import Tk, Button, Frame, Entry, END

class ABC(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()        

root = Tk()
app = ABC(master=root)
app.master.title("Simple Prog")
app.mainloop()
root.destroy()

Now you should have a frame with a title, then afterwards you can add windows for different widgets if you like.

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
lugte098
  • 2,271
  • 5
  • 21
  • 30
6

One point that must be stressed out is: The .title() method must go before the .mainloop()

Example:


from tkinter import *

# Instantiating/Creating the object
main_menu = Tk()

# Set title
main_menu.title("Hello World")

# Infinite loop
main_menu.mainloop()

Otherwise, this error might occur:

File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 2217, in wm_title
    return self.tk.call('wm', 'title', self._w, string)
_tkinter.TclError: can't invoke "wm" command: application has been destroyed

And the title won't show up on the top frame.

victorkolis
  • 780
  • 13
  • 13
  • Whilst this question has no accepted answer. It was asked 10 years ago and has 11 submitted answers. – GoodJuJu Dec 12 '20 at 22:33
4

Example of python GUI


Here is an example:

from tkinter import *;
screen = Tk();
screen.geometry("370x420"); //size of screen

Change the name of window

  screen.title('Title Name')

Run it:

screen.mainloop();
3

I found this works:

window = Tk()
window.title('Window')

Maybe this helps?

Derek
  • 31
  • 1
3

Easy method:

root = Tk()
root.title('Hello World')
Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
  • root.title('Hello World') should be on the next line – kegaming860 Jul 27 '20 at 17:21
  • 1
    what is the difference between this ans and the rest of the answer? – Delrius Euphoria Jul 27 '20 at 17:28
  • Welcome to SO! Thank you for your time in answering this question. Can you please give more details about your solution? For example, why is your solution better than the other answers? Please read [How to Answer](https://stackoverflow.com/help/how-to-answer). – above_c_level Jul 27 '20 at 17:31
1

Having just done this myself you can do it this way:

from tkinter import Tk, Button, Frame, Entry, END

class ABC(Frame):
    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        self.parent = parent
        self.pack()
        ABC.make_widgets(self)

    def make_widgets(self):
        self.parent.title("Simple Prog")

You will see the title change, and you won't get two windows. I've left my parent as master as in the Tkinter reference stuff in the python library documentation.

Danny Staple
  • 7,101
  • 4
  • 43
  • 56
  • So how exactly is this post different from what i posted? – lugte098 Apr 14 '14 at 10:45
  • My answer was years ago so I won't remember why - my answer is much closer to the OP sample and kept in the class, but they could've been suggested as an edit to yours if they mattered. No idea... – Danny Staple Apr 14 '14 at 12:38
1

For anybody who runs into the issue of having two windows open and runs across this question, here is how I stumbled upon a solution:

The reason the code in this question is producing two windows is because

Frame.__init__(self, parent)

is being run before

self.root = Tk()

The simple fix is to run Tk() before running Frame.__init__():

self.root = Tk()
Frame.__init__(self, parent)

Why that is the case, I'm not entirely sure.

LasevIX_
  • 3
  • 3
Rethipher
  • 336
  • 1
  • 14
0

self.parent is a reference to the actual window, so self.root.title should be self.parent.title, and self.root shouldn't exist.

Colin Valliant
  • 1,899
  • 1
  • 13
  • 20
0
widget.winfo_toplevel().title("My_Title")

changes the title of either Tk or Toplevel instance that the widget is a child of.

Nae
  • 14,209
  • 7
  • 52
  • 79
  • Also, see more on [why there are multiple windows displayed](https://stackoverflow.com/questions/48289171/why-is-toplevel-showing-2-windows/48289343#48289343). – Nae Jan 29 '18 at 23:08
0

I found a solution that should help you:

from tkinter import Tk, Button, Frame, Entry, END

class ABC(Frame):
    def __init__(self,master=None):
        super().__init__(master)
        self.pack()
        self.master.title("Simple Prog")
        self.make_widgets()

    def make_widgets(self):
        pass

root = Tk()
app = ABC(master=root)
app.mainloop()

Found at: docs.python.org

Vianpyro
  • 75
  • 1
  • 6