import tkinter as tk
from tkinter import *
from PIL import ImageTk, Image
class keyGui():
def __init__(self, parent, *args, **kwargs):
self.parent = parent
root.title("test")
root.overrideredirect(1)
self.frame1 = tk.Frame(root, bg="black")
self.frame1.pack(side=TOP, fill=X)
self.frame2 = tk.Frame(root, bg="black")
self.frame2.pack(side=BOTTOM, fill=X)
self.image = Image.open("sudu.png")
self.image2 = self.image.resize((100, 100), Image.ANTIALIAS)
self.photoImg = ImageTk.PhotoImage(self.image2)
self.keyLabel = tk.Label(self.frame2, image=self.photoImg)
self.keyLabel.grid(column=0, row=0, padx=100)
if __name__ == "__main__":
root = tk.Tk()
keyGui(root)
root.mainloop()
因此,我一直在将GUI重写为一个类,以获取更简洁的代码。但是,我注意到将图像放入课堂后,它不再显示了。如果它不在课程中,则可以正常工作。有什么想法吗?