How to create a new window in tkinter
To create a new window in Tkinter, you can use the Toplevel
class. Here's a simple example:
import tkinter as tk
def create_new_window():
new_window = tk.Toplevel(root)
new_window.title("New Window")
label = tk.Label(new_window, text="This is a new window")
label.pack()
root = tk.Tk()
root.title("Main Window")
button = tk.Button(root, text="Create new window", command=create_new_window)
button.pack()
root.mainloop()
In this example, we create a new window by calling tk.Toplevel(root)
, where root
is the main window. We then set the title of the new window with new_window.title("New Window")
. We create a label with tk.Label(new_window, text="This is a new window")
and pack it with label.pack()
.
We also create a button in the main window with tk.Button(root, text="Create new window", command=create_new_window)
. When this button is clicked, the create_new_window
function is called, which creates a new window.
You can customize the new window as needed by adding more widgets, changing its size, and so on.
Note that Toplevel
windows are independent of the main window, so they can have their own title bars, menus, and other features. They are also modal, meaning that they will block the main window until they are closed.
If you want to create a new window that is not modal, you can use the overrideredirect
method to remove the title bar and other decorations. For example:
new_window = tk.Toplevel(root)
new_window.overrideredirect(True)
This will create a new window that is not modal and does not have a title bar.