我看不出你在哪里寫self.tk或者你在哪里調用這個類。可能,在聲明window時需要添加self.。 Like this: from tkinter import *class window: def __init__(self, size, title, ): self.size = size self.title = title self.window = Tk() self.window.geometry(self.size) self.window.title(self.title) print('a window has been created') self.window.mainloop() def button(self, window, text, x, y): self.window = window self.text = text self.x = x self.y = y button = Button(window, text=text).place(x=str(x), y=str(y))if __name__ == "__main__": worker = window("500x500", "nice title") # <-- You need to call the class. (如果需要聲明按鈕,可以在self.window.main