TkInter displays extra windows after winfo is called.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gregory Sherrod
    New Member
    • Aug 2010
    • 2

    TkInter displays extra windows after winfo is called.

    Code:
    #! /usr/bin/python
    # This one creates three windows because of the Tk().winfo_screenwidth(),
    # Tk().winfo_screenheight() call
    
    from Tkinter import *
    
    def quit(): sys.exit(0)
    
    root = Tk()
    root.title("Test screen")
    w, h = Tk().winfo_screenwidth(), Tk().winfo_screenheight()
    h = h * 0.75
    paper = Canvas(root, width = h, height = h, bg = "#000020")
    
    a = h / 10
    b = h - a
    
    paper.create_line(a, a, b, b, fill="white")
    paper.create_line(a, b, b, a, fill="white")
    
    exitB = Button(text="Exit", width=15, command=quit)
    exitB.pack()
    
    paper.pack()
    mainloop()
    Code:
    #! /usr/bin/python
    # This one creates only a single window.
    
    from Tkinter import *
    
    def quit(): sys.exit(0)
    
    root = Tk()
    root.title("Test screen")
    
    h = 400
    paper = Canvas(root, width = h, height = h, bg = "#000020")
    
    a = h / 10
    b = h - a
    
    paper.create_line(a, a, b, b, fill="white")
    paper.create_line(a, b, b, a, fill="white")
    
    exitB = Button(text="Exit", width=15, command=quit)
    exitB.pack()
    
    paper.pack()
    mainloop()
    Any idea how to easily fix this? Thanks in advance...

    Greg
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    You are starting 3 instances of Tk(). Use root instead
    Code:
    ##--- instance 1
    root = Tk()
    root.title("Test screen")
    
    ##-- instances 2 & 3
    w, h = Tk().winfo_screenwidth(), Tk().winfo_screenheight()
    
    """ this opens one window only
    ""
    root = Tk()
    root.title("Test screen")
    w, h = root.winfo_screenwidth(), root.winfo_screenheight()
    Also, to exit, use the quit command instead of sys.exit.
    Code:
    exitB = Button(text="Exit", width=15, command=root.quit)

    Comment

    • Gregory Sherrod
      New Member
      • Aug 2010
      • 2

      #3
      That fixed it!

      Like most errors, this one is trivial when solved, but opaque beforehand. Many thanks, dwblas!

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Gregory,

        Since you have gotten your answer, please mark the appropriate post with the 'choose as best answer button'. This makes it easier for the next person to find the solution.

        Comment

        Working...