Can I open two Tkinter Windows at the same time?

后端 未结 1 793
遇见更好的自我
遇见更好的自我 2021-01-26 16:59

Is it possible to open 2 windows at the same time?

import tkinter as Tk
import random
import math
root = Tk.Tk()
canvas = Tk.Canvas(root)
background_image=Tk.Ph         


        
1条回答
  •  隐瞒了意图╮
    2021-01-26 17:34

    Once you have made your first window the other window needs to be a Toplevel

    Check out this link to tkinters Toplevel page.

    EDIT:

    I was playing around with your code to see if I could manage to get 2 windows to open and display an image. Here is what I came up with. It might not be perfect but its a start and should point you in the right direction.

    I put the toplevel in as a defined function and then called it as part of the main loop.

    NOTE: The mainloop() can only be called once.

    from tkinter import *
    import random
    import math
    
    root = Tk()
    canvas = Canvas(root)
    background_image=PhotoImage(file="map.png")
    canvas.pack(fill=BOTH, expand=1) # Stretch canvas to root window size.
    image = canvas.create_image(0, 0, anchor=NW, image=background_image)
    root.wm_geometry("794x370")
    root.title('Map')
    
    def toplevel():
        top = Toplevel()
        top.title('Optimized Map')
        top.wm_geometry("794x370")
        optimized_canvas = Canvas(top)
        optimized_canvas.pack(fill=BOTH, expand=1)
        optimized_image = optimized_canvas.create_image(0, 0, anchor=NW, image=background_image)
    
    toplevel()
    
    root.mainloop()
    

    0 讨论(0)
提交回复
热议问题