Home > Back-end >  unable to find attribute using tkinter
unable to find attribute using tkinter

Time:01-04

i'm having problem with this error, can someone tell me what wrong with my code. im totally new to python coding using tkinter.

i can't find what the problem and im trying ti solve it and i cant get any fix on this area.

im trying to find where the attribute error, i check every lines and i unable to find the area where the error occurred

import tkinter as tk
import math as mt

class truepositioncal:
  def __init__(self):
    
    #create main window
    self.main_window = tk.Tk()
    
    #Main Window Title
    self.main_window.title('True Position Calculator')

    #Create Frame
    self.maxtrueposition_frame = tk.Frame()
    self.nom_x_frame = tk.Frame()
    self.nom_y_frame = tk.Frame()
    self.act_x_frame = tk.Frame()
    self.act_y_frame = tk.Frame()
    self.nom_dia_frame = tk.Frame()
    self.tol_pos_frame = tk.Frame()
    self.tol_neg_frame = tk.Frame()
    self.result_frame = tk.Frame()
    self.calculate_frame = tk.Frame()

    #prompt label Max True position
    self.promptlabelmaxpos = tk.Label(self.maxtrueposition_frame, text = 'Enter Max Position Deviation:')
    #Input Max True Position
    self.maxposdev = tk.Entry(self.maxtrueposition_frame, width = 10)
    self.promptlabelmaxpos.pack(side = 'left')
    self.maxposdev.pack(side = 'left')

    #Prompt label nominal X 
    self.labeldrawingpara_x = tk.Label(self.nom_x_frame, text = 'Enter Nominal X Value:')
    self.nominal_x = tk.Entry(self.nom_x_frame, width=10)

    #pack to left (nominal X)
    self.labeldrawingpara_x.pack(side = 'left')
    self.nominal_x.pack(side = 'left')

    #Prompt label nominal y 
    self.labeldrawingpara_y = tk.Label(self.nom_y_frame, text = 'Enter Nominal Y Value:')
    self.nominal_y = tk.Entry(self.nom_y_frame, width=10)
    self.labeldrawingpara_y.pack(side = 'left')
    self.nominal_y.pack(side = 'left')

    #Prompt label actual X 
    self.labelact_x = tk.Label(self.act_x_frame, text = 'Enter Actual X Value:')
    self.act_x = tk.Entry(self.act_x_frame, width=10)
    self.labelact_x.pack(side = 'left')
    self.act_x.pack(side = 'left')

        #Prompt label Actual Y 
    self.labelact_y = tk.Label(self.act_y_frame, text = 'Enter Actual Y Value:')
    self.act_y = tk.Entry(self.act_y_frame, width=10)
    self.labelact_y.pack(side = 'left')
    self.act_y.pack(side = 'left')

        #Prompt label Nominal Diameter 
    self.labelnomdia = tk.Label(self.nom_dia_frame, text = 'Enter Nominal Diameter Value:')
    self.nom_dia = tk.Entry(self.nom_dia_frame, width=10)
    self.labelnomdia.pack(side = 'left')
    self.nom_dia.pack(side = 'left')  

        #Prompt label tolerance positive 
    self.labeltolpos = tk.Label(self.tol_pos_frame, text = 'Enter Tolerance ( ) Value:')
    self.tol_pos = tk.Entry(self.tol_pos_frame, width=10)
    self.labeltolpos.pack(side = 'left')
    self.tol_pos.pack(side = 'left')  

        #Prompt label tolerance Negative 
    self.labeltolneg = tk.Label(self.tol_neg_frame, text = 'Enter Tolerance (-) Value:')
    self.tol_neg = tk.Entry(self.tol_neg_frame, width=10)
    self.labeltolneg.pack(side = 'left')
    self.tol_neg.pack(side = 'left')  
    
    #Pack into one
    self.maxtrueposition_frame.pack()
    self.nom_x_frame.pack()
    self.nom_y_frame.pack()
    self.act_x_frame.pack()
    self.act_y_frame.pack()
    self.nom_dia_frame.pack()
    self.tol_pos_frame.pack()
    self.tol_neg_frame.pack()

    #Create and pack the widgets for the average     
    self.result_label = tk.Label(self.result_frame,text='True Position Result:')

    self.value = tk.StringVar()
    self.results_label = tk.Label(self.result_frame,
textvariable=self.result)

    self.result_label.pack(side='left') 
    self.results_label.pack(side='left')

    self.calc_button = tk.Button(self.calculate_frame, text='calculate',
command=self.caltruepos)

    self.calculate_button.pack(side = 'left')
  
    tk.mainloop()


def caltruepos(self):

  #Change string into float numbers
  self.maxtrueposition = float(self.maxposdev.get())
  self.nom_x = float(self.nominal_x.get())
  self.nom_y = float(self.nominal_y.get())
  self.act_x = float(self.act_x.get())
  self.act_y = float(self.act_y.get())


  #calculate true position results
  x = self.nom_x - self.act_x
  y = self.nom_y - self.act_y
  square_root = mt.sqrt(x * 2   y * 2)
  true_pos_result = (2 * square_root)

  self.value.set(true_pos_result)


  

#def trueposmmclmc(self):
  
  #self.nom_dia = float(self.nom_dia.get())
  #self.tol_pos = float(self.tol_pos.get())
  #self.tol_neg = float(self.tol_neg.get())

  #calculate true position mmc
  #mmc_a = (self.nom_dia - self.tol_pos)
  #mmc_b = (mmc_a - self.nom_dia   self.maxtrueposition)
  
trueposcal_a = truepositioncal()

enter image description here

CodePudding user response:

The error is telling you precisely what is wrong. You've never defined self.result anywhere, but you're trying to use it as a variable.

If you really did intend to use self.result as a variable, you must define it before using it.

self.result= tk.StringVar()
self.results_label = tk.Label(self.result_frame,
textvariable=self.result)

Your code does define a variable named self.value immediately before that block of code - did you intend to use that instead of self.result? If so, then the fix is to use self.value rather than self.result

self.value = tk.StringVar()
self.results_label = tk.Label(self.result_frame,
textvariable=self.value)
#            ^^^^^^^^^^
  • Related