Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Tkinter in python to edit the title bar

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") 
like image 543
Dan Avatar asked Mar 07 '10 06:03

Dan


People also ask

Can I change the title bar in Tkinter?

Tkinter doesn't come with the ability to customize your titlebar built into it. We'll have to get creative and hack around a bit to create a custom title bar. We'll create a titlebar that we can change the color.

How do I change my title on Tk?

Use the title() method to change the title of the window. Use the geometry() method to change the size and location of the window. Use the resizable() method to specify whether a window can be resizable horizontally or vertically.


1 Answers

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.

like image 108
Bryan Oakley Avatar answered Oct 12 '22 12:10

Bryan Oakley