我可以同时打开两个 Tkinter Windows 吗? [英] Can I open two Tkinter Windows at the same time?
问题描述
可以同时打开两个窗口吗?
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.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.
推荐答案
一旦你制作了你的第一个窗口,另一个窗口需要是一个 Toplevel
Once you have made your first window the other window needs to be a Toplevel
查看 tkinters Toplevel 页面的链接.
Check out this link to tkinters Toplevel page.
我正在玩弄您的代码,看看我是否可以设法打开 2 个窗口并显示图像.这是我想出的.它可能并不完美,但它是一个开始,应该为您指明正确的方向.
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.
注意:mainloop()
只能调用一次.
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()
这篇关于我可以同时打开两个 Tkinter Windows 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!