Search code examples
pythontextcalculatorindentation

Python 3 Tkinker Text Box: "IndentationError: unexpected indent" Whenever I add new text to result text box


I'm trying to calculate the result of calculator inputs from the result text of my calculator window. It starts with a "0" by default.

When, for example, I add 3: "0 + 3"

However, when I add another action, like add 4, I get this: "0 + 3

  • 4"

For some reason, I keeping getting these indents when I try to add further calculator input. When I attempt to calculate the result, I'm met with "IndentationError: unexpected indent."

Idk why tkinker or python is doing this.

calculator.py

# -*- coding: utf-8 -*-
from classes_GUI import *


# Store input number
def storeInput(entry_text, result_text, action):
    numb = 0.0

    try:
        numb = float(entry_text.retrieveTextInput())
    except ValueError:
        print('Please enter a valid number')
        return

    num = entry_text.retrieveTextInput()

    input_texts = dict([
        (1, ' + ' + str(num)),
        (2, ' - ' + str(num)),
        (3, ' * ' + str(num)),
        (4, ' / ' + str(num)),
        (5, ' % ' + str(num)),
        (6, ' // ' + str(num)),
        (7, ' ** ' + str(num))
    ])

    result_text.changeText(input_texts[action])
    entry_text.clearText()


# Calculate result
def calcResult(entry_text, result_text):
    result = eval(result_text.retrieveTextInput())
    entry_text.clearText()
    result_text.changeText(str(result), True)

classes_GUI.py

# Create a data block for text
class TextBlock(tk.Text):
    def __init__(self, master, row, column, text='', **kwargs):
        tk.Text.__init__(self, master, **asdict(TextDc(**kwargs)))

        self.grid(row=row, column=column)
        self.insert('1.end', text)

    # Clear text
    def clearText(self):
        self.delete('1.0', 'end')

    # Change text
    def changeText(self, new_txt, clear=False):
        self.config(state='normal')
        if clear:
            self.clearText()
        self.insert('end', new_txt)
        self.config(state='disabled')

    # Retrieve input from text box
    def retrieveTextInput(self):
        return self.get('1.0', 'end')

Solution

  • Ok I found the solution to the problem:

    # Store input number
    def storeInput(entry_text, result_text, action):
        numb = 0.0
    
        try:
            numb = float(entry_text.retrieveTextInput())
            numb = str(numb)
        except ValueError:
            print('Please enter a valid number')
            return
    
        input_texts = dict([
            (1, ' + ' + numb),
            (2, ' - ' + numb),
            (3, ' * ' + numb),
            (4, ' / ' + numb),
            (5, ' % ' + numb),
            (6, ' // ' + numb),
            (7, ' ** ' + numb)
        ])
    
        result_text.changeText(input_texts[action])
        entry_text.clearText()
    

    The problem was that I was reading the number input (in this case, the var numb) as a str rather than a float. Reading the variable as a float, and then converting it to a str solved the issue.