简体   繁体   English

我可以同时打开两个 Tkinter Windows 吗?

[英]Can I open two Tkinter Windows at the same time?

Is it possible to open 2 windows at the same time?可以同时打开2个窗口吗?

import tkinter as Tk
import random
import math
root = Tk.Tk()
canvas = Tk.Canvas(root)
background_image=Tk.PhotoImage(file="map.png")
canvas.pack(fill=Tk.BOTH, expand=1) # Stretch canvas to root window size.
image = canvas.create_image(0, 0, anchor=Tk.NW, image=background_image)
root.wm_geometry("794x370")
root.title('Map')
root.mainloop()

optimized_root = Tk.Tk()
optimized_canvas = Tk.Canvas(optimized_root)
optimized_root.pack(fill=Tk.BOTH, expand=1)
optimized_image = second.create_image(0, 0, anchor=Tk.NW, image=background_image)
optimized_root.wm_geometry("794x370")
optimized_root.title('Optimized Map')
optimized_root.mainloop()

I'm drawing lines on the first map and then optimizing them to different locations on the second map.我在第一张地图上画线,然后将它们优化到第二张地图上的不同位置。 That part isn't pictured here, but I want to just open both windows simultaneously and have the random starting points going towards their closest location in the second window.此处未显示该部分,但我只想同时打开两个窗口,并使随机起点朝向第二个窗口中最近的位置。 Everything works if I run one at a time, but I have to comment out the other half.如果我一次运行一个,一切正常,但我必须注释掉另一半。

Once you have made your first window the other window needs to be a Toplevel创建第一个窗口后,另一个窗口需要是顶层

Check out this link to tkinters Toplevel page.查看此链接至 tkinters Toplevel页面。

EDIT:编辑:

I was playing around with your code to see if I could manage to get 2 windows to open and display an image.我正在玩你的代码,看看我是否可以设法打开 2 个窗口并显示图像。 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.注意: mainloop()只能调用一次。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM