为什么我的CustomTkinter创建了一个空白窗口,而我已经创建了一个窗口?

huangapple go评论67阅读模式
英文:

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

问题

我试图在第87行创建一个新窗口。它创建了一个窗口,但也创建了一个白色的窗口,尽管我只创建了一个窗口。

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):
    # ...

def Toggle_password_visibility():
    # ...

def Button_handler():
    # ...

def Post_Button(master, windowHeight, windowWidth):
    # ...

def new_post():
    # ...

def Create_main_window():
    # ...

# ...

loginWindow.mainloop()

这是我使用的代码。我尝试创建一个新窗口,但它创建了两个窗口。一个是“正常”的窗口,另一个是只有名称为“tk”的白色窗口。我尝试删除这些行,但是空白窗口就不会创建了。你们有没有人遇到过这个问题?我还尝试将其注释掉并运行,但也不起作用,我不明白是为什么:/

英文:

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 :/

答案1

得分: 1

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.

英文:

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.

huangapple
  • 本文由 发表于 2023年5月25日 20:15:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定