TkInter在调用winfo后显示额外窗口。

选择 | 换行 | 行号
  1. #! /usr/bin/python
  2. # This one creates three windows because of the Tk().winfo_screenwidth(),
  3. # Tk().winfo_screenheight() call
  4.  
  5. from Tkinter import *
  6.  
  7. def quit(): sys.exit(0)
  8.  
  9. root = Tk()
  10. root.title("Test screen")
  11. w, h = Tk().winfo_screenwidth(), Tk().winfo_screenheight()
  12. h = h * 0.75
  13. paper = Canvas(root, width = h, height = h, bg = "#000020")
  14.  
  15. a = h / 10
  16. b = h - a
  17.  
  18. paper.create_line(a, a, b, b, fill="white")
  19. paper.create_line(a, b, b, a, fill="white")
  20.  
  21. exitB = Button(text="Exit", width=15, command=quit)
  22. exitB.pack()
  23.  
  24. paper.pack()
  25. mainloop()
  26.  
选择 | 换行 | 行号
  1. #! /usr/bin/python
  2. # This one creates only a single window.
  3.  
  4. from Tkinter import *
  5.  
  6. def quit(): sys.exit(0)
  7.  
  8. root = Tk()
  9. root.title("Test screen")
  10.  
  11. h = 400
  12. paper = Canvas(root, width = h, height = h, bg = "#000020")
  13.  
  14. a = h / 10
  15. b = h - a
  16.  
  17. paper.create_line(a, a, b, b, fill="white")
  18. paper.create_line(a, b, b, a, fill="white")
  19.  
  20. exitB = Button(text="Exit", width=15, command=quit)
  21. exitB.pack()
  22.  
  23. paper.pack()
  24. mainloop()
  25.  

你知道怎么轻松解决这个问题吗?先谢谢你……
格雷格

# 回答1


您正在启动Tk()的3个实例。改用超级用户

选择 | 换行 | 行号
  1. ##--- instance 1
  2. root = Tk()
  3. root.title("Test screen")
  4.  
  5. ##-- instances 2 & 3
  6. w, h = Tk().winfo_screenwidth(), Tk().winfo_screenheight()
  7.  
  8. """ this opens one window only
  9. ""
  10. root = Tk()
  11. root.title("Test screen")
  12. w, h = root.winfo_screenwidth(), root.winfo_screenheight() 

此外,要退出,请使用Quit命令,而不是sys.exit。

选择 | 换行 | 行号
  1. exitB = Button(text="Exit", width=15, command=root.quit)
# 回答2


像大多数错误一样,这个错误在解决时是微不足道的,但在解决之前是不透明的。非常感谢,德布拉斯!
# 回答3


格雷戈里
既然您已经得到了答案,请在相应的帖子上打上"选择为最佳答案按钮"。这使得下一个人更容易找到解决方案。

标签: python

添加新评论