Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tkinter: Open a new window with a button prompt [closed]

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.

like image 312
Eddy Loring Avatar asked Dec 24 '14 15:12

Eddy Loring


1 Answers

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.

like image 172
Bryan Oakley Avatar answered Nov 04 '22 07:11

Bryan Oakley