Search code examples
pythontkintercustomtkinter

Tk window randomly opening along with CTkInputDialog


For this code:

import customtkinter as ctk
import tkinter

ctk.set_appearance_mode("dark")
dialog = ctk.CTkInputDialog(text="Type in a number:", title="Test")
print("Number:", dialog.get_input())

The desirable output is CtkInputDialog Box which asks me the number and prints it in the console. However while running the code. A Tkinter Windows opens alongside. It cannot be closed and closes automatically when CtkInputDialog Box is closed or the code finishes execution. This code is from the Ctk documentations


Solution

  • Every tkinter application requires a root window. If you don't create one explicitly then one will be created the first time you create any other widget.

    In this case, you aren't creating a root window so one is being created for you.

    You should explicitly create a root window. If you don't want it to be visible you can hide it.

    import customtkinter as ctk
    import tkinter
    
    root = ctk.CTk()
    root.withdraw()
    ctk.set_appearance_mode("dark")
    dialog = ctk.CTkInputDialog(text="Type in a number:", title="Test")
    print("Number:", dialog.get_input())