20

I am trying to figure out how to change the title of a Tkinter Frame. Below is simplified code that mimics the portion of my program where I am trying to change the title:

from Tkinter import *

class start_window(Frame):
    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        Frame.pack(self)
        Label(self, text = 'Test', width=30).pack()

if __name__ == '__main__':
    start_window().mainloop()

With this sample code the Frame has the standard "tk" title but I would like to change it to something like "My Database". I've tried everything I can think of with no success. Any help would be appreciated.

martineau
  • 119,623
  • 25
  • 170
  • 301
user3798654
  • 431
  • 2
  • 9
  • 19

3 Answers3

43

Try this:

if __name__ == '__main__':
    root = Tk()
    root.title("My Database")
    root.geometry("500x400")
    app = start_window(root)
    root.mainloop()
letsc
  • 2,515
  • 5
  • 35
  • 54
2

First, you should be explicitly creating the main window by creating an instance of Tk. When you do, you can use the reference to this window to change the title.

I also recommend not using a global import. Instead, import tkinter by name,and prefix your tkinter commands with the module name. I use the name tk to cut down on typing:

import Tkinter as tk

class start_window(tk.Frame):
    def __init__(self, parent=None):
        tk.Frame.__init__(self, parent)
        tk.Frame.pack(self)
        tk.Label(self, text = 'Test', width=30).pack()

if __name__ == '__main__':
    root = tk.Tk()
    root.wm_title("This is my title")
    start_window(root)
    root.mainloop()

Finally, to make your code easier to read I suggest giving your class name an uppercase first letter to be consistent with almost all python programmers everywhere:

class StartWindow(...):

By using the same conventions as everyone else, it makes it easier for us to understand your code.

For more information about naming conventions used by the tkinter community, see PEP8

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Hi, Thank you very much for your response. Unfortunately this didn't work for me. After making the suggested modifications I end up with two widget windows, one that looks like my original widget and a second that is blank but has the desired title. However the next answer did solve my issue. Thanks I really do appreciate your help. – user3798654 Nov 10 '15 at 19:06
  • You will end up with two windows if you create some widgets before you call `tk.Tk()`. You should explicitly create the root window before you create any other widgets. – Bryan Oakley Nov 10 '15 at 19:11
2

I generally start my tkinter apps with

#!/usr/local/bin/python3

import Tkinter as tk

root = Tk() 

root.title('The name of my app')

root.minsize(300,300)
root.geometry("800x800")

root.mainloop()
Athina
  • 55
  • 9