tkinter: Open a new window with a button prompt

125,593

Here's the nearly shortest possible solution to your question. The solution works in python 3.x. For python 2.x change the import to Tkinter rather than tkinter (the difference being the capitalization):

import tkinter as tk
#import Tkinter as tk  # for python 2
    
def create_window():
    window = tk.Toplevel(root)

root = tk.Tk()
b = tk.Button(root, text="Create new window", command=create_window)
b.pack()

root.mainloop()

This is definitely not what I recommend as an example of good coding style, but it illustrates the basic concepts: a button with a command, and a function that creates a window.

Share:
125,593
Eddy Loring
Author by

Eddy Loring

Updated on December 02, 2020

Comments

  • Eddy Loring
    Eddy Loring over 3 years

    How would I be able to open a new window by the user pressing a button in a tkinter GUI? I only need quite simple solutions, and if the code could be explained as well that would be great.

  • Avinash Raj
    Avinash Raj almost 8 years
    I tried this solution, it works but I have the second window which uses grid.. And thus this err TclError: cannot use geometry manager grid inside . which already has slaves managed by pac
  • Anarach
    Anarach about 6 years
    @AvinashRaj You cannot use both .pack() and .grid() in same code. change your .pack() to .grid()
  • Rishabh Deep Singh
    Rishabh Deep Singh almost 5 years
    How Do I add more features to the new window?
  • Russell Smith
    Russell Smith almost 5 years
    @RishabhDeepSingh: use window as the master for additional widgets.