Home > other >  tkinter having different titles for two windows
tkinter having different titles for two windows

Time:02-11

I want to use tkinter to create an application that has two windows with different titles.

Here's my code.

import tkinter as tk

class Demo1:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.button1 = tk.Button(self.frame, text = 'New Window', width = 25, command = self.new_window)
        self.button1.pack()
        self.frame.pack()
        self.new_window()

    def new_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = Demo2(self.newWindow)

class Demo2:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows)
        self.quitButton.pack()
        self.frame.pack()

    def close_windows(self):
        self.master.destroy()

def main(): 
    root = tk.Tk()
    root.wm_title('title1') # How can I set different titles for Demo1 and Demo2?
    app = Demo1(root)
    root.mainloop()

if __name__ == '__main__':
    main()

If you run it, you can see two windows having the same title namedtitle1.

enter image description here

How can I reorgganize my code so that Demo1 and Demo2 have different titles?

CodePudding user response:

Simply you can apply this:

def new_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.newWindow.title("title2")
        self.app = Demo2(self.newWindow)

Here You set the title for the new TopLevel window so it's different than the main window

  • Related