BrockenDuck
BrockenDuck

Reputation: 220

How to init a Tkinter canvas dependent on a main Tkinter window

I am new to programing and am currently trying to do a program that draws graphs for affine functions (functions under the form f(x)=ax+b) with the Tkinter python library. I am doing a class called graph but when I want to init the canvas object it must be dependent to a frame. How can I init the main frame and a canvas object inside of a class? Here is the code:

class Graph:
    def __init__(self, a="", b="", dimensions=None, graduation=None, graph=tkinter.Tk(), graphing_area=tkinter.Canvas(graph, height=500, width=500)):
        self.a = a
        self.b = b
        self.dimensions = dimensions
        self.graduation = graduation
        self.graph = graph
        self.graphing_area = graphing_area

Upvotes: 0

Views: 411

Answers (1)

Filip Młynarski
Filip Młynarski

Reputation: 3612

Initiate values of graph and graphing_area outside arguments section. If you want option to set graph as function parameter set graph parameter to None by default and set self.graph to graph only if value is different that None.

class Graph:
    def __init__(self, a="", b="", dimensions=None, graduation=None, graph=None):
        self.a = a
        self.b = b
        self.dimensions = dimensions
        self.graduation = graduation
        if not graph:
            self.graph = tkinter.Tk()
        else:
            self.graph = graph
        self.graphing_area = tkinter.Canvas(self.graph, height=500, width=500)

Upvotes: 2

Related Questions