Home > Back-end >  NameError: name 'value1' is not defined
NameError: name 'value1' is not defined

Time:02-27

I was New to Programming and Python and I was trying Tkinter then I run on this Problem, It will always run on an error when it got to the last click for the Button. I want to get all the Value from value1-5 but it always say "NameError: name 'value1' is not defined". I try defining value1-5 from inside to outside the function and class but it turn to 0 or " "

Please Help

class Spam:

    def click1():
        
        value1= (s1.get())
        Text1 = Label(window, text = value1)
        Text1.grid(row= 3)
        Print2 = Label(window, text = "2nd Number:")
        Print2.grid(row= 4)

        b1.configure( text= 'Submit',command= Spam.click2)
        s1.delete(0, END)
        
        return
    def click2():
        value2= (s1.get())
        Text2 = Label(window, text = value2)
        Text2.grid(row= 5)
        Print3 = Label(window, text = "3nd Number:")
        Print3.grid(row= 6)

        b1.configure(text= 'Submit',command= Spam.click3)
        s1.delete(0, END)
        
        return
    def click3():
        value3= (s1.get())
        result= value3
        Text3 = Label(window, text = value3)
        Text3.grid(row= 7)
        Print4 = Label(window, text = "4th Number:")
        Print4.grid(row= 8)

        b1.configure( text= 'Submit',command= Spam.click4)
        s1.delete(0, END)
           
        return
    def click4():
        value4= (s1.get())
        result= value4
        Text4 = Label(window, text = value4)
        Text4.grid(row= 9)
        Print5 = Label(window, text = "5th Number:")
        Print5.grid(row= 10)

        b1.configure(text= 'Submit',command= Spam.click5)
        s1.delete(0, END)
               
        return
    
    def click5():
        value5= (s1.get())
        result= value5
        Text5 = Label(window, text = value5)
        Text5.grid(row= 11)
        Print6 = Label(window,)
        Print6.grid(row= 12)

        s1.delete(0, END)
        b1.configure( text= 'ANALYZE',command= Spam.SumAll)
        
               
        return
    
    def SumAll():
      
        s1.delete(0, END)      
        b1.destroy()
        Sum= value1   value2
        
        word1 = Label(text= '1st   2nd ='   str(Sum) )
        word1.grid(row= 1, column= 1)
        
        if Sum <= value3:
            Diff= "The Sum is Smaller than the 3rd Number"
            cow = Diff
        if Sum >= value3:
            Siff= "The Sum is Larger than the 3rd Number"
            cow = Siff
        diff= Sum - value3
        
        word2 = Label(text= diff   cow  )
        word2.grid(row= 4 , column= 1 )
        
        
        return

s1 = Entry(window,width=55 ,borderwidth=5, justify='center' )
s1.grid(row= 0, column=0 ,padx = 20, pady = 40)
b1 = Button(window, text = 'Submit', command = Spam.click1)
b1.grid(row= 1, column=0 ,padx = 20, pady = 20)
Print1 = Label(window, text = "1st Number:")
Print1.grid(row= 2)

CodePudding user response:

Did you mean to use an instance of Spam?

class Spam:

    def click1(self):
        
        self.value1= (s1.get())
        Text1 = Label(window, text = self.value1)
        Text1.grid(row= 3)
        Print2 = Label(window, text = "2nd Number:")
        Print2.grid(row= 4)
        
        b1.configure( text= 'Submit',command= self.click2)
        s1.delete(0, END)

    def click2(self):
        ...

    def SumAll(self):
      
        s1.delete(0, END)      
        b1.destroy()
        Sum= self.value1   self.value2

# ...

s = Spam()
...
b1 = Button(window, text = 'Submit', command = s.click1)
...
  • Related