我是编程新手,正在尝试在tkinter上显示图像,我将其设置为当你按下第一个屏幕上的按钮时,它将打开另一个屏幕-我已经让这个工作正常我的问题是,当新窗口打开时,应该在第二个屏幕上的图像会出现在第一个屏幕上,而我不知道如何修复它。我没有得到任何错误,它只是把图像放在第一个屏幕的中间,我希望这是有意义的。谢谢
代码如下:(对于第二个屏幕,它应该显示图像,但没有显示)
from tkinter import *
window2 = Tk()
window2.geometry("1920x1200")
Namearea = Label(window2, text = "Please Name the Prebuild: ")
Namearea.pack()
e = Entry(window2, width=50, borderwidth=3, bg="Light Grey", fg="black")
e.pack()
e.insert(0, "Remove this text and Enter the name of your prebuild: ")
# this is the part for the image
img3 = PhotoImage(file=r"C:\Tkinter\ComputerImage.png ")
picture1 = Label(image=img3)
picture1.pack()
SaveAndContinue = Button(window2, text = "Save and Return to Main Menu", padx = 75, pady = 20, bg = "Light Grey")
SaveAndContinue.pack()
LinkTitle = Label(window2, text = "Here are some links to purchase the parts from:")
Link1 = Label(window2, text = "Scorptec: www.scorptec.com.au/")
Link2 = Label(window2, text = "Centre-Com: www.centrecom.com.au/")
LinkTitle.pack()
Link1.pack()
Link2.pack()发布于 2020-08-03 14:45:14
由于您使用了多个Tk()实例(一个用于第一个窗口,另一个用于第二个窗口),因此需要为picture1和img3指定父级
img3 = PhotoImage(master=window2, file=r"C:\Tkinter\ComputerImage.png")
picture1 = Label(window2, image=img3)
picture1.pack()但是,您应该避免使用多个Tk()实例。最好将Tk()的第二个实例更改为Toplevel()。
发布于 2020-08-03 12:51:03
嘿,你忘了在picture1 = Label(image=img3)里提到窗口名了
这就是正确的错误
# this is the part for the image
img3 = PhotoImage(file=r"C:\\Tkinter\\ComputerImage.png ")
picture1 = Label(window2,image=img3)
picture1.pack()和你必须在第二个窗口中使用Toplevel()的error error _tkinter.TclError: image "pyimage4" doesn't exist -
window2=Toplevel ()对我来说很管用
https://stackoverflow.com/questions/63223565
复制相似问题