Using Tkinter in python to edit the title bar

Using Tkinter in python to edit the title bar

If you dont create a root window, Tkinter will create one for you when you try to create any other widget. Thus, in your __init__, because you havent 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, youll 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):
        # dont 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.

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.

Using Tkinter in python to edit the title bar

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.

Leave a Reply

Your email address will not be published.