Mike Pylypyshyn
Mike Pylypyshyn

Reputation: 452

I can't create a second window using tkinter, on os x

When creating a second window using python 3.6 and tkinter, it is not responsible. I`m using os x 10.11.6. In other systems such as Ubuntu, this code works.

from tkinter import *

class win2:

    def __init__(self):
        self.root = Tk()
        self.root.mainloop()

class win1:

    def __init__(self):
        self.root = Tk()

        self.button = Button(self.root)
        self.button.bind('<Button-1>', self.buttonFunc)
        self.button.pack()

        self.root.mainloop()

    def buttonFunc(self, event):
        windows2 = win2()

if __name__ == "__main__":
    window1 = win1()

Upvotes: 0

Views: 289

Answers (2)

Mike Pylypyshyn
Mike Pylypyshyn

Reputation: 452

This code works for me.

from tkinter import *

class win1:

    def __init__(self):
        root = Tk()
        button = Button(root)
        button.bind('<Button-1>', self.buttonFunc)
        button.pack()
        root.mainloop()

    def buttonFunc(self, event):
        window2 = win2()

class win2(win1):

    def __init__(self):
        top = Toplevel()

if __name__ == "__main__":
    window1 = win1()

Upvotes: 0

Novel
Novel

Reputation: 13729

It's a very bad idea to use Tk() more than once in your program. Use it to make the root window, and then use Toplevel() to make any additional windows.

def buttonFunc(self, event):
    Toplevel(self.root)

That said, it still looks like you are trying to do something the hard way. Can you describe better what your end goal is?

To make a modal window (a popup) use code like this:

try: #python3 imports
    import tkinter as tk
except ImportError: #python3 failed, try python2 imports
    import Tkinter as tk

class Main(tk.Frame):
    def __init__(self, master=None, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)

        lbl = tk.Label(self, text="this is the main frame")
        lbl.pack()

        btn = tk.Button(self, text='click me', command=self.open_popup)
        btn.pack()

    def open_popup(self):
        print("runs before the popup")
        Popup(self)
        print("runs after the popup closes")

class Popup(tk.Toplevel):
    """modal window requires a master"""
    def __init__(self, master, **kwargs):
        tk.Toplevel.__init__(self, master, **kwargs)

        lbl = tk.Label(self, text="this is the popup")
        lbl.pack()

        btn = tk.Button(self, text="OK", command=self.destroy)
        btn.pack()

        # The following commands keep the popup on top.
        # Remove these if you want a program with 2 responding windows.
        # These commands must be at the end of __init__
        self.transient(master) # set to be on top of the main window
        self.grab_set() # hijack all commands from the master (clicks on the main window are ignored)
        master.wait_window(self) # pause anything on the main window until this one closes

def main():
    root = tk.Tk()
    window = Main(root)
    window.pack()
    root.mainloop()

if __name__ == '__main__':
    main()

Upvotes: 2

Related Questions