Home > database >  Creating a small add/subtract calculator with tkinter in Python?
Creating a small add/subtract calculator with tkinter in Python?

Time:10-22

I've for the most part created the program using the tkinter GUI tools, I just cannot see where I've gone wrong in the actual point where the or - are clicked. It should be taking the value entered into the entry field, and adding it to the result label, but it's throwing a TypeError saying can only concatenate str (not "StringVar") to str.

import tkinter

class Calculator_GUI:

def __init__(self):
    self.mw = tkinter.Tk()
    self.mw.title("Calculator")
    
    self.top_frame = tkinter.Frame(self.mw)
    self.mid_frame = tkinter.Frame(self.mw)
    self.bottom_frame = tkinter.Frame(self.mw)
    
    self.prompt_label = tkinter.Label(self.top_frame, text = "Total: ")
    self.num_entry = tkinter.Entry(self.mid_frame, width = 15)
    
    self.add_button = tkinter.Button(self.bottom_frame, text = " ", width = 10, command = self.add)
    self.minus_button = tkinter.Button(self.bottom_frame, text = "-", width = 10, command = self.subtract)
    self.reset_button = tkinter.Button(self.bottom_frame, text = "Reset", width = 10, command = self.reset)
    self.result = tkinter.StringVar()
    self.result_label = tkinter.Label(self.top_frame, textvariable = self.result)
    
    self.prompt_label.pack(side = "left")
    self.num_entry.pack(side = "left")
    self.add_button.pack(side = "left")
    self.minus_button.pack(side = "left")
    self.reset_button.pack(side = "left")
    self.result_label.pack(side = "left")
    
    self.top_frame.pack()
    self.mid_frame.pack()
    self.bottom_frame.pack()
    
    tkinter.mainloop()
    
def add(self):
    self.result = float(self.num_entry.get()   self.result)
    
def subtract(self):
    self.result = float(self.result - self.num_entry.get())
    
def reset(self):
    self.result.set(0)
    
calc = Calculator_GUI()
    

CodePudding user response:

self.result is of type StringVar and StringVar.get() returns type str, therefore this error makes sense. Moreover, due to the same fact, you need to use the setter method set() in order to set your result label.
I tried it real quick and it works so far with one problem: it concatinates the strings instead of adding their parsed values. You need to parse the str before adding them.

CodePudding user response:

You need to set and convert correctly

import tkinter

class Calculator_GUI:

    def __init__(self):
        self.mw = tkinter.Tk()
        self.mw.title("Calculator")
        
        self.top_frame = tkinter.Frame(self.mw)
        self.mid_frame = tkinter.Frame(self.mw)
        self.bottom_frame = tkinter.Frame(self.mw)
        
        self.prompt_label = tkinter.Label(self.top_frame, text = "Total: ")
        self.num_entry = tkinter.Entry(self.mid_frame, width = 15)
        
        self.add_button = tkinter.Button(self.bottom_frame, text = " ", width = 10, command = self.add)
        self.minus_button = tkinter.Button(self.bottom_frame, text = "-", width = 10, command = self.subtract)
        self.reset_button = tkinter.Button(self.bottom_frame, text = "Reset", width = 10, command = self.reset)
        self.result = tkinter.StringVar()
        self.result.set('0')
        self.result_label = tkinter.Label(self.top_frame, textvariable = self.result)
        
        self.prompt_label.pack(side = "left")
        self.num_entry.pack(side = "left")
        self.add_button.pack(side = "left")
        self.minus_button.pack(side = "left")
        self.reset_button.pack(side = "left")
        self.result_label.pack(side = "left")
        
        self.top_frame.pack()
        self.mid_frame.pack()
        self.bottom_frame.pack()
        
        tkinter.mainloop()
        
    def add(self):
        self.result.set (str(float(self.num_entry.get())   float(self.result.get())))
        
    def subtract(self):
        self.result.set (str(float(self.result.get()) - float(self.num_entry.get())))
        
    def reset(self):
        self.result.set('0')
    
calc = Calculator_GUI()

CodePudding user response:

Since self.result is a StringVar, you need to use self.result.set(...) to update its value.

Also you need to convert the strings returned by .get() to number before doing the addition and substraction.

class Calculator_GUI:

    def __init__(self):
        ...
        self.num_entry = tkinter.Entry(self.mid_frame, width = 15)
        self.num_entry.insert(0, '0') # initialize to zero 
        ...

    def add(self):
        try:
            self.result.set(float(self.num_entry.get())   float(self.result.get()))
        except ValueError as e:
            print(e)

    def subtract(self):
        try:
            self.result.set(float(self.result.get()) - float(self.num_entry.get()))
        except ValueError as e:
            print(e)

...
  • Related