Mickey
Mickey

Reputation: 141

How can I prevent Toplevel() from opening two additional windows?

I have a program that uses Tkinter and I'm trying to assign a command to a button in my root window that opens one additional window. I'm using Toplevel(), but whenever I click the button I've assigned the command to, two windows open, one with my root window's name and one with the name of the additional window I've assigned.

I've tried using .withdraw and .destroy, to hide or remove this extra root window, but nothing seems to be working.

Here is my code:

import Tkinter
from Tkinter import *

root = Tk()
root.wm_title("VACS")

# # Top label # #

SetParameters = Label(text="Set Parameters", width=110, relief=RIDGE)
SetParameters.grid(row=1, column=0, columnspan=7, padx=5, pady=5)

# # Spatial freq settings # #

SpatialFreq = Label(text="Spatial Frequency", width=15, relief=RIDGE)
SpatialFreq.grid(row=3, column=0, padx=5, pady=5)

From1 = Label(text="from")
From1.grid(row=3, column=1, padx=5, pady=5)

Select1 = Spinbox(from_=0, to=10, width=25)
Select1.grid(row=3, column=2, padx=5, pady=5)

To1 = Label(text="to")
To1.grid(row=3, column=3, padx=5, pady=5)

Select2 = Spinbox(from_=0, to=10, width=25)
Select2.grid(row=3, column=4, padx=5, pady=5)

Steps = Label(text="in steps of")
Steps.grid(row=3, column=5, padx=5, pady=5)

Select3 = Spinbox(from_=0, to=10, width=25)
Select3.grid(row=3, column=6, padx=5, pady=5)

# # Contrast settings # #

Contrast = Label(text="Contrast", width=15, relief=RIDGE)
Contrast.grid(row=5, column=0, padx=5, pady=5)

From2 = Label(text="from")
From2.grid(row=5, column=1, padx=5, pady=5)

Select4 = Spinbox(from_=0, to=10, width=25)
Select4.grid(row=5, column=2, padx=5, pady=5)

To2 = Label(text="to")
To2.grid(row=5, column=3, padx=5, pady=5)

Select5 = Spinbox(from_=0, to=10, width=25)
Select5.grid(row=5, column=4, padx=5, pady=5)

Steps2 = Label(text="in steps of")
Steps2.grid(row=5, column=5, padx=5, pady=5)

Select6 = Spinbox(from_=0, to=10, width=25)
Select6.grid(row=5, column=6, padx=5, pady=5)

# # Test button # #

Test = Button(text="Begin Test", width=25, command=Top)
Test.grid(row=6, column=0, columnspan=7, pady=5)

# # Directory input window # #

def Top():
    Toplevel()
    Toplevel().wm_title("Directory")

root.mainloop()

If you click "Begin Test" in the root window, two extras pop up. I only want the one that says "Directory."

Any ideas?

Upvotes: 0

Views: 534

Answers (1)

TigerhawkT3
TigerhawkT3

Reputation: 49320

You're creating two, since Toplevel() is the constructor call:

Toplevel()
Toplevel().wm_title("Directory")

Instead, create one and save it:

top = Toplevel()
top.wm_title("Directory")

Upvotes: 2

Related Questions