简体   繁体   English

Tkinter 无法打开多张照片

[英]Tkinter can't open mutiple photos

Hello I am trying to open multiple tinker images.您好,我正在尝试打开多个修补程序图像。 But when the first image loads up it stops the rest of the code from running please help.但是当第一个图像加载时,它会停止运行其余的代码,请帮忙。

Here is my code这是我的代码

from tkinter import *
from PIL import ImageTk, Image

root = Tk()

canvas=Canvas(root,width=420,height=560)
image= ImageTk.PhotoImage(Image.open("C:\Users\photo.jpg"))
root.geometry("420x560+0+0")

canvas.create_image(0,0, image= image, anchor=NW)
canvas.pack()
root.overrideredirect(1)

root = Tk()

canvas=Canvas(root,width=420,height=560)
image2= ImageTk.PhotoImage(Image.open("C:\Users\photo.jpg"))
root.geometry("420x560+77+77")

canvas.create_image(0,0, image= image2, anchor=NW)
canvas.pack()
root.overrideredirect(1)

root.mainloop()

I can not run this code in my computer,but just see your code.我无法在我的计算机上运行此代码,但只需查看您的代码。

You have used root = Tk() in line 4. Then you also use root = Tk() in line 14.您在第 4 行中使用了root = Tk() 。然后您也在第 14 行中使用了root = Tk()

And when you use mainloop() in your code.it will mainloop the Tk() instance in line 14.Because the first Tk() has been covered.当您在代码中使用mainloop() ,它将mainloop第 14 mainloopTk()实例。因为第一个Tk()已被覆盖。

There are two solutions:有两种解决方案:

  1. If you want to show two windows at the same time.Use .after in the first Tk() instance and call a function to show another picture in a Toplevel() instance(Don't use many Tk() instance at the same time.It maybe will bring some bugs).如果你想在同一time.Use显示两个窗口.after第一Tk()实例,并调用一个函数来显示在另一张照片Toplevel()实例(不使用许多Tk()在同一时刻.它可能会带来一些错误)。
  2. Or you want to show it in the same window,Just use .grid() (or others) to place them in the place of the window where you want.或者你想在同一个窗口中显示它,只需使用.grid() (或其他)将它们放在你想要的窗口的位置。

A easy example for .after() to show more than two windows: .after()显示两个以上窗口的简单示例:

from tkinter import *
from PIL import ImageTk,Image

def ShowAnotherWin(i):
    win = Toplevel()
    win.title('Show')
    image= ImageTk.PhotoImage(Image.open(i))
    canvas=Canvas(win,width=420,height=560)
    canvas.create_image(0,0, image= image, anchor=NW)
    canvas.pack()
    win.mainloop()

YourImageList = ['C:/Users/photo.jpg','C:/Users/photo.jpg']
root = Tk()
for i in YourImageList:
    root.after(0,lambda :ShowAnotherWin(i))
root.title('First')
image= ImageTk.PhotoImage(Image.open(r"C:/Users/photo.jpg"))
canvas=Canvas(root,width=420,height=560)
canvas.create_image(0,0, image= image, anchor=NW)
canvas.pack()
root.mainloop()

YourImageList is the Image list that you want to show.You should put your path in it. YourImageList是您要显示的图像列表。您应该将路径放入其中。

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

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