使用tkinter,为什么我在第五个页面加入的图片和第一个页面的图片一块出现而且点击第一个页面的内容第五个页面就直接消失了
这是页面一:
root = Tk()
root.title("mbti十六型人格测试")
root.geometry("800x800")
root.configure(bg="AntiqueWhite")
image = Image.open(r"C://Users//29212//Desktop//mbti1.jpg")
zoom = 1.8
pixels_x, pixels_y = tuple([int(zoom * x) for x in image.size])
img = ImageTk.PhotoImage(image.resize((600, 400)))
label = Label(root, image=img)
label.image = img
label.pack()
这是页面五:
image = Image.open(r"C:\Users\29212\Desktop\results.jpg")
zoom = 1.8
pixels_m, pixels_n = tuple([int(zoom * x) for x in image.size])
img = ImageTk.PhotoImage(image.resize((600, 400)))
label = Label(fifth, image=img)
label.image = img
label.pack()
image = Image.open(r"C:\Users\29212\Desktop\results2.jpg")
zoom = 1.8
pixels_x, pixels_y = tuple([int(zoom * x) for x in image.size])
img = ImageTk.PhotoImage(image.resize((600, 400)))
label = Label(fifth, image=img)
label.image = img
label.pack()
运行结果:
【以下回答由 GPT 生成】
这个问题出现的原因是,你在创建第五个页面的图片时,将pixels_x
和pixels_y
分别赋值给了第一个页面图片的变量名,导致第一个页面也显示了第五个页面的图片,并且点击第一个页面的内容时,第五个页面也消失了。
要修正这个问题,你需要分别使用独立的变量名来存储第一个页面和第五个页面的图片尺寸。以下是修正后的代码示例:
root = Tk()
root.title("mbti十六型人格测试")
root.geometry("800x800")
root.configure(bg="AntiqueWhite")
# 第一个页面
image1 = Image.open(r"C://Users//29212//Desktop//mbti1.jpg")
zoom = 1.8
pixels_x1, pixels_y1 = tuple([int(zoom * x) for x in image1.size])
img1 = ImageTk.PhotoImage(image1.resize((600, 400)))
label1 = Label(root, image=img1)
label1.image = img1
label1.pack()
# 第五个页面
fifth = Toplevel()
fifth.title("第五个页面")
fifth.geometry("800x800")
fifth.configure(bg="AntiqueWhite")
image2 = Image.open(r"C:\Users\29212\Desktop\results.jpg")
zoom = 1.8
pixels_x2, pixels_y2 = tuple([int(zoom * x) for x in image2.size])
img2 = ImageTk.PhotoImage(image2.resize((600, 400)))
label2 = Label(fifth, image=img2)
label2.image = img2
label2.pack()
image3 = Image.open(r"C:\Users\29212\Desktop\results2.jpg")
zoom = 1.8
pixels_x3, pixels_y3 = tuple([int(zoom * x) for x in image3.size])
img3 = ImageTk.PhotoImage(image3.resize((600, 400)))
label3 = Label(fifth, image=img3)
label3.image = img3
label3.pack()
这样就保证了第一个页面和第五个页面的图片分别使用独立的变量存储,并且点击第一个页面的内容不会导致第五个页面消失。
【相关推荐】