class PLUI:
def __init__(self):
self.__CANVAS_WIDTH = 320
self.__CANVAS_HEIGHT = 240
self.__aqd = AirQuality()
self.__pop_percentage = self.__aqd.get_percent_population()
self.__pm10 = self.__aqd.get_pm10()
self.__country_list = self.__aqd.get_countries()
self.main_window = None
self.canvas = None
def draw_piechart(self):
self.main_window = Tk()
self.canvas = Canvas(self.main_window,width = self.__CANVAS_WIDTH,height = self.__CANVAS_HEIGHT)
self.canvas.pack()
canvas.create_arc(400,400,100,100,start=0,extent=36,fill="red")
canvas.create_arc(400,400,100,100,start=36,extent=72,fill="green")
canvas.create_arc(400,400,100,100,start=108,extent=108,fill="yellow")
canvas.create_arc(400,400,100,100,start=216,extent=144,fill="blue")
tkinter.mainloop()
plui = PLUI()
plui.draw_piechart()
因为从未定义过canvas
。这一行:
self.canvas = Canvas(self.main_window,width = self.__CANVAS_WIDTH,height = self.__CANVAS_HEIGHT)
将其定义为self
上的属性。因此,将所有canvas.create_arc
调用更改为self.canvas.create_arc