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

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

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

问题

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

  1. import customtkinter as ctk
  2. import tkinter as tk
  3. import tkinter.font as font
  4. passwordNum = 0
  5. visible = False
  6. textentry = None
  7. titleentry = None
  8. mainWindowVar = None
  9. ctk.set_default_color_theme("green")
  10. loginWindow = ctk.CTk()
  11. loginWindow.geometry("500x500")
  12. button_font = font.Font(size=50)
  13. # 1.Name, 2.Password, 3. Perms
  14. loginData = {"1": ["1", ["post"]]}
  15. # ...
  16. def Create_Post(master, windowHeight, windowWidth):
  17. # ...
  18. def Toggle_password_visibility():
  19. # ...
  20. def Button_handler():
  21. # ...
  22. def Post_Button(master, windowHeight, windowWidth):
  23. # ...
  24. def new_post():
  25. # ...
  26. def Create_main_window():
  27. # ...
  28. # ...
  29. 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

  1. import customtkinter as ctk
  2. import tkinter as tk
  3. import tkinter.font as font
  4. passwordNum = 0
  5. visible = False
  6. textentry = None
  7. titleentry = None
  8. mainWindowVar = None
  9. ctk.set_default_color_theme("green")
  10. loginWindow = ctk.CTk()
  11. loginWindow.geometry("500x500")
  12. button_font = font.Font(size=50)
  13. # 1.Name, 2.Password, 3. Perms
  14. loginData = {"1": ["1", ["post"]]}
  15. def Create_Post(master, windowHeight, windowWidth):
  16. distanceBetweenTopAndPost = (windowHeight / 100) * 0.1
  17. distanceBetweenLeftAndPost = (windowWidth / 100) * 0.1
  18. nameLabel = ctk.CTkLabel(master=master, text="test")
  19. nameLabel.place(relx=distanceBetweenLeftAndPost, rely=distanceBetweenTopAndPost)
  20. def Toggle_password_visibility():
  21. global visible
  22. if visible == False:
  23. visible = True
  24. uPasswordEntry.configure(show="")
  25. togglePasswordBtn.configure(text="🤫")
  26. else:
  27. visible = False
  28. uPasswordEntry.configure(show="*")
  29. togglePasswordBtn.configure(text="👀")
  30. def Button_handler():
  31. uName = uNameEntry.get()
  32. uPassword = uPasswordEntry.get()
  33. for name in loginData:
  34. if name == uName:
  35. if uPassword == loginData.get(uName)[passwordNum]:
  36. invalidLogin.place_forget()
  37. loginWindow.destroy()
  38. Create_main_window()
  39. return
  40. invalidLogin.place(relx=0.5, rely=0.35, anchor=tk.N)
  41. def Post_Button(master, windowHeight, windowWidth):
  42. global textentry, titleentry
  43. textentrytext = textentry.get("1.0", "end-1c")
  44. text = titleentry.get()
  45. Create_Post(master, windowHeight, windowWidth)
  46. def new_post():
  47. global titleentry, textentry, mainWindowVar
  48. post_window = ctk.CTkToplevel()
  49. post_window.title("New Post")
  50. post_window.geometry("350x350")
  51. post_window.resizable(0,0)
  52. headingLabel = ctk.CTkLabel(master=post_window, text="Headline", fg_color="transparent")
  53. headingLabel.place(relx=0.06, rely=0.05, anchor=tk.W)
  54. textLabel = ctk.CTkLabel(master=post_window, text="Text", fg_color="transparent")
  55. textLabel.place(relx=0.05, rely=0.23)
  56. global titleentry
  57. titleEntry = ctk.CTkEntry(master=post_window)
  58. titleEntry.place(relx=0.05, rely=0.15, anchor=tk.W)
  59. titleentry = titleEntry
  60. global textentry
  61. textEntry = ctk.CTkTextbox(master=post_window, width=250, height=200)
  62. textEntry.place(relx=0.4, rely=0.6, anchor=tk.CENTER)
  63. textentry = textEntry
  64. 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()))
  65. postBtn.place(relx=0.85, rely=0.1, anchor=tk.W)
  66. def Create_main_window():
  67. mainWindow = ctk.CTkToplevel()
  68. mainWindow.title("Social")
  69. mainWindow.geometry("650x650")
  70. mainWindow.resizable(0,0)
  71. global mainWindowVar
  72. mainWindowVar = mainWindow
  73. ctk_textbox_scrollbar = ctk.CTkScrollbar(mainWindow)
  74. ctk_textbox_scrollbar.grid(row=0, column=1, sticky="ns")
  75. createButton = ctk.CTkButton(master=mainWindow, text="+ New Post", command=new_post)
  76. createButton.place(rely=0.05, relx=0.97, anchor=tk.E)
  77. invalidLogin = ctk.CTkLabel(loginWindow, text="Invalid password", text_color="red", fg_color="transparent")
  78. invalidLogin.place(relx=0.5, rely=0.38, anchor=tk.N)
  79. invalidLogin.place_forget()
  80. uNameEntry = ctk.CTkEntry(master=loginWindow, placeholder_text="Username")
  81. uNameEntry.place(relx=0.5, rely=0.17, anchor=tk.N)
  82. # The entry for the password
  83. uPasswordEntry = ctk.CTkEntry(master=loginWindow, show="*", placeholder_text="Password")
  84. uPasswordEntry.place(relx=0.5, rely=0.27, anchor=tk.N)
  85. # Confirm button
  86. confirmButton = ctk.CTkButton(master=loginWindow, text="Login", command=Button_handler)
  87. confirmButton.place(relx=0.5, rely=0.40, anchor=tk.CENTER)
  88. togglePasswordBtn = ctk.CTkButton(master=loginWindow, text="👀", fg_color="transparent", command=Toggle_password_visibility, width=10, height=15, hover=False)
  89. togglePasswordBtn["font"] = button_font
  90. togglePasswordBtn.place(relx=0.65, rely=0.27)
  91. 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:

  1. def Button_handler():
  2. uName = uNameEntry.get()
  3. uPassword = uPasswordEntry.get()
  4. for name in loginData:
  5. if name == uName:
  6. if uPassword == loginData.get(uName)[passwordNum]:
  7. invalidLogin.place_forget()
  8. # use 'withdraw' to minimize the window w/o destroying it and quitting the app
  9. loginWindow.withdraw()
  10. # don't call 'Create_main_window()' here
  11. # return # you also don't need this
  12. 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:

  1. def Button_handler():
  2. uName = uNameEntry.get()
  3. uPassword = uPasswordEntry.get()
  4. for name in loginData:
  5. if name == uName:
  6. if uPassword == loginData.get(uName)[passwordNum]:
  7. invalidLogin.place_forget()
  8. # use 'withdraw' to minimize the window w/o destroying it and quitting the app
  9. loginWindow.withdraw()
  10. # don't call 'Create_main_window()' here
  11. # return # you also don't need this
  12. 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:

确定