tkinter中错误屏幕上显示的图像

2024-04-29 10:29:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编码比较陌生,我试图在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()

  

Tags: andthetextfrom图像屏幕tkinterthis
2条回答

由于使用了Tk()的多个实例(一个用于第一个窗口,一个用于第二个窗口),因此需要为picture1img3指定父级:

img3 = PhotoImage(master=window2, file=r"C:\Tkinter\ComputerImage.png")
picture1 = Label(window2, image=img3)
picture1.pack()

但是,应该避免使用Tk()的多个实例。最好将Tk()的第二个实例更改为Toplevel()

嘿,你忘了在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()

error error _tkinter.TclError: image "pyimage4" doesn't exist - 您必须在第二个窗口中使用Toplevel()

window2=Toplevel ()

它对我有用

相关问题 更多 >