Tkinter图片未在正确的页面上显示

时间:2020-06-24 14:44:14

标签: python user-interface tkinter

我一般对Python和编码都不熟悉,因此请谨慎使用我的代码。我试图创建一个具有多个窗口的gui,您可以对每个窗口执行操作。我想在一些页面上显示一些图片,以使用户了解他们所选择的内容。我的“主页”上的图像显示正确,但是当我尝试打开一个应该包含其他图像的页面时,该图像将显示在主页上,而不是目标页面上。据我所知,下面是发生此问题的代码块。让我知道是否需要添加更多信息:)

import tkinter as tk
from PIL import ImageTk, Image

class Main(tk.Tk):
    def __init__(self,*args, **kwargs):
        tk.Tk.__init__(self,*args, *kwargs)
        self.config(bg='gray')
        self.title('Functionality')
        self.geometry('560x400')
        test_img = ImageTk.PhotoImage(Image.open('C:\\Users\\myuser\\Desktop\\Python Tests\\pics\\test.jpg'))
        self.label2 = tk.Label(image=test_img)
        self.label2.photo = test_img
        self.label2.grid(column=1, row=0)
        self.label = tk.Label(self, text='Please Select a Choice Below', font=('Arial Bold', 30), bg='gray')
        self.label.grid(column=1, row=1)
        self.button_retorque = tk.Button(self, text='Re-torque', command=lambda: reTorque())
        self.button_retorque.grid(column=1, row=3, pady=10)
        self.quit = tk.Button(self, text='Close Application', command=self.destroy)
        self.quit.grid(column=1, row=5, pady=10)

class reTorque(tk.Toplevel):
    def __init__(self, *args, **kwargs):
        tk.Toplevel.__init__(self, *args, *kwargs)
        self.config(bg='gray')
        self.geometry('750x400')
        self.label = tk.Label(self, text='This is for a Re-torque', font=('Arial Bold', 30), bg='gray')
        self.label.grid(column=0, row=0)
        self.button3 = tk.Button(self, text = 'Set Board Type Odd', command=boardOdd)
        self.button3.grid(column=0, row=1, pady=10)
        self.button = tk.Button(self,text='Close', command=self.destroy)
        self.button.grid(column=0, row=5)
        self.grab_set()

class boardOdd(tk.Toplevel):
    def __init__(self, *args, **kwargs):
        tk.Toplevel.__init__(self, *args, **kwargs)
        self.config(bg='gray')
        self.geometry('500x400')
        self.label = tk.Label(self, text='Select Number of Odd Boards', font=('Arial Bold', 12), bg='gray')
        self.label.grid(column=0, row=0)
        odd_img = ImageTk.PhotoImage(Image.open('C:\\Users\\myuser\\Desktop\\Python Tests\\pics\\odd.jpg'))
        self.label2 = tk.Label(image=odd_img)
        self.label2.photo = odd_img
        self.label2.grid(column=2, row=0)
        self.button1 = tk.Button(self, text='1')
        self.button1.grid(column=0, row=1, pady=10)
        self.button2 = tk.Button(self, text='2')
        self.grab_set()

if __name__ == '__main__':
    app = Main()
    app.mainloop()

编辑:原来我错过了一个非常重要的部分。感谢acw1668和jasonharper告诉我。通过更改为

进行了修复
self.label2 = tk.Label(self,image=test_img)

0 个答案:

没有答案