为什么NameError:名称'canvas'未定义

时间:2019-12-06 22:21:08

标签: python tkinter python-3.8

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()

1 个答案:

答案 0 :(得分:1)

因为从未定义public string getPartFromBL(int id) { var client = new RestClient("https://api.bricklink.com/api/store/v1/part/" + id); client.Timeout = -1; var request = new RestRequest(Method.GET); request.AddHeader("Authorization", "OAuth oauth_consumer_key=\"KEY\",oauth_token=\"TOKEN\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" + getNewTimestamp() + "\",oauth_nonce=\"hildehf\",oauth_version=\"1.0\",oauth_signature=\"pn9gQaTah144obUJ3dLJwxWXGtw%3D\""); IRestResponse response = client.Execute(request); Console.WriteLine(response.Content); return response.Content; } 。这行:

canvas

将其定义为self.canvas = Canvas(self.main_window,width = self.__CANVAS_WIDTH,height = self.__CANVAS_HEIGHT) 上的属性。因此,将所有self调用更改为canvas.create_arc