Home > Net >  Dynamically changing the value of a label from another method from another class in tkinter
Dynamically changing the value of a label from another method from another class in tkinter

Time:08-27

I have a small problem where I can not find a suitable solution.

I have two classes, one that deals with Linear Regression and the other, with the GUI view.

The linear regression works, also the method, I checked that with the print method. Now I want to display the results in the view as a label automatically.

That means, using buttons from the View class, I call the methods in the Plot class. Then I want the results that are printed to appear in the View class as a label. That was my approach, but unfortunately it doesn't work... Do you have other ideas?

class Plot():
...
    def importdataset(self):

        dataset = filedialog.askopenfilename()

        df = pd.read_csv(dataset)
        self.x = df.iloc[:, :-1].values
        self.y = df.iloc[:, 1].values
        self.reg = linear_model.LinearRegression()
        self.reg.fit(self.x.reshape(-1,1), self.y)
        self.ax5.scatter(self.x, self.y, color='blue')
        self.ax5.plot(self.x, self.reg.predict(self.x), color='orange')
        self.ax5.set_xlim(min(self.x),max(self.x))
        self.ax5.set_ylim(min(self.y), max(self.y))

        self.canvas.draw_idle()

    def predictvalue(self, X):
        self.y_value = self.reg.predict([[X]])
        print(type(self.y_value))
        print(self.y_value)


    def get_linear_function(self):
        self.getlinearfunction = ttk.StringVar()
        self.text = ("y = "   str(self.reg.coef_)   "* x   "   str(self.reg.intercept_))
        self.getlinearfunction.set(self.text)
        print(self.getlinearfunction) 

class View():
...
        self.plotframe = Plot()

        self.get_linear_function_button = ttk.Button(self.plotframe, text = "Get linear Function", command = lambda: self.plotframe.get_linear_function())
        self.get_linear_function_button.grid(row = 4, column = 0 , sticky= tk.E)


        self.get_linear_function_label_variable = ttk.StringVar(self)
        self.get_linear_function_label_variable.grid(row=2,column=0, in_=self.plotframe)
        self.get_linear_function_label_variable.set(self.plotframe.get_linear_function())
        self.get_linear_function_label = ttk.Label(self.plotframe, textvariable = 
        self.get_linear_function_label_variable)
        self.get_linear_function_label.grid(row = 5, column = 0)

CodePudding user response:

Changes as stated in the comments:

self.plotframe = Plot()
self.label_variable = ttk.StringVar(self)
#self.get_linear_function_label_variable.grid(row=2,column=0, in_=self.plotframe)
#^????
linear_function = lambda var = self.label_variable: self.plotframe.get_linear_function(var)
self.get_linear_function_button = ttk.Button(self.plotframe, text = "Get linear Function", command = linear_function)
self.get_linear_function_button.grid(row = 4, column = 0 , sticky= tk.E)

self.get_linear_function_label = ttk.Label(self.plotframe, textvariable = self.label_variable)
self.get_linear_function_label.grid(row = 5, column = 0)

and your function changes accordingly:

def get_linear_function(self,var):
    self.getlinearfunction = var
    self.text = ("y = "   str(self.reg.coef_)   "* x   "   str(self.reg.intercept_))
    self.getlinearfunction.set(self.text)
    print(self.getlinearfunction.get()) 
  • Related