views:

501

answers:

3

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")
A: 

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

AlcariTheMad
It doesnt seem to work. Am I suppose to keep self.parent = Tk()?
Dan
yes, you are. I only said to change self.root.title to self.parent.title and remove self.root
AlcariTheMad
+1  A: 

Tkinter automatically creates a widnow for you -- the one created when you call Tk(). I'm not sure why you're seeing two windows with the code you posted. Typically that means you're creating a toplevel window at some point in your code.

Try this:

from Tkinter import Tk
root = Tk()
root.wm_title("Hello, world")

This should set the title of the default window to be "Hello, world"

Are you certain that the exact code you posted results in two windows?

Bryan Oakley
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
@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
A: 

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.

lugte098