LoveSpock
LoveSpock

Reputation: 15

How to code for a new window

how can I code for a new window? I have a button that creates a new one but I like to code for it and I don't know how. I think I must define the new window in any way but I don't know how to do this because for opening a new window with help of a button you must define the window self, but no name.

Thanks for helping!

I have create the button and its command in this way:

from Tkinter import *
import Tkinter as tk

master = tk.Tk()

def create_window(): #Definion und Festlegung neues Fenster
    toplevel = Toplevel()
    toplevel.title('result')
    toplevel.geometry('1500x1000')
    toplevel.focus_set()

Button(master, text='forward', command=create_window).pack(padx=5, anchor=N, pady=4)

master.mainloop()

Upvotes: 0

Views: 12208

Answers (1)

Khristos
Khristos

Reputation: 983

Coding for the new window (or creating widgets in the new window) is similar to how you do it in the main window. Just pass the new window (toplevel) as the parent.

Here is an example that creates a Label and an Entry widgets in the new window.

from Tkinter import *
import Tkinter as tk

master = tk.Tk() # Create the main window

def create_window(): #Definion und Festlegung neues Fenster
    toplevel = Toplevel()
    toplevel.title('result')
    toplevel.geometry('1500x1000')

    # Create widges in the new window
    label = tk.Label(toplevel, text="A Label", fg='blue')
    entry = tk.Entry(toplevel)
    label.pack()
    entry.pack()

    toplevel.focus_set()

Button(master, text='forward', command=create_window).pack(padx=5, anchor=N, pady=4)

master.mainloop()

Upvotes: 1

Related Questions