Search code examples
pythontkintercustomtkinter

Why is my CustomTkinter creating a blank window in addition to the one I created?


I'm trying to Create a new window in line 87. it creates the window and also a white one although I have created only one window

import customtkinter as ctk
import tkinter as tk
import tkinter.font as font

passwordNum = 0
visible = False
textentry = None
titleentry = None
mainWindowVar = None

ctk.set_default_color_theme("green")
loginWindow = ctk.CTk()
loginWindow.geometry("500x500")
button_font = font.Font(size=50) 

# 1.Name, 2.Password, 3. Perms
loginData = {"1": ["1", ["post"]]}


def Create_Post(master, windowHeight, windowWidth):
    distanceBetweenTopAndPost = (windowHeight / 100) * 0.1
    distanceBetweenLeftAndPost = (windowWidth / 100) * 0.1
    nameLabel = ctk.CTkLabel(master=master, text="test")
    nameLabel.place(relx=distanceBetweenLeftAndPost, rely=distanceBetweenTopAndPost)
    

def Toggle_password_visibility():
    global visible
    if visible == False:
        visible = True
        uPasswordEntry.configure(show="")
        togglePasswordBtn.configure(text="🤫")
    else:
        visible = False
        uPasswordEntry.configure(show="*")
        togglePasswordBtn.configure(text="👀")


def Button_handler():
    uName = uNameEntry.get()
    uPassword = uPasswordEntry.get()
    for name in loginData:
        if name == uName:
            if uPassword == loginData.get(uName)[passwordNum]:
                invalidLogin.place_forget()
                loginWindow.destroy()
                Create_main_window()
                return
    invalidLogin.place(relx=0.5, rely=0.35, anchor=tk.N)


def Post_Button(master, windowHeight, windowWidth):
    global textentry, titleentry
    textentrytext = textentry.get("1.0", "end-1c")
    text = titleentry.get()
    Create_Post(master, windowHeight, windowWidth)


def new_post():
    global titleentry, textentry, mainWindowVar
    post_window = ctk.CTkToplevel()
    post_window.title("New Post")
    post_window.geometry("350x350")
    post_window.resizable(0,0)

    headingLabel = ctk.CTkLabel(master=post_window, text="Headline", fg_color="transparent")
    headingLabel.place(relx=0.06, rely=0.05, anchor=tk.W)

    textLabel = ctk.CTkLabel(master=post_window, text="Text", fg_color="transparent")
    textLabel.place(relx=0.05, rely=0.23)

    global titleentry
    titleEntry = ctk.CTkEntry(master=post_window)
    titleEntry.place(relx=0.05, rely=0.15, anchor=tk.W)
    titleentry = titleEntry

    global textentry
    textEntry = ctk.CTkTextbox(master=post_window, width=250, height=200)
    textEntry.place(relx=0.4, rely=0.6, anchor=tk.CENTER)
    textentry = textEntry

    postBtn = ctk.CTkButton(master=post_window, text="Post", width=30, height=30, command=lambda: Post_Button(mainWindowVar, post_window.winfo_height(), post_window.winfo_width()))
    postBtn.place(relx=0.85, rely=0.1, anchor=tk.W)


def Create_main_window():
    mainWindow = ctk.CTkToplevel()
    mainWindow.title("Social")
    mainWindow.geometry("650x650")
    mainWindow.resizable(0,0)
    global mainWindowVar
    mainWindowVar = mainWindow


    ctk_textbox_scrollbar = ctk.CTkScrollbar(mainWindow)
    ctk_textbox_scrollbar.grid(row=0, column=1, sticky="ns")

    createButton = ctk.CTkButton(master=mainWindow, text="+ New Post", command=new_post)
    createButton.place(rely=0.05, relx=0.97, anchor=tk.E)


invalidLogin = ctk.CTkLabel(loginWindow, text="Invalid password", text_color="red", fg_color="transparent")
invalidLogin.place(relx=0.5, rely=0.38, anchor=tk.N)
invalidLogin.place_forget()

uNameEntry = ctk.CTkEntry(master=loginWindow, placeholder_text="Username")
uNameEntry.place(relx=0.5, rely=0.17, anchor=tk.N)

# The entry for the password
uPasswordEntry = ctk.CTkEntry(master=loginWindow, show="*", placeholder_text="Password")
uPasswordEntry.place(relx=0.5, rely=0.27, anchor=tk.N)

# Confirm button
confirmButton = ctk.CTkButton(master=loginWindow, text="Login", command=Button_handler)
confirmButton.place(relx=0.5, rely=0.40, anchor=tk.CENTER)

togglePasswordBtn = ctk.CTkButton(master=loginWindow, text="👀", fg_color="transparent", command=Toggle_password_visibility, width=10, height=15, hover=False)
togglePasswordBtn["font"] = button_font
togglePasswordBtn.place(relx=0.65, rely=0.27)

loginWindow.mainloop()

This is the code I use

I tried to Create an new window but he creates 2 windows. 1 "normal" one and one only white whit the name "tk" I tried to delete the lines and the blank window doesn't get created anyone of you had this problem before? I also tried to Comment it out and run it but it doesn't work either I don't understand it :/


Solution

  • Since you want loginWindow to serve as your application's main window, but want to hide it while post_window is being shown, you should update Button_handler as follows:

    def Button_handler():
        uName = uNameEntry.get()
        uPassword = uPasswordEntry.get()
        for name in loginData:
            if name == uName:
                if uPassword == loginData.get(uName)[passwordNum]:
                    invalidLogin.place_forget()
                    # use 'withdraw' to minimize the window w/o destroying it and quitting the app
                    loginWindow.withdraw()
                    # don't call 'Create_main_window()' here
                    # return  # you also don't need this
        invalidLogin.place(relx=0.5, rely=0.35, anchor=tk.N)
    

    Then you should delete the Create_main_window function altogether.

    You can use loginWindow.deiconify() to restore the main window when you need it again.